improvements

This commit is contained in:
2024-09-21 17:17:36 +02:00
parent b644df02e0
commit f47f3575de
2 changed files with 18 additions and 13 deletions
+12 -7
View File
@@ -2,18 +2,23 @@ use std::io::{self, Write};
pub mod ui; pub mod ui;
#[derive(Clone)]
pub struct ComponentWrapper<T> { pub struct ComponentWrapper<T> {
pub component: T, pub component: T,
pub x: usize, pub x: usize,
pub y: usize, pub y: usize,
cleared_frame: String, cleared_frame: String,
pub clear_trace: bool,
} }
pub trait Component { pub trait Component {
fn render(&mut self) -> String { fn render(&mut self) -> String {
String::new() String::new()
} }
fn run(&mut self, x: usize, y: usize) -> String {
let _ = x;
let _ = y;
String::new()
}
} }
impl<T: Component> ComponentWrapper<T> { impl<T: Component> ComponentWrapper<T> {
@@ -23,13 +28,10 @@ impl<T: Component> ComponentWrapper<T> {
x: 0, x: 0,
y: 0, y: 0,
cleared_frame: String::new(), cleared_frame: String::new(),
clear_trace: false,
} }
} }
pub fn render(&mut self) { pub fn render(&mut self) {
if self.clear_trace { self.clean();
self.clear();
}
self.cleared_frame.clear(); self.cleared_frame.clear();
for (i, d) in format!("{}", self.component.render()) for (i, d) in format!("{}", self.component.render())
.split("\n") .split("\n")
@@ -46,14 +48,17 @@ impl<T: Component> ComponentWrapper<T> {
} }
flush(); flush();
} }
pub fn clear(&self) { pub fn clean(&self) {
print!("{}", self.cleared_frame); print!("{}", self.cleared_frame);
flush(); flush();
} }
pub fn run(&mut self) -> String {
self.component.run(self.x, self.y)
}
} }
pub fn clear() { pub fn clear() {
print!("\x1B[2J\x1B[1;1H"); print!("\x1b[H\x1b[2J\x1b[3J");
flush(); flush();
} }
+4 -4
View File
@@ -25,10 +25,8 @@ impl Component for Input {
self.max_length self.max_length
) )
} }
} fn run(&mut self, x: usize, y: usize) -> String {
print!("\x1B[{};{}H", y + 2, x + 2);
impl Input {
pub fn get_input(&mut self) -> String {
let mut input = String::new(); let mut input = String::new();
loop { loop {
let key = Keyboard::new().read_key(); let key = Keyboard::new().read_key();
@@ -46,9 +44,11 @@ impl Input {
print!(" "); print!(" ");
} }
Keys::Delete => { Keys::Delete => {
if input.len() > 0 {
input.pop(); input.pop();
print!("\x1b[1D \x1b[1D"); print!("\x1b[1D \x1b[1D");
} }
}
_ => {} _ => {}
} }
} }