diff --git a/src/lib.rs b/src/lib.rs index bf3ca42..02ca008 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,9 +20,6 @@ pub trait Framing { pub trait Element { fn render(&self) -> String; - fn get_colors(&self) -> std::collections::HashMap { - std::collections::HashMap::new() - } } // Types @@ -59,6 +56,10 @@ impl Props { } } + pub fn auto() -> Self { + Self::new(0, 0) + } + pub fn center() -> Self { Self { x: style::Position::Center, @@ -92,6 +93,16 @@ impl Props { } } + pub fn x_percent(&mut self, p: u16) -> Self { + self.x = style::Position::Percent(p); + self.clone() + } + + pub fn y_percent(&mut self, p: u16) -> Self { + self.y = style::Position::Percent(p); + self.clone() + } + pub fn style(&mut self, style: style::Style) -> Self { self.style = style; self.clone() @@ -111,6 +122,16 @@ impl Props { self.height = style::Dimension::Num(h); self.clone() } + + pub fn width_percent(&mut self, w: u16) -> Self { + self.width = style::Dimension::Percent(w); + self.clone() + } + + pub fn height_percent(&mut self, h: u16) -> Self { + self.height = style::Dimension::Percent(h); + self.clone() + } } impl Framing for Console<'_> { @@ -168,6 +189,8 @@ impl Framing for Console<'_> { ) .unwrap(); } + } else { + panic!("Handle not found"); } } } diff --git a/src/main.rs b/src/main.rs index c15ab7c..c0178c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,14 +26,6 @@ fn main() -> Result<()> { pub fn app() -> Ui { Arc::new(|frame| { - frame.draw( - &"Hello, World", - &Props::center().state("hover", Style { - color: Color::Red, - background: Color::Red, - px: 1, - py: 0, - }), - ); + frame.draw(&"this is centered!", &Props::center()); }) } diff --git a/src/style.rs b/src/style.rs index 1368ee8..60434d2 100644 --- a/src/style.rs +++ b/src/style.rs @@ -23,30 +23,36 @@ pub enum Color { impl Color { /// Converts the enum to its ANSI escape code pub fn ansi_number(&self, fg: bool) -> String { - let f = if fg { "3" } else { "4" }; - match self { - Self::NoColor => String::new(), - Self::Black => format!("{f}0"), - Self::Red => format!("{f}1"), - Self::Green => format!("{f}2"), - Self::Yellow => format!("{f}3"), - Self::Blue => format!("{f}4"), - Self::Magenta => format!("{f}5"), - Self::Cyan => format!("{f}6"), - Self::White => format!("{f}7"), - Self::BrightBlack => format!("{f}90"), - Self::BrightRed => format!("{f}91"), - Self::BrightGreen => format!("{f}92"), - Self::BrightYellow => format!("{f}93"), - Self::BrightBlue => format!("{f}94"), - Self::BrightMagenta => format!("{f}95"), - Self::BrightCyan => format!("{f}96"), - Self::BrightWhite => format!("{f}97"), - Self::Rgb(r, g, b) => { - let code = if fg { "38" } else { "48" }; - format!("{code};2;{r};{g};{b}") - } + let f = if fg { 30 } else { 40 }; + + if *self == Self::NoColor { + return String::new(); + } else if let Self::Rgb(r, g, b) = self { + return format!("{};2;{r};{g};{b}", f + 8); } + + (f + match self { + Self::Rgb(_, _, _) => 0, + Self::NoColor => 0, + + Self::Black => 0, + Self::Red => 1, + Self::Green => 2, + Self::Yellow => 3, + Self::Blue => 4, + Self::Magenta => 5, + Self::Cyan => 6, + Self::White => 7, + Self::BrightBlack => 60, + Self::BrightRed => 61, + Self::BrightGreen => 62, + Self::BrightYellow => 63, + Self::BrightBlue => 64, + Self::BrightMagenta => 65, + Self::BrightCyan => 66, + Self::BrightWhite => 67, + }) + .to_string() } pub fn to_ansi(&self, foreground: bool) -> String { @@ -94,12 +100,14 @@ pub struct Style { #[derive(Debug, Clone, Copy)] pub enum Position { Num(u16), + Percent(u16), Center, } impl Position { pub fn get(&self, written: u16, frame: u16) -> u16 { match self { Self::Center => (frame - written) / 2, + Self::Percent(percent) => (((*percent as f32) / 100.0) * (frame as f32)) as u16, Self::Num(n) => *n, } } @@ -108,19 +116,22 @@ impl Position { #[derive(Debug, Clone, Copy)] pub enum Dimension { Num(u16), + Percent(u16), Auto, } impl Dimension { pub fn get(&self, _written: u16, frame: u16) -> u16 { match self { Self::Auto => frame, + Self::Percent(percent) => (((*percent as f32) / 100.0) * (frame as f32)) as u16, Self::Num(n) => *n, } } - pub fn get_root(&self, written: u16, _frame: u16) -> u16 { + pub fn get_root(&self, written: u16, frame: u16) -> u16 { match self { Self::Auto => written, + Self::Percent(percent) => (((*percent as f32) / 100.0) * (frame as f32)) as u16, Self::Num(n) => *n, } } diff --git a/src/utils.rs b/src/utils.rs index 5b93987..adb36fe 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -116,16 +116,3 @@ pub fn init<'a>(mouse: bool) -> crate::Result> { mouse_position: None, }) } - -pub fn calculate_padding(string_length: usize, width: usize) -> (usize, usize) { - if width <= string_length { - // No padding needed if the string length is greater than or equal to the width - return (0, 0); - } - - let total_padding = width - string_length; - let left_padding = total_padding / 2; // Floor division for left padding - let right_padding = total_padding - left_padding; // Remaining for right padding - - (left_padding, right_padding) -}