hello world example

This commit is contained in:
2024-09-17 20:30:02 +02:00
parent 9af6a8e6c5
commit 4e135f92e4
4 changed files with 79 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
[package]
name = "osui"
version = "0.0.1"
edition = "2021"
[lib]
name = "osui"
path = "src/lib.rs"
[[bin]]
name = "osui"
path = "src/main.rs"
[dependencies]
+53
View File
@@ -0,0 +1,53 @@
use std::io::{self, Write};
pub mod ui;
pub struct Component<T> {
pub component: T,
pub x: usize,
pub y: usize,
cleared_frame: String,
pub clear_trace: bool,
}
impl<T: std::fmt::Display> Component<T> {
pub fn new(component: T) -> Component<T> {
Component {
component,
x: 0,
y: 0,
cleared_frame: String::new(),
clear_trace: false,
}
}
pub fn render(&mut self) {
if self.clear_trace {
self.clear();
}
self.cleared_frame.clear();
for (i, d) in format!("{}", self.component).split("\n").enumerate() {
print!("\x1B[{};{}H{}", self.y + (i + 1), self.x, d);
self.cleared_frame += format!(
"\x1B[{};{}H{}",
self.y + (i + 1),
self.x,
" ".repeat(d.len())
)
.as_str();
}
flush();
}
pub fn clear(&self) {
print!("{}", self.cleared_frame);
flush();
}
}
pub fn clear() {
print!("\x1B[2J\x1B[1;1H");
flush();
}
pub fn flush() {
io::stdout().flush().unwrap();
}
+7
View File
@@ -0,0 +1,7 @@
use osui::{self, ui};
fn main() {
osui::clear();
let mut txt = ui::text("hello, world!");
txt.render();
}
+5
View File
@@ -0,0 +1,5 @@
use crate::Component;
pub fn text(text: &str) -> Component<String> {
Component::new(text.to_string())
}