From ef570e6ba8c3d6f5e90a07a26ef7385b0278198a Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 11 Mar 2026 21:04:08 +0100 Subject: stable and working, both platforms --- .gitignore | 3 ++- src/bin/client/macros.rs | 9 +++++++++ src/bin/client/main.rs | 4 ++++ src/bin/client/repl.rs | 44 +++++++++++++++++++++++++++++--------------- 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index a678faf..3f32615 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target *db -*.lock \ No newline at end of file +*.lock +/text \ No newline at end of file diff --git a/src/bin/client/macros.rs b/src/bin/client/macros.rs index f97a4ff..e45f2b3 100644 --- a/src/bin/client/macros.rs +++ b/src/bin/client/macros.rs @@ -6,3 +6,12 @@ macro_rules! println { print!("\r{}\n", format_args!($($arg)*)) }; } + +macro_rules! printsame { + () => { + print!("\r") + }; + ($($arg:tt)*) => { + print!("\r{}", format_args!($($arg)*)) + }; +} \ No newline at end of file diff --git a/src/bin/client/main.rs b/src/bin/client/main.rs index 240ad66..2ff6f55 100644 --- a/src/bin/client/main.rs +++ b/src/bin/client/main.rs @@ -11,5 +11,9 @@ fn main() { if let Some(host) = state.addr.clone() { state.connect_to(&host); } + + println!("Welcome to Zagan-Bet Client!"); + println!("Type 'help' for available commands."); + state.run(); } diff --git a/src/bin/client/repl.rs b/src/bin/client/repl.rs index 4e787cb..a0e5b5e 100644 --- a/src/bin/client/repl.rs +++ b/src/bin/client/repl.rs @@ -59,8 +59,12 @@ impl ClientState { self.history_index = self.history.len(); redraw(""); loop { - match read().unwrap() { - Event::Key(key) => match key.code { + let event = read().unwrap(); + match event { + Event::Key(key) => { + // Only handle key press events, not release events + if key.kind == crossterm::event::KeyEventKind::Press { + match key.code { KeyCode::Up => { if self.history_index > 0 { self.history_index -= 1; @@ -331,26 +335,31 @@ impl ClientState { KeyCode::Backspace => { if !current_input.is_empty() { current_input.pop(); - redraw(¤t_input); + execute!(stdout(), MoveToColumn(2), + Clear(ClearType::UntilNewLine)).unwrap(); + print!("{}", current_input); + stdout().flush().unwrap(); } } KeyCode::Char(c) => { if key.modifiers.contains(KeyModifiers::CONTROL) && c == 'c' { - disable_raw_mode().unwrap(); execute!(stdout(), Show).unwrap(); return; } else if c == '\t' { self.handle_tab_completion(&mut current_input); } else { current_input.push(c); - redraw(¤t_input); + print!("{}", c); + stdout().flush().unwrap(); } } KeyCode::Tab => { self.handle_tab_completion(&mut current_input); } _ => {} + } } + } _ => {} } } @@ -408,7 +417,7 @@ fn help() { logout - logout from the server\r info - show login info\r help - show this message\r - quit - quit the program\n"); + quit - quit the program"); } fn redraw(current_input: &str) { @@ -420,7 +429,7 @@ fn redraw(current_input: &str) { fn read_line(prompt: &str) -> String { execute!(stdout(), Show).unwrap(); - print!("{}", prompt); + printsame!("{}", prompt); stdout().flush().unwrap(); let mut input = String::new(); @@ -428,7 +437,9 @@ fn read_line(prompt: &str) -> String { loop { match read().unwrap() { - Event::Key(key) => match key.code { + Event::Key(key) => { + if key.kind == crossterm::event::KeyEventKind::Press { + match key.code { KeyCode::Enter => { println!(); break; @@ -448,7 +459,9 @@ fn read_line(prompt: &str) -> String { stdout().flush().unwrap(); } _ => {} - }, + } + } + } _ => {} } } @@ -461,12 +474,13 @@ fn read_password(prompt: &str) -> String { stdout().flush().unwrap(); let mut input = String::new(); - let mut display = String::new(); let prompt_len = prompt.len(); loop { match read().unwrap() { - Event::Key(key) => match key.code { + Event::Key(key) => { + if key.kind == crossterm::event::KeyEventKind::Press { + match key.code { KeyCode::Enter => { println!(); break; @@ -474,21 +488,21 @@ fn read_password(prompt: &str) -> String { KeyCode::Backspace => { if !input.is_empty() { input.pop(); - display.pop(); // Move cursor back and clear from current position to end of line - execute!(stdout(), MoveToColumn((prompt_len + display.len() + 1) as u16), + execute!(stdout(), MoveToColumn((prompt_len + input.len() + 1) as u16), Clear(ClearType::UntilNewLine)).unwrap(); stdout().flush().unwrap(); } } KeyCode::Char(c) => { input.push(c); - display.push('*'); print!("*"); stdout().flush().unwrap(); } _ => {} - }, + } + } + } _ => {} } } -- cgit v1.2.3