diff options
| author | salmonik <raisersoft@proton.me> | 2026-03-11 21:04:08 +0100 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-03-11 21:04:08 +0100 |
| commit | ef570e6ba8c3d6f5e90a07a26ef7385b0278198a (patch) | |
| tree | cfc6b835d0b05ccbcd8fa06b28072bd2315d2af3 /src/bin | |
| parent | only use raw mode (diff) | |
| download | zagan-bet-ef570e6ba8c3d6f5e90a07a26ef7385b0278198a.tar.gz zagan-bet-ef570e6ba8c3d6f5e90a07a26ef7385b0278198a.zip | |
stable and working, both platforms
Diffstat (limited to 'src/bin')
| -rw-r--r-- | src/bin/client/macros.rs | 9 | ||||
| -rw-r--r-- | src/bin/client/main.rs | 4 | ||||
| -rw-r--r-- | src/bin/client/repl.rs | 44 |
3 files changed, 42 insertions, 15 deletions
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 { | |||
| 6 | print!("\r{}\n", format_args!($($arg)*)) | 6 | print!("\r{}\n", format_args!($($arg)*)) |
| 7 | }; | 7 | }; |
| 8 | } | 8 | } |
| 9 | |||
| 10 | macro_rules! printsame { | ||
| 11 | () => { | ||
| 12 | print!("\r") | ||
| 13 | }; | ||
| 14 | ($($arg:tt)*) => { | ||
| 15 | print!("\r{}", format_args!($($arg)*)) | ||
| 16 | }; | ||
| 17 | } \ 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() { | |||
| 11 | if let Some(host) = state.addr.clone() { | 11 | if let Some(host) = state.addr.clone() { |
| 12 | state.connect_to(&host); | 12 | state.connect_to(&host); |
| 13 | } | 13 | } |
| 14 | |||
| 15 | println!("Welcome to Zagan-Bet Client!"); | ||
| 16 | println!("Type 'help' for available commands."); | ||
| 17 | |||
| 14 | state.run(); | 18 | state.run(); |
| 15 | } | 19 | } |
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 { | |||
| 59 | self.history_index = self.history.len(); | 59 | self.history_index = self.history.len(); |
| 60 | redraw(""); | 60 | redraw(""); |
| 61 | loop { | 61 | loop { |
| 62 | match read().unwrap() { | 62 | let event = read().unwrap(); |
| 63 | Event::Key(key) => match key.code { | 63 | match event { |
| 64 | Event::Key(key) => { | ||
| 65 | // Only handle key press events, not release events | ||
| 66 | if key.kind == crossterm::event::KeyEventKind::Press { | ||
| 67 | match key.code { | ||
| 64 | KeyCode::Up => { | 68 | KeyCode::Up => { |
| 65 | if self.history_index > 0 { | 69 | if self.history_index > 0 { |
| 66 | self.history_index -= 1; | 70 | self.history_index -= 1; |
| @@ -331,26 +335,31 @@ impl ClientState { | |||
| 331 | KeyCode::Backspace => { | 335 | KeyCode::Backspace => { |
| 332 | if !current_input.is_empty() { | 336 | if !current_input.is_empty() { |
| 333 | current_input.pop(); | 337 | current_input.pop(); |
| 334 | redraw(¤t_input); | 338 | execute!(stdout(), MoveToColumn(2), |
| 339 | Clear(ClearType::UntilNewLine)).unwrap(); | ||
| 340 | print!("{}", current_input); | ||
| 341 | stdout().flush().unwrap(); | ||
| 335 | } | 342 | } |
| 336 | } | 343 | } |
| 337 | KeyCode::Char(c) => { | 344 | KeyCode::Char(c) => { |
| 338 | if key.modifiers.contains(KeyModifiers::CONTROL) && c == 'c' { | 345 | if key.modifiers.contains(KeyModifiers::CONTROL) && c == 'c' { |
| 339 | disable_raw_mode().unwrap(); | ||
| 340 | execute!(stdout(), Show).unwrap(); | 346 | execute!(stdout(), Show).unwrap(); |
| 341 | return; | 347 | return; |
| 342 | } else if c == '\t' { | 348 | } else if c == '\t' { |
| 343 | self.handle_tab_completion(&mut current_input); | 349 | self.handle_tab_completion(&mut current_input); |
| 344 | } else { | 350 | } else { |
| 345 | current_input.push(c); | 351 | current_input.push(c); |
| 346 | redraw(¤t_input); | 352 | print!("{}", c); |
| 353 | stdout().flush().unwrap(); | ||
| 347 | } | 354 | } |
| 348 | } | 355 | } |
| 349 | KeyCode::Tab => { | 356 | KeyCode::Tab => { |
| 350 | self.handle_tab_completion(&mut current_input); | 357 | self.handle_tab_completion(&mut current_input); |
| 351 | } | 358 | } |
| 352 | _ => {} | 359 | _ => {} |
| 360 | } | ||
| 353 | } | 361 | } |
| 362 | } | ||
| 354 | _ => {} | 363 | _ => {} |
| 355 | } | 364 | } |
| 356 | } | 365 | } |
| @@ -408,7 +417,7 @@ fn help() { | |||
| 408 | logout - logout from the server\r | 417 | logout - logout from the server\r |
| 409 | info - show login info\r | 418 | info - show login info\r |
| 410 | help - show this message\r | 419 | help - show this message\r |
| 411 | quit - quit the program\n"); | 420 | quit - quit the program"); |
| 412 | } | 421 | } |
| 413 | 422 | ||
| 414 | fn redraw(current_input: &str) { | 423 | fn redraw(current_input: &str) { |
| @@ -420,7 +429,7 @@ fn redraw(current_input: &str) { | |||
| 420 | 429 | ||
| 421 | fn read_line(prompt: &str) -> String { | 430 | fn read_line(prompt: &str) -> String { |
| 422 | execute!(stdout(), Show).unwrap(); | 431 | execute!(stdout(), Show).unwrap(); |
| 423 | print!("{}", prompt); | 432 | printsame!("{}", prompt); |
| 424 | stdout().flush().unwrap(); | 433 | stdout().flush().unwrap(); |
| 425 | 434 | ||
| 426 | let mut input = String::new(); | 435 | let mut input = String::new(); |
| @@ -428,7 +437,9 @@ fn read_line(prompt: &str) -> String { | |||
| 428 | 437 | ||
| 429 | loop { | 438 | loop { |
| 430 | match read().unwrap() { | 439 | match read().unwrap() { |
| 431 | Event::Key(key) => match key.code { | 440 | Event::Key(key) => { |
| 441 | if key.kind == crossterm::event::KeyEventKind::Press { | ||
| 442 | match key.code { | ||
| 432 | KeyCode::Enter => { | 443 | KeyCode::Enter => { |
| 433 | println!(); | 444 | println!(); |
| 434 | break; | 445 | break; |
| @@ -448,7 +459,9 @@ fn read_line(prompt: &str) -> String { | |||
| 448 | stdout().flush().unwrap(); | 459 | stdout().flush().unwrap(); |
| 449 | } | 460 | } |
| 450 | _ => {} | 461 | _ => {} |
| 451 | }, | 462 | } |
| 463 | } | ||
| 464 | } | ||
| 452 | _ => {} | 465 | _ => {} |
| 453 | } | 466 | } |
| 454 | } | 467 | } |
| @@ -461,12 +474,13 @@ fn read_password(prompt: &str) -> String { | |||
| 461 | stdout().flush().unwrap(); | 474 | stdout().flush().unwrap(); |
| 462 | 475 | ||
| 463 | let mut input = String::new(); | 476 | let mut input = String::new(); |
| 464 | let mut display = String::new(); | ||
| 465 | let prompt_len = prompt.len(); | 477 | let prompt_len = prompt.len(); |
| 466 | 478 | ||
| 467 | loop { | 479 | loop { |
| 468 | match read().unwrap() { | 480 | match read().unwrap() { |
| 469 | Event::Key(key) => match key.code { | 481 | Event::Key(key) => { |
| 482 | if key.kind == crossterm::event::KeyEventKind::Press { | ||
| 483 | match key.code { | ||
| 470 | KeyCode::Enter => { | 484 | KeyCode::Enter => { |
| 471 | println!(); | 485 | println!(); |
| 472 | break; | 486 | break; |
| @@ -474,21 +488,21 @@ fn read_password(prompt: &str) -> String { | |||
| 474 | KeyCode::Backspace => { | 488 | KeyCode::Backspace => { |
| 475 | if !input.is_empty() { | 489 | if !input.is_empty() { |
| 476 | input.pop(); | 490 | input.pop(); |
| 477 | display.pop(); | ||
| 478 | // Move cursor back and clear from current position to end of line | 491 | // Move cursor back and clear from current position to end of line |
| 479 | execute!(stdout(), MoveToColumn((prompt_len + display.len() + 1) as u16), | 492 | execute!(stdout(), MoveToColumn((prompt_len + input.len() + 1) as u16), |
| 480 | Clear(ClearType::UntilNewLine)).unwrap(); | 493 | Clear(ClearType::UntilNewLine)).unwrap(); |
| 481 | stdout().flush().unwrap(); | 494 | stdout().flush().unwrap(); |
| 482 | } | 495 | } |
| 483 | } | 496 | } |
| 484 | KeyCode::Char(c) => { | 497 | KeyCode::Char(c) => { |
| 485 | input.push(c); | 498 | input.push(c); |
| 486 | display.push('*'); | ||
| 487 | print!("*"); | 499 | print!("*"); |
| 488 | stdout().flush().unwrap(); | 500 | stdout().flush().unwrap(); |
| 489 | } | 501 | } |
| 490 | _ => {} | 502 | _ => {} |
| 491 | }, | 503 | } |
| 504 | } | ||
| 505 | } | ||
| 492 | _ => {} | 506 | _ => {} |
| 493 | } | 507 | } |
| 494 | } | 508 | } |
