diff --git a/src/lib.rs b/src/lib.rs index f0d78d3..4d40d34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,18 +2,23 @@ use std::io::{self, Write}; pub mod ui; +#[derive(Clone)] pub struct ComponentWrapper { pub component: T, pub x: usize, pub y: usize, cleared_frame: String, - pub clear_trace: bool, } pub trait Component { fn render(&mut self) -> String { String::new() } + fn run(&mut self, x: usize, y: usize) -> String { + let _ = x; + let _ = y; + String::new() + } } impl ComponentWrapper { @@ -23,13 +28,10 @@ impl ComponentWrapper { x: 0, y: 0, cleared_frame: String::new(), - clear_trace: false, } } pub fn render(&mut self) { - if self.clear_trace { - self.clear(); - } + self.clean(); self.cleared_frame.clear(); for (i, d) in format!("{}", self.component.render()) .split("\n") @@ -46,14 +48,17 @@ impl ComponentWrapper { } flush(); } - pub fn clear(&self) { + pub fn clean(&self) { print!("{}", self.cleared_frame); flush(); } + pub fn run(&mut self) -> String { + self.component.run(self.x, self.y) + } } pub fn clear() { - print!("\x1B[2J\x1B[1;1H"); + print!("\x1b[H\x1b[2J\x1b[3J"); flush(); } diff --git a/src/ui.rs b/src/ui.rs index c1b1109..234da0c 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -25,10 +25,8 @@ impl Component for Input { self.max_length ) } -} - -impl Input { - pub fn get_input(&mut self) -> String { + fn run(&mut self, x: usize, y: usize) -> String { + print!("\x1B[{};{}H", y + 2, x + 2); let mut input = String::new(); loop { let key = Keyboard::new().read_key(); @@ -46,8 +44,10 @@ impl Input { print!(" "); } Keys::Delete => { - input.pop(); - print!("\x1b[1D \x1b[1D"); + if input.len() > 0 { + input.pop(); + print!("\x1b[1D \x1b[1D"); + } } _ => {} }