small refractor
This commit is contained in:
@@ -30,11 +30,8 @@
|
|||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
To use OSUI in your project, include it in your `Cargo.toml`:
|
```bash
|
||||||
|
cargo add osui
|
||||||
```toml
|
|
||||||
[dependencies]
|
|
||||||
osui = "0.2" # Replace with the latest version
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example Usage
|
## Example Usage
|
||||||
|
|||||||
@@ -25,6 +25,14 @@ macro_rules! __css {
|
|||||||
$crate::__css!($_style, $($($other)*)?);
|
$crate::__css!($_style, $($($other)*)?);
|
||||||
}};
|
}};
|
||||||
|
|
||||||
|
// Vector
|
||||||
|
(
|
||||||
|
$_style:expr, $name:ident: [$($inner:tt),*] $(, $($other:tt)*)?
|
||||||
|
) => {{
|
||||||
|
$_style.$name = vec![$($inner),*];
|
||||||
|
$crate::__css!($_style, $($($other)*)?);
|
||||||
|
}};
|
||||||
|
|
||||||
// Normal
|
// Normal
|
||||||
(
|
(
|
||||||
$_style:expr, $name:ident: $value:expr $(, $($other:tt)*)?
|
$_style:expr, $name:ident: $value:expr $(, $($other:tt)*)?
|
||||||
@@ -32,6 +40,7 @@ macro_rules! __css {
|
|||||||
$_style.$name = $value;
|
$_style.$name = $value;
|
||||||
$crate::__css!($_style, $($($other)*)?);
|
$crate::__css!($_style, $($($other)*)?);
|
||||||
}};
|
}};
|
||||||
|
|
||||||
(
|
(
|
||||||
$_style:expr, ($name:ident): $value:expr $(, $($other:tt)*)?
|
$_style:expr, ($name:ident): $value:expr $(, $($other:tt)*)?
|
||||||
) => {{
|
) => {{
|
||||||
|
|||||||
+5
-2
@@ -3,7 +3,10 @@ use crate::prelude::*;
|
|||||||
pub fn examples() -> Element {
|
pub fn examples() -> Element {
|
||||||
rsx! {
|
rsx! {
|
||||||
styling: Some(styling()),
|
styling: Some(styling()),
|
||||||
button { class: "btn", "Hello, World!" }
|
|
||||||
|
button { class: "btn", "Click me!" }
|
||||||
|
|
||||||
|
text { "Welcome!" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -11,7 +14,7 @@ pub fn styling() -> Css {
|
|||||||
css! {
|
css! {
|
||||||
.btn {
|
.btn {
|
||||||
x: Center,
|
x: Center,
|
||||||
y: Auto,
|
y: Center,
|
||||||
color: Red,
|
color: Red,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-62
@@ -10,13 +10,12 @@
|
|||||||
//! ```rust
|
//! ```rust
|
||||||
//! use osui::prelude::*;
|
//! use osui::prelude::*;
|
||||||
//!
|
//!
|
||||||
//! run(&mut rsx! {
|
//! launch(rsx! {
|
||||||
//! text { "Hello, World!" }
|
//! text { "Hello, World!" }
|
||||||
//! });
|
//! });
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## Modules
|
//! ## Modules
|
||||||
//! - `app` - The main function for rendering.
|
|
||||||
//! - `ui` - Contains all user interface components, enabling rich CLI experiences.
|
//! - `ui` - Contains all user interface components, enabling rich CLI experiences.
|
||||||
//! - `utils` - Utility functions for common TUI tasks such as clearing the screen.
|
//! - `utils` - Utility functions for common TUI tasks such as clearing the screen.
|
||||||
|
|
||||||
@@ -36,9 +35,10 @@ pub mod utils;
|
|||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use crate::ui::Color::*;
|
pub use crate::ui::Color::*;
|
||||||
pub use crate::ui::Number::*;
|
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::{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 use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind};
|
||||||
pub fn sleep(ms: u64) {
|
pub fn sleep(ms: u64) {
|
||||||
std::thread::sleep(std::time::Duration::from_millis(ms));
|
std::thread::sleep(std::time::Duration::from_millis(ms));
|
||||||
@@ -95,32 +95,6 @@ pub struct RenderWriter {
|
|||||||
/// Enums
|
/// 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)]
|
#[derive(Debug)]
|
||||||
pub enum Children {
|
pub enum Children {
|
||||||
None,
|
None,
|
||||||
@@ -128,42 +102,10 @@ pub enum Children {
|
|||||||
Children(Vec<Element>, usize),
|
Children(Vec<Element>, usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Command {
|
|
||||||
Exit,
|
|
||||||
Render,
|
|
||||||
GetElementById(String),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum CommandResult {
|
|
||||||
Element(*mut Element),
|
|
||||||
None,
|
|
||||||
}
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
/// Implementations
|
/// 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 {
|
impl Document {
|
||||||
pub fn exit(&self) {
|
pub fn exit(&self) {
|
||||||
if !self.running.is_null() {
|
if !self.running.is_null() {
|
||||||
|
|||||||
@@ -1,10 +1,3 @@
|
|||||||
#[macro_export]
|
|
||||||
macro_rules! val {
|
|
||||||
($a:expr) => {
|
|
||||||
$crate::Value::new($a)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! call {
|
macro_rules! call {
|
||||||
($self:ident.$handler:ident ($($inner:tt)*)) => {{
|
($self:ident.$handler:ident ($($inner:tt)*)) => {{
|
||||||
|
|||||||
+25
-19
@@ -29,13 +29,11 @@ clone_trait_object!(StyleCore);
|
|||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum Font {
|
pub enum Font {
|
||||||
None,
|
|
||||||
Bold,
|
Bold,
|
||||||
Underline,
|
Underline,
|
||||||
Italic,
|
Italic,
|
||||||
Reverse,
|
Reverse,
|
||||||
Strike,
|
Strike,
|
||||||
Mul(Vec<Font>),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
@@ -77,7 +75,7 @@ pub enum StyleName {
|
|||||||
pub struct StyleElement {
|
pub struct StyleElement {
|
||||||
pub color: Color,
|
pub color: Color,
|
||||||
pub background: Color,
|
pub background: Color,
|
||||||
pub font: Font,
|
pub font: Vec<Font>,
|
||||||
pub x: Number,
|
pub x: Number,
|
||||||
pub y: Number,
|
pub y: Number,
|
||||||
pub width: Number,
|
pub width: Number,
|
||||||
@@ -138,7 +136,7 @@ impl Default for StyleElement {
|
|||||||
StyleElement {
|
StyleElement {
|
||||||
color: Color::NoColor,
|
color: Color::NoColor,
|
||||||
background: Color::NoColor,
|
background: Color::NoColor,
|
||||||
font: Font::None,
|
font: Vec::new(),
|
||||||
x: Number::Default,
|
x: Number::Default,
|
||||||
y: Number::Default,
|
y: Number::Default,
|
||||||
width: 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) {
|
fn hex_to_rgb(hex: &str) -> (u8, u8, u8) {
|
||||||
let hex = hex.trim_start_matches('#');
|
let hex = hex.trim_start_matches('#');
|
||||||
|
|
||||||
@@ -263,25 +255,39 @@ fn hex_to_rgb(hex: &str) -> (u8, u8, u8) {
|
|||||||
impl StyleCore for Font {
|
impl StyleCore for Font {
|
||||||
fn ansi(&self) -> String {
|
fn ansi(&self) -> String {
|
||||||
String::from(match self {
|
String::from(match self {
|
||||||
Font::None => "",
|
|
||||||
Font::Bold => "\x1b[1m",
|
Font::Bold => "\x1b[1m",
|
||||||
Font::Underline => "\x1b[4m",
|
Font::Underline => "\x1b[4m",
|
||||||
Font::Italic => "\x1b[3m",
|
Font::Italic => "\x1b[3m",
|
||||||
Font::Reverse => "\x1b[7m",
|
Font::Reverse => "\x1b[7m",
|
||||||
Font::Strike => "\x1b[9m",
|
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 {
|
fn ansi_bg(&self) -> String {
|
||||||
self.ansi()
|
self.ansi()
|
||||||
}
|
}
|
||||||
fn is_null(&self) -> bool {
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user