some more structural changes
This commit is contained in:
+12
-177
@@ -1,180 +1,3 @@
|
||||
/// Represents a style configuration for TUI elements, including background,
|
||||
/// foreground, outline colors, and font settings for different element states
|
||||
/// (hovered, clicked, selected).
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Style {
|
||||
/// Background color of the element.
|
||||
pub bg: Color,
|
||||
/// Foreground color of the element.
|
||||
pub fg: Color,
|
||||
/// Outline color of the element.
|
||||
pub outline: Color,
|
||||
/// Font style for the element.
|
||||
pub font: Font,
|
||||
|
||||
/// Background color when the element is hovered.
|
||||
pub hover_bg: Color,
|
||||
/// Foreground color when the element is hovered.
|
||||
pub hover_fg: Color,
|
||||
/// Outline color when the element is hovered.
|
||||
pub hover_outline: Color,
|
||||
/// Font style when the element is hovered.
|
||||
pub hover_font: Font,
|
||||
/// Foreground color for the cursor when hovered.
|
||||
pub hover_cursor_fg: Color,
|
||||
/// Background color for the cursor when hovered.
|
||||
pub hover_cursor_bg: Color,
|
||||
|
||||
/// Background color when the element is clicked.
|
||||
pub clicked_bg: Color,
|
||||
/// Foreground color when the element is clicked.
|
||||
pub clicked_fg: Color,
|
||||
/// Outline color when the element is clicked.
|
||||
pub clicked_outline: Color,
|
||||
/// Font style when the element is clicked.
|
||||
pub clicked_font: Font,
|
||||
|
||||
/// Background color when the element is selected.
|
||||
pub selected_bg: Color,
|
||||
/// Foreground color when the element is selected.
|
||||
pub selected_fg: Color,
|
||||
/// Font style when the element is selected.
|
||||
pub selected_font: Font,
|
||||
|
||||
/// Foreground color for the cursor.
|
||||
pub cursor_fg: Color,
|
||||
/// Background color for the cursor.
|
||||
pub cursor_bg: Color,
|
||||
|
||||
/// Indicates if the style is active, affecting its state-based styling.
|
||||
pub is_active: bool,
|
||||
}
|
||||
|
||||
impl Default for Style {
|
||||
/// Creates a default `Style` instance with `None` for all color and font fields
|
||||
/// and inactive (`is_active` set to false).
|
||||
fn default() -> Style {
|
||||
Style {
|
||||
bg: Color::None,
|
||||
fg: Color::None,
|
||||
outline: Color::None,
|
||||
font: Font::None,
|
||||
hover_bg: Color::None,
|
||||
hover_fg: Color::None,
|
||||
hover_outline: Color::None,
|
||||
hover_font: Font::None,
|
||||
hover_cursor_fg: Color::None,
|
||||
hover_cursor_bg: Color::None,
|
||||
clicked_bg: Color::None,
|
||||
clicked_fg: Color::None,
|
||||
clicked_outline: Color::None,
|
||||
clicked_font: Font::None,
|
||||
selected_bg: Color::None,
|
||||
selected_fg: Color::None,
|
||||
selected_font: Font::None,
|
||||
cursor_fg: Color::None,
|
||||
cursor_bg: Color::None,
|
||||
is_active: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Style {
|
||||
/// Generates the ANSI code for the current style, using the active color
|
||||
/// and font if `is_active` is true.
|
||||
pub fn get(&self) -> String {
|
||||
if self.is_active {
|
||||
format!(
|
||||
"{}{}{}",
|
||||
self.fg.prioritize(&self.hover_fg).ansi(),
|
||||
self.bg.prioritize(&self.hover_bg).ansi_bg(),
|
||||
self.font.prioritize(&self.hover_font).ansi()
|
||||
)
|
||||
} else {
|
||||
format!(
|
||||
"{}{}{}",
|
||||
self.fg.ansi(),
|
||||
self.bg.ansi_bg(),
|
||||
self.font.ansi()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the outline color in ANSI format, applying hover style if active.
|
||||
pub fn get_outline(&self) -> String {
|
||||
if self.is_active {
|
||||
self.outline.prioritize(&self.hover_outline).ansi()
|
||||
} else {
|
||||
self.outline.ansi()
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieves the clicked style ANSI sequence for the element.
|
||||
pub fn get_clicked(&self) -> String {
|
||||
format!(
|
||||
"{}{}{}",
|
||||
self.fg.prioritize(&self.clicked_fg).ansi(),
|
||||
self.bg.prioritize(&self.clicked_bg).ansi_bg(),
|
||||
self.font.prioritize(&self.clicked_font).ansi()
|
||||
)
|
||||
}
|
||||
|
||||
/// Retrieves the selected style ANSI sequence for the element.
|
||||
pub fn get_selected(&self) -> String {
|
||||
format!(
|
||||
"{}{}{}",
|
||||
self.fg.prioritize(&self.selected_fg).ansi(),
|
||||
self.bg.prioritize(&self.selected_bg).ansi_bg(),
|
||||
self.font.prioritize(&self.selected_font).ansi()
|
||||
)
|
||||
}
|
||||
|
||||
/// Retrieves the cursor style ANSI sequence for the element, considering hover.
|
||||
pub fn get_cursor(&self) -> String {
|
||||
if self.is_active {
|
||||
format!(
|
||||
"{}{}",
|
||||
self.cursor_fg.prioritize(&self.hover_cursor_fg).ansi(),
|
||||
self.cursor_bg.prioritize(&self.hover_cursor_bg).ansi_bg(),
|
||||
)
|
||||
} else {
|
||||
format!("{}{}", self.cursor_fg.ansi(), self.cursor_bg.ansi_bg())
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes the given string with the active style's ANSI sequences.
|
||||
pub fn write(&self, s: &str) -> String {
|
||||
format!("{}{}\x1b[0m", self.get(), s)
|
||||
}
|
||||
|
||||
/// Writes a string with the outline color applied.
|
||||
pub fn write_outline(&self, s: &str) -> String {
|
||||
format!("{}{}\x1b[0m", self.get_outline(), s)
|
||||
}
|
||||
|
||||
/// Writes a string with the clicked style applied.
|
||||
pub fn write_clicked(&self, s: &str) -> String {
|
||||
format!("{}{}\x1b[0m", self.get_clicked(), s)
|
||||
}
|
||||
|
||||
/// Writes a string with the selected style applied.
|
||||
pub fn write_selected(&self, s: &str) -> String {
|
||||
format!("{}{}\x1b[0m", self.get_selected(), s)
|
||||
}
|
||||
|
||||
/// Writes a string with the cursor style applied.
|
||||
pub fn write_cursor(&self, s: &str) -> String {
|
||||
format!("{}{}\x1b[0m", self.get_cursor(), s)
|
||||
}
|
||||
|
||||
/// Clones the style and sets `is_active` based on the given state.
|
||||
pub fn use_style(&self, state: &usize) -> Style {
|
||||
let mut style = self.clone();
|
||||
style.is_active = *state == 1;
|
||||
style
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents different font styles that can be applied to TUI elements.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Font {
|
||||
@@ -280,3 +103,15 @@ impl Color {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Color {
|
||||
fn default() -> Self {
|
||||
Color::None
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Font {
|
||||
fn default() -> Self {
|
||||
Font::None
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user