Crates.io | simple_tui |
lib.rs | simple_tui |
version | 0.1.1 |
source | src |
created_at | 2023-12-12 09:25:27.012723 |
updated_at | 2023-12-12 09:49:12.324564 |
description | SimpleTUI is a lightweight and cross-platform Text User Interface (TUI) library |
homepage | |
repository | https://github.com/0xNullByte/simple_tui |
max_upload_size | |
id | 1066199 |
size | 42,646 |
SimpleTUI is a lightweight and cross-platform Text User Interface (TUI) library designed for simplicity and ease of use. Leveraging the powerful crossterm backend for input handling, SimpleTUI ensures a seamless cross-platform experience, making it an ideal choice for developers seeking a straightforward solution for building TUI applications.
OS: Windows 10 Pro
Widget-Centric Design: Build your TUI applications using a widget-centric approach, where every component is a widget. This makes the library intuitive and developer-friendly.
Custom Widget: You can make your custom Widget easily.
Cross-Platform Compatibility: Leveraging the crossterm backend, simple_tui ensures consistent TUI behavior across different platforms, including Windows, macOS, and Linux.
Callback Support with on_click
: Effortlessly handle user interactions with the on_click
callback, enabling developers to create responsive and dynamic UIs.
Super widgets are the widget that implement EventHandler
trait; it will give them special capabilities:
This approche might change in future. But for now this will keep everything simple ;P
Widget | Description | Status |
---|---|---|
vbox (sw) | Vertical layout | Available |
hbox (sw) | Horizontal layout | Available |
label | Text display | Available |
button | Button with on_click |
Available |
input | User text input | Todo |
paragraph | Paragraph with title | Todo |
... more | ... | ... |
(sw) Super Widget
use simple_tui::functions::*;
fn main() {
hbox( // Construct new Horizontal Layout `Super Widget`.
// Macro: Vector of Widgets (implement AsWidget trait)
widgets![
label("مرحباََ") // Construct new lable widget with `ََمرحبا` text.
.align(Alignment::Center) // Center text.
.wrap() // wrap around
]
).start(); // Calling start method to start the app.
}
add your widget into enum Widget
enum Widget{
....
MyWidget(MyWidgetStruct)
}
implement AsWidget
for your widget.
impl AsWidget for MyWidgetStruct {
fn as_widget(self) -> Option<Widget>{
Some(Widget::MyWidget(self))
};
}
Your widget MUST have those methods and fields:
struct CustomWidget{
console: Console,
rect: Rect
}
fn render(&mut self, rect: Rect) {
// draw your Widget using the rect.
// self.console.draw((rect.x, rect.y), "My custom widget!!!")
// ...
// after drawing you need to move rect to self.rect .
// self.rect = rect
}
fn shape(&self) -> Rect { self.rect }
In future:
CustomWidget
trait for your custom widget.on_click
use simple_tui::functions::*;
fn add_one(w: &mut Widget) -> &mut Widget {
if let Widget::Lable(l) = w {
let n = &l.text["Counter: ".len()..].parse::<usize>().unwrap();
l.text = "Counter: ".to_string() + &(n + 1).to_string();
}
w
}
fn main() {
vbox(widgets![
lable("Counter: 0")
.set_id(1337)
.align(Alignment::Center)
.wrap(),
button("Click me!")
.set_rid(1337)
.on_click(|w| add_one(w))
])
.start();
}
use simple_tui::functions::*;
fn main() {
hbox(
widgets![
label("Hello 1").align(Alignment::Left),
label("Hello 2").align(Alignment::Center),
label("Hello 3").align(Alignment::Right),
label("Hello 4").align(Alignment::Left).wrap(),
label("Hello 5").align(Alignment::Center).wrap(),
label("Hello 6").align(Alignment::Right).wrap()
]
)
.start();
}
use simple_tui::functions::*;
fn main() {
vbox(
widgets![
label("Hello 1").align(Alignment::Left),
label("Hello 2").align(Alignment::Center),
label("Hello 3").align(Alignment::Right),
label("Hello 4").align(Alignment::Right).wrap(),
label("Hello 5").align(Alignment::Center).wrap(),
label("Hello 6").align(Alignment::Left).wrap()
]
)
.start();
}
use simple_tui::functions::*;
fn main() {
vbox(widgets![
hbox(widgets![
label("Hello 1").align(Alignment::Right).wrap(),
label("Hello 2").align(Alignment::Right).wrap(),
label("Hello 3").align(Alignment::Left).wrap()
]),
hbox(widgets![
label("Hello 4").align(Alignment::Left).wrap(),
label("Hello 5").align(Alignment::Center).wrap(),
label("Hello 6").align(Alignment::Right).wrap()
]),
hbox(widgets![
label("Hello 7").align(Alignment::Right).wrap(),
label("Hello 8").align(Alignment::Left).wrap(),
label("Hello 9").align(Alignment::Left).wrap()
])
])
.start();
}