diff --git a/README.md b/README.md index 488b810..b62090d 100644 --- a/README.md +++ b/README.md @@ -30,11 +30,8 @@ ## Getting Started -To use OSUI in your project, include it in your `Cargo.toml`: - -```toml -[dependencies] -osui = "0.2" # Replace with the latest version +```bash +cargo add osui ``` ## Example Usage diff --git a/src/css.rs b/src/css.rs index a7e88d4..19eddcc 100644 --- a/src/css.rs +++ b/src/css.rs @@ -25,6 +25,14 @@ macro_rules! __css { $crate::__css!($_style, $($($other)*)?); }}; + // Vector + ( + $_style:expr, $name:ident: [$($inner:tt),*] $(, $($other:tt)*)? + ) => {{ + $_style.$name = vec![$($inner),*]; + $crate::__css!($_style, $($($other)*)?); + }}; + // Normal ( $_style:expr, $name:ident: $value:expr $(, $($other:tt)*)? @@ -32,6 +40,7 @@ macro_rules! __css { $_style.$name = $value; $crate::__css!($_style, $($($other)*)?); }}; + ( $_style:expr, ($name:ident): $value:expr $(, $($other:tt)*)? ) => {{ diff --git a/src/examples.rs b/src/examples.rs index f08fbc4..24deab3 100644 --- a/src/examples.rs +++ b/src/examples.rs @@ -3,7 +3,10 @@ use crate::prelude::*; pub fn examples() -> Element { rsx! { styling: Some(styling()), - button { class: "btn", "Hello, World!" } + + button { class: "btn", "Click me!" } + + text { "Welcome!" } } } @@ -11,7 +14,7 @@ pub fn styling() -> Css { css! { .btn { x: Center, - y: Auto, + y: Center, color: Red, } diff --git a/src/lib.rs b/src/lib.rs index be7e663..a28f81c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,13 +10,12 @@ //! ```rust //! use osui::prelude::*; //! -//! run(&mut rsx! { +//! launch(rsx! { //! text { "Hello, World!" } //! }); //! ``` //! //! ## Modules -//! - `app` - The main function for rendering. //! - `ui` - Contains all user interface components, enabling rich CLI experiences. //! - `utils` - Utility functions for common TUI tasks such as clearing the screen. @@ -36,9 +35,10 @@ pub mod utils; pub mod prelude { pub use crate::ui::Color::*; pub use crate::ui::Number::*; - pub use crate::{self as osui, css, ersx, rsx, ui::*, Handler, launch}; + pub use crate::ui::Font::*; + pub use crate::{self as osui, css, ersx, launch, rsx, ui::*, Handler}; pub use crate::{call, Children, Element, ElementCore, ElementWidget}; - pub use crate::{style, Command, Document, RenderResult, RenderWriter, Value}; + pub use crate::{style, Document, RenderResult, RenderWriter}; pub use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind}; pub fn sleep(ms: u64) { std::thread::sleep(std::time::Duration::from_millis(ms)); @@ -95,32 +95,6 @@ pub struct RenderWriter { /// Enums ////////////////////////////////////////////////////////////////////////////////////////////////// -#[derive(Debug, Clone, Copy)] -pub enum Value { - Default(T), - Custom(T), -} - -/// Child enum of an `Element` -/// -/// # Example -/// ``` -/// // Check if it's text -/// if let Children::Text(text) { -/// // do something -/// } -/// // Check if it's inner elements -/// if let Children::Children(text) { -/// // do something -/// } -/// // recommended: use get_text -/// children.get_text() // returns a empty string if it's not text -/// ``` -/// -/// # Useful for -/// - `Element::event` -/// - `Element::render` -/// - `Handler` #[derive(Debug)] pub enum Children { None, @@ -128,42 +102,10 @@ pub enum Children { Children(Vec, usize), } -pub enum Command { - Exit, - Render, - GetElementById(String), -} - -pub enum CommandResult { - Element(*mut Element), - None, -} - ////////////////////////////////////////////////////////////////////////////////////////////////// /// Implementations ////////////////////////////////////////////////////////////////////////////////////////////////// -impl Value { - pub fn get_value(&self) -> T { - match self { - Value::Custom(s) => *s, - Value::Default(s) => *s, - } - } - - pub fn try_set_value(&mut self, value: T) { - if let Value::Default(_) = *self { - *self = Value::Default(value); - } - } -} - -impl Default for Value { - fn default() -> Self { - Self::Default(T::default()) - } -} - impl Document { pub fn exit(&self) { if !self.running.is_null() { diff --git a/src/macros.rs b/src/macros.rs index 9804bd6..b5db83a 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,10 +1,3 @@ -#[macro_export] -macro_rules! val { - ($a:expr) => { - $crate::Value::new($a) - }; -} - #[macro_export] macro_rules! call { ($self:ident.$handler:ident ($($inner:tt)*)) => {{ diff --git a/src/ui/styles.rs b/src/ui/styles.rs index 7573daf..68eac40 100644 --- a/src/ui/styles.rs +++ b/src/ui/styles.rs @@ -29,13 +29,11 @@ clone_trait_object!(StyleCore); #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum Font { - None, Bold, Underline, Italic, Reverse, Strike, - Mul(Vec), } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -77,7 +75,7 @@ pub enum StyleName { pub struct StyleElement { pub color: Color, pub background: Color, - pub font: Font, + pub font: Vec, pub x: Number, pub y: Number, pub width: Number, @@ -138,7 +136,7 @@ impl Default for StyleElement { StyleElement { color: Color::NoColor, background: Color::NoColor, - font: Font::None, + font: Vec::new(), x: Number::Default, y: Number::Default, width: Number::Default, @@ -228,12 +226,6 @@ impl Default for Color { } } -impl Default for Font { - fn default() -> Self { - Font::None - } -} - fn hex_to_rgb(hex: &str) -> (u8, u8, u8) { let hex = hex.trim_start_matches('#'); @@ -263,25 +255,39 @@ fn hex_to_rgb(hex: &str) -> (u8, u8, u8) { impl StyleCore for Font { fn ansi(&self) -> String { String::from(match self { - Font::None => "", Font::Bold => "\x1b[1m", Font::Underline => "\x1b[4m", Font::Italic => "\x1b[3m", Font::Reverse => "\x1b[7m", Font::Strike => "\x1b[9m", - Font::Mul(v) => { - let mut s = String::new(); - for n in v { - s += n.ansi().as_str(); - } - return s; - } }) } fn ansi_bg(&self) -> String { self.ansi() } fn is_null(&self) -> bool { - *self == Self::None + false + } +} + +impl StyleCore for Vec { + fn ansi(&self) -> String { + let mut a = String::new(); + for i in self { + a += i.ansi().as_str(); + } + a + } + + fn ansi_bg(&self) -> String { + let mut a = String::new(); + for i in self { + a += i.ansi_bg().as_str(); + } + a + } + + fn is_null(&self) -> bool { + self.len() == 0 } }