From 4e135f92e43b793363980d183fe63946ab8da877 Mon Sep 17 00:00:00 2001 From: Leo Date: Tue, 17 Sep 2024 20:30:02 +0200 Subject: [PATCH] hello world example --- Cargo.toml | 14 ++++++++++++++ src/lib.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 7 +++++++ src/ui.rs | 5 +++++ 4 files changed, 79 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/lib.rs create mode 100644 src/main.rs create mode 100644 src/ui.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..163578e --- /dev/null +++ b/Cargo.toml @@ -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] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..0505664 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,53 @@ +use std::io::{self, Write}; + +pub mod ui; + +pub struct Component { + pub component: T, + pub x: usize, + pub y: usize, + cleared_frame: String, + pub clear_trace: bool, +} + +impl Component { + pub fn new(component: T) -> Component { + 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(); +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..7bbfd40 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,7 @@ +use osui::{self, ui}; + +fn main() { + osui::clear(); + let mut txt = ui::text("hello, world!"); + txt.render(); +} diff --git a/src/ui.rs b/src/ui.rs new file mode 100644 index 0000000..a3cabc6 --- /dev/null +++ b/src/ui.rs @@ -0,0 +1,5 @@ +use crate::Component; + +pub fn text(text: &str) -> Component { + Component::new(text.to_string()) +}