small refractor

This commit is contained in:
2024-11-25 23:45:32 +01:00
parent 6d86e8a42b
commit fd4f905f22
6 changed files with 45 additions and 95 deletions
+9
View File
@@ -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)*)?
) => {{
+5 -2
View File
@@ -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,
}
+4 -62
View File
@@ -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<T: Copy + PartialEq> {
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<Element>, usize),
}
pub enum Command {
Exit,
Render,
GetElementById(String),
}
pub enum CommandResult {
Element(*mut Element),
None,
}
//////////////////////////////////////////////////////////////////////////////////////////////////
/// Implementations
//////////////////////////////////////////////////////////////////////////////////////////////////
impl<T: Copy + PartialEq> Value<T> {
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<T: Copy + PartialEq + Default> Default for Value<T> {
fn default() -> Self {
Self::Default(T::default())
}
}
impl Document {
pub fn exit(&self) {
if !self.running.is_null() {
-7
View File
@@ -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)*)) => {{
+25 -19
View File
@@ -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<Font>),
}
#[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<Font>,
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<Font> {
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
}
}