updater function

This commit is contained in:
2024-09-21 22:42:05 +02:00
parent 81c9a677e3
commit bc9c9d9bdc
3 changed files with 38 additions and 18 deletions
+5
View File
@@ -1,3 +1,8 @@
pub const ENTER: &str = "\r"; pub const ENTER: &str = "\r";
pub const TAB: &str = "\t"; pub const TAB: &str = "\t";
pub const BACKSPACE: &str = "\u{7f}"; pub const BACKSPACE: &str = "\u{7f}";
pub const ESC: &str = "\u{1b}";
pub const UP: &str = "\u{1b}[A";
pub const DOWN: &str = "\u{1b}[B";
pub const RIGHT: &str = "\u{1b}[C";
pub const LEFT: &str = "\u{1b}[D";
+13 -6
View File
@@ -6,20 +6,23 @@ use std::{
pub mod key; pub mod key;
pub mod ui; pub mod ui;
#[derive(Clone)] #[derive(Clone, Copy)]
pub struct ComponentWrapper<T> { pub struct ComponentWrapper<T> {
pub component: T, pub contents: T,
pub x: usize, pub x: usize,
pub y: usize, pub y: usize,
update_fn: fn(&mut T) -> bool,
} }
pub trait Component { pub trait Component {
fn render(&self) -> String { fn render(&self) -> String {
String::new() String::new()
} }
fn run(&mut self, x: usize, y: usize) -> String {
fn run(&mut self, x: usize, y: usize, update_fn: fn(&mut Self) -> bool) -> String {
let _ = x; let _ = x;
let _ = y; let _ = y;
let _ = update_fn;
String::new() String::new()
} }
} }
@@ -27,19 +30,23 @@ pub trait Component {
impl<T: Component> ComponentWrapper<T> { impl<T: Component> ComponentWrapper<T> {
pub fn new(component: T) -> ComponentWrapper<T> { pub fn new(component: T) -> ComponentWrapper<T> {
ComponentWrapper { ComponentWrapper {
component, contents: component,
x: 0, x: 0,
y: 0, y: 0,
update_fn: (|_| false),
} }
} }
pub fn run(&mut self) -> String { pub fn run(&mut self) -> String {
self.component.run(self.x, self.y) self.contents.run(self.x, self.y, self.update_fn)
} }
pub fn render(&self, frame: &mut Vec<String>) { pub fn render(&self, frame: &mut Vec<String>) {
for (i, line) in self.component.render().split("\n").enumerate() { for (i, line) in self.contents.render().split("\n").enumerate() {
frame[self.y + i] = _render_line(frame[self.y + i].clone(), line.to_string(), self.x); frame[self.y + i] = _render_line(frame[self.y + i].clone(), line.to_string(), self.x);
} }
} }
pub fn update(&mut self, update_fn: fn(&mut T) -> bool) {
self.update_fn = update_fn;
}
} }
pub fn clear() { pub fn clear() {
+20 -12
View File
@@ -12,6 +12,8 @@ pub fn text(text: &str) -> ComponentWrapper<String> {
#[derive(Clone)] #[derive(Clone)]
pub struct Input { pub struct Input {
pub max_length: usize, pub max_length: usize,
pub key: String,
pub data: String,
} }
impl Component for Input { impl Component for Input {
@@ -23,35 +25,41 @@ impl Component for Input {
"‾".repeat(self.max_length), "‾".repeat(self.max_length),
) )
} }
fn run(&mut self, x: usize, y: usize) -> String { fn run(&mut self, x: usize, y: usize, update_fn: fn(&mut Self) -> bool) -> String {
print!("\x1B[{};{}H", y + 2, x + 2); print!("\x1B[{};{}H", y + 2, x + 2);
flush(); flush();
let mut input = String::new();
loop { loop {
match read_key().as_str() { self.key = read_key();
key::ENTER => { if update_fn(self) {
print!("\n\n"); break;
return input; }
} match self.key.as_str() {
key::ENTER => break,
key::BACKSPACE => { key::BACKSPACE => {
if input.len() > 0 { if self.data.len() > 0 {
input.pop(); self.data.pop();
print!("\x1b[1D \x1b[1D"); print!("\x1b[1D \x1b[1D");
flush(); flush();
} }
} }
c => { c => {
if input.len() < self.max_length { if self.data.len() < self.max_length {
input += c.to_string().as_str(); self.data += c.to_string().as_str();
print!("{c}"); print!("{c}");
flush(); flush();
} }
} }
} }
} }
print!("\n\n");
return self.data.clone();
} }
} }
pub fn input_box(max_length: usize) -> ComponentWrapper<Input> { pub fn input_box(max_length: usize) -> ComponentWrapper<Input> {
ComponentWrapper::new(Input { max_length }) ComponentWrapper::new(Input {
max_length,
key: String::new(),
data: String::new(),
})
} }