From 25f9abeb4c69024f8d9dceb472ba33c03e00daa0 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 28 Oct 2026 22:36:07 +0100 Subject: v0.2.5: fix terminal width, ping timing and output formatting --- src/bin/client/connection.rs | 14 ++++-- src/bin/client/repl.rs | 107 ++++++++++++++++++++++++++++++------------- 2 files changed, 84 insertions(+), 37 deletions(-) (limited to 'src/bin') diff --git a/src/bin/client/connection.rs b/src/bin/client/connection.rs index e54b646..93a7829 100644 --- a/src/bin/client/connection.rs +++ b/src/bin/client/connection.rs @@ -1,4 +1,5 @@ use zagan_bet::client::database; +use zagan_bet::client::ui::first_contact_lines; use crate::tls::TlsConnector; use rusqlite::Connection; use std::io::{Read, Write}; @@ -57,10 +58,9 @@ pub fn connect( if let Some(fp) = observed { if database::get_fingerprint(conn, addr).is_none() { database::save_fingerprint(conn, addr, &fp)?; - println!( - "First contact with {addr}: trusting fingerprint {fp}\n\ - Future connections must present the same certificate key." - ); + for line in first_contact_lines(addr, &fp) { + println!("{}", line); + } } } } @@ -72,7 +72,11 @@ pub fn connect( let mut response = String::new(); tls.read_line(&mut response)?; let elapsed = start.elapsed(); - println!("\n\rConnected to {} with ping delay: {:?} (TLS)", addr, elapsed); + println!( + "\n\rConnected to {} with ping delay: {:.1}ms (TLS)", + addr, + elapsed.as_secs_f64() * 1000.0 + ); if let Some((username, password)) = database::get_credentials(conn, addr) { let auth_msg = format!("AUTH {} {}", username, password); diff --git a/src/bin/client/repl.rs b/src/bin/client/repl.rs index 3fa49b8..76b149b 100644 --- a/src/bin/client/repl.rs +++ b/src/bin/client/repl.rs @@ -1,11 +1,13 @@ use rusqlite::Connection; use std::io::{stdout, Write}; use std::sync::{Arc, Mutex}; +use std::time::Instant; use crossterm::{ cursor::{MoveToColumn, MoveUp, Show}, event::{read, Event, KeyCode, KeyEventKind, KeyModifiers}, execute, + style::Stylize, terminal::{disable_raw_mode, enable_raw_mode, Clear, ClearType}, }; @@ -13,7 +15,9 @@ use sha2::{Digest, Sha256}; use zagan_bet::client::core::{ClientCore, Effect, InteractiveCommand, Key}; use zagan_bet::client::database; -use zagan_bet::client::ui::{input_cursor_column, lines_to_move_up}; +use zagan_bet::client::ui::{ + display_width, format_pong, host_lines, input_cursor_column, lines_to_move_up, +}; use crate::connection::TlsStream; @@ -96,18 +100,22 @@ impl ClientState { loop { let event = read().unwrap(); - if let Event::Key(key) = event { - if key.kind != KeyEventKind::Press { - continue; - } - let Some(key) = map_key(key) else { - continue; - }; - let effects = self.core.handle_key(key); - if self.apply_effects(effects) { - break; + match event { + Event::Key(key) => { + if key.kind != KeyEventKind::Press { + continue; + } + let Some(key) = map_key(key) else { + continue; + }; + let effects = self.core.handle_key(key); + if self.apply_effects(effects) { + break; + } + self.apply_prompt(); } - self.apply_prompt(); + Event::Resize(_, _) => self.apply_prompt(), + _ => {} } } @@ -120,15 +128,45 @@ impl ClientState { Effect::Print(lines) => { self.commit_prompt_line(); for line in lines { - println!("{}", line); + let rendered = if line.starts_with("ERROR") + || line.contains("failed") + { + line.as_str().red().to_string() + } else if line.starts_with("Logged in") + || line.starts_with("Successfully") + { + line.as_str().green().to_string() + } else if line.starts_with("Available commands:") + || line.starts_with("Available games:") + || line.starts_with("Remembered hosts:") + { + line.as_str().bold().cyan().to_string() + } else { + line + }; + println!("{}", rendered); } } Effect::ReplaceRegion(lines) => self.draw_region(&lines), Effect::ClearRegion => self.clear_region(), Effect::SetPrompt { text, cursor_col } => self.draw_prompt(&text, cursor_col), Effect::SendLine(line) => { + let is_ping = line == "PING"; + let started = Instant::now(); let reply = self.send_and_read(&line); - let more = self.core.handle_reply(&reply); + let elapsed = started.elapsed(); + let mut more = self.core.handle_reply(&reply); + if is_ping && reply.trim() == "PONG" { + for effect in &mut more { + if let Effect::Print(lines) = effect { + for text in lines { + if text == "Pong" { + *text = format_pong(elapsed); + } + } + } + } + } if self.stream.is_none() { self.core.set_connected(false); } @@ -196,7 +234,21 @@ impl ClientState { execute!(stdout(), MoveUp(lines_to_move_up(self.game_lines))).unwrap(); } execute!(stdout(), MoveToColumn(0), Clear(ClearType::FromCursorDown)).unwrap(); - print!("{}", lines.join("\r\n")); + let rendered: Vec = lines + .iter() + .map(|line| { + if line.starts_with("===") { + line.as_str().bold().cyan().to_string() + } else if line.contains("JACKPOT") || line.contains("Two of a kind") { + line.as_str().bold().green().to_string() + } else if line.starts_with("ERROR:") { + line.as_str().red().to_string() + } else { + line.clone() + } + }) + .collect(); + print!("{}", rendered.join("\r\n")); stdout().flush().unwrap(); self.game_lines = lines.len().max(1); } @@ -217,7 +269,8 @@ impl ClientState { fn draw_prompt(&mut self, text: &str, cursor_col: u16) { execute!(stdout(), MoveToColumn(0), Clear(ClearType::CurrentLine)).unwrap(); - print!("> {}", text); + print!("{}", "> ".cyan()); + print!("{}", text); stdout().flush().unwrap(); execute!(stdout(), MoveToColumn(cursor_col)).unwrap(); self.prompt_text = text.to_string(); @@ -376,16 +429,8 @@ impl ClientState { fn list_hosts(&mut self) { let hosts = database::get_all_hosts(&self.conn); - if hosts.is_empty() { - println!("No remembered hosts."); - return; - } - println!("Remembered hosts:"); - for (host, fingerprint) in hosts { - match fingerprint { - Some(fingerprint) => println!(" {} - {}", host, fingerprint), - None => println!(" {} - (no fingerprint)", host), - } + for line in host_lines(&hosts) { + println!("{}", line); } } } @@ -425,7 +470,6 @@ fn read_line(prompt: &str, cleanup_done: &Arc>) -> String { stdout().flush().unwrap(); let mut input = String::new(); - let prompt_len = prompt.len(); loop { match read().unwrap() { @@ -442,8 +486,8 @@ fn read_line(prompt: &str, cleanup_done: &Arc>) -> String { execute!( stdout(), MoveToColumn(input_cursor_column( - prompt_len, - input.len() + display_width(prompt), + display_width(&input) )), Clear(ClearType::UntilNewLine) ) @@ -486,7 +530,6 @@ fn read_password(prompt: &str, cleanup_done: &Arc>) -> String { stdout().flush().unwrap(); let mut input = String::new(); - let prompt_len = prompt.len(); loop { match read().unwrap() { @@ -503,8 +546,8 @@ fn read_password(prompt: &str, cleanup_done: &Arc>) -> String { execute!( stdout(), MoveToColumn(input_cursor_column( - prompt_len, - input.len() + display_width(prompt), + display_width(&input) )), Clear(ClearType::UntilNewLine) ) -- cgit v1.2.3