From f266172cdf1d0db64e2cdf8e5f09017041eecd19 Mon Sep 17 00:00:00 2001 From: MeexReay Date: Tue, 11 Feb 2025 21:34:58 +0300 Subject: [PATCH] fix history --- Cargo.lock | 2 +- Cargo.toml | 2 +- flake.nix | 4 +--- src/chat.rs | 32 +++++++++++++++++--------------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 918bb36..9af8587 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -75,7 +75,7 @@ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "bRAC" -version = "0.1.1+1.99.2" +version = "0.1.1+2.0" dependencies = [ "clap", "colored", diff --git a/Cargo.toml b/Cargo.toml index 3cdd80c..1af2a52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bRAC" -version = "0.1.1+1.99.2" +version = "0.1.1+2.0" edition = "2021" [dependencies] diff --git a/flake.nix b/flake.nix index 5fe00ff..72a7309 100644 --- a/flake.nix +++ b/flake.nix @@ -1,6 +1,4 @@ { - description = "bRAC"; - inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11"; rust-overlay.url = "github:oxalica/rust-overlay"; @@ -24,7 +22,7 @@ packages.default = pkgs.rustPlatform.buildRustPackage { pname = "bRAC"; - version = "0.1.2+1.99.2"; + version = "0.1.2+2.0"; src = pkgs.lib.cleanSource ./.; cargoLock = { diff --git a/src/chat.rs b/src/chat.rs index d47b389..f42affc 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -178,11 +178,7 @@ fn find_username_color(message: &str) -> Option<(String, String, Color)> { } -fn write_backspace(len: usize) { - write_backspace_with_text(len, "") -} - -fn write_backspace_with_text(len: usize, text: &str) { +fn replace_input(cursor: usize, len: usize, text: &str) { let spaces = if text.chars().count() < len { len-text.chars().count() } else { @@ -190,7 +186,7 @@ fn write_backspace_with_text(len: usize, text: &str) { }; write!(stdout(), "{}{}{}{}", - MoveLeft(1).to_string().repeat(len), + MoveLeft(1).to_string().repeat(cursor), text, " ".repeat(spaces), MoveLeft(1).to_string().repeat(spaces) @@ -222,11 +218,13 @@ fn poll_events(ctx: Arc) -> Result<(), Box> { let message = input.read().unwrap().clone(); if !message.is_empty() { - write_backspace(message.chars().count()); + replace_input(cursor, message.chars().count(), ""); input.write().unwrap().clear(); - history.insert(history_cursor, message.clone()); - history_cursor += 1; + cursor = 0; + + history_cursor = history.len()-1; + history.push(String::new()); if message.starts_with("/") && !ctx.disable_commands { on_command(ctx.clone(), &message)?; @@ -245,9 +243,11 @@ fn poll_events(ctx: Arc) -> Result<(), Box> { } } KeyCode::Backspace => { + let len = input.read().unwrap().chars().count(); if input.write().unwrap().pop().is_some() { history[history_cursor].pop(); - write_backspace(1); + replace_input(cursor, len, &history[history_cursor]); + cursor -= 1; } } KeyCode::Esc => { @@ -256,13 +256,14 @@ fn poll_events(ctx: Arc) -> Result<(), Box> { } KeyCode::Up | KeyCode::Down => { history_cursor = if event.code == KeyCode::Up { - max(history_cursor + 1, 1) - 1 + max(history_cursor, 1) - 1 } else { min(history_cursor + 1, history.len() - 1) }; - let was_len = input.read().unwrap().chars().count(); + let len = input.read().unwrap().chars().count(); *input.write().unwrap() = history[history_cursor].clone(); - write_backspace_with_text(was_len, &history[history_cursor]); + replace_input(cursor, len, &history[history_cursor]); + cursor = history[history_cursor].chars().count(); } KeyCode::PageUp => { @@ -271,10 +272,10 @@ fn poll_events(ctx: Arc) -> Result<(), Box> { } KeyCode::Left => { - + cursor = max(1, cursor + 1) - 1; } KeyCode::Right => { - + cursor += 1; } KeyCode::Char(c) => { if event.modifiers.contains(KeyModifiers::CONTROL) && "zxcZXCячсЯЧС".contains(c) { @@ -285,6 +286,7 @@ fn poll_events(ctx: Arc) -> Result<(), Box> { input.write().unwrap().push(c); write!(stdout(), "{}", c).unwrap(); stdout().lock().flush().unwrap(); + cursor += 1; } _ => {} }