precentage

This commit is contained in:
2025-01-25 22:07:27 +01:00
parent 5a9597aed4
commit eb90f619ce
4 changed files with 62 additions and 49 deletions
+26 -3
View File
@@ -20,9 +20,6 @@ pub trait Framing {
pub trait Element { pub trait Element {
fn render(&self) -> String; fn render(&self) -> String;
fn get_colors(&self) -> std::collections::HashMap<String, (style::Color, bool)> {
std::collections::HashMap::new()
}
} }
// Types // Types
@@ -59,6 +56,10 @@ impl Props {
} }
} }
pub fn auto() -> Self {
Self::new(0, 0)
}
pub fn center() -> Self { pub fn center() -> Self {
Self { Self {
x: style::Position::Center, 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 { pub fn style(&mut self, style: style::Style) -> Self {
self.style = style; self.style = style;
self.clone() self.clone()
@@ -111,6 +122,16 @@ impl Props {
self.height = style::Dimension::Num(h); self.height = style::Dimension::Num(h);
self.clone() 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<'_> { impl Framing for Console<'_> {
@@ -168,6 +189,8 @@ impl Framing for Console<'_> {
) )
.unwrap(); .unwrap();
} }
} else {
panic!("Handle not found");
} }
} }
} }
+1 -9
View File
@@ -26,14 +26,6 @@ fn main() -> Result<()> {
pub fn app() -> Ui { pub fn app() -> Ui {
Arc::new(|frame| { Arc::new(|frame| {
frame.draw( frame.draw(&"this is centered!", &Props::center());
&"Hello, World",
&Props::center().state("hover", Style {
color: Color::Red,
background: Color::Red,
px: 1,
py: 0,
}),
);
}) })
} }
+35 -24
View File
@@ -23,30 +23,36 @@ pub enum Color {
impl Color { impl Color {
/// Converts the enum to its ANSI escape code /// Converts the enum to its ANSI escape code
pub fn ansi_number(&self, fg: bool) -> String { pub fn ansi_number(&self, fg: bool) -> String {
let f = if fg { "3" } else { "4" }; let f = if fg { 30 } else { 40 };
match self {
Self::NoColor => String::new(), if *self == Self::NoColor {
Self::Black => format!("{f}0"), return String::new();
Self::Red => format!("{f}1"), } else if let Self::Rgb(r, g, b) = self {
Self::Green => format!("{f}2"), return format!("{};2;{r};{g};{b}", f + 8);
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}")
}
} }
(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 { pub fn to_ansi(&self, foreground: bool) -> String {
@@ -94,12 +100,14 @@ pub struct Style {
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum Position { pub enum Position {
Num(u16), Num(u16),
Percent(u16),
Center, Center,
} }
impl Position { impl Position {
pub fn get(&self, written: u16, frame: u16) -> u16 { pub fn get(&self, written: u16, frame: u16) -> u16 {
match self { match self {
Self::Center => (frame - written) / 2, Self::Center => (frame - written) / 2,
Self::Percent(percent) => (((*percent as f32) / 100.0) * (frame as f32)) as u16,
Self::Num(n) => *n, Self::Num(n) => *n,
} }
} }
@@ -108,19 +116,22 @@ impl Position {
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum Dimension { pub enum Dimension {
Num(u16), Num(u16),
Percent(u16),
Auto, Auto,
} }
impl Dimension { impl Dimension {
pub fn get(&self, _written: u16, frame: u16) -> u16 { pub fn get(&self, _written: u16, frame: u16) -> u16 {
match self { match self {
Self::Auto => frame, Self::Auto => frame,
Self::Percent(percent) => (((*percent as f32) / 100.0) * (frame as f32)) as u16,
Self::Num(n) => *n, 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 { match self {
Self::Auto => written, Self::Auto => written,
Self::Percent(percent) => (((*percent as f32) / 100.0) * (frame as f32)) as u16,
Self::Num(n) => *n, Self::Num(n) => *n,
} }
} }
-13
View File
@@ -116,16 +116,3 @@ pub fn init<'a>(mouse: bool) -> crate::Result<crate::Console<'a>> {
mouse_position: None, 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)
}