fix history

This commit is contained in:
MeexReay 2025-02-11 21:34:58 +03:00
parent 47910442b6
commit f266172cdf
4 changed files with 20 additions and 20 deletions

2
Cargo.lock generated
View File

@ -75,7 +75,7 @@ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]] [[package]]
name = "bRAC" name = "bRAC"
version = "0.1.1+1.99.2" version = "0.1.1+2.0"
dependencies = [ dependencies = [
"clap", "clap",
"colored", "colored",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "bRAC" name = "bRAC"
version = "0.1.1+1.99.2" version = "0.1.1+2.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -1,6 +1,4 @@
{ {
description = "bRAC";
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
rust-overlay.url = "github:oxalica/rust-overlay"; rust-overlay.url = "github:oxalica/rust-overlay";
@ -24,7 +22,7 @@
packages.default = pkgs.rustPlatform.buildRustPackage { packages.default = pkgs.rustPlatform.buildRustPackage {
pname = "bRAC"; pname = "bRAC";
version = "0.1.2+1.99.2"; version = "0.1.2+2.0";
src = pkgs.lib.cleanSource ./.; src = pkgs.lib.cleanSource ./.;
cargoLock = { cargoLock = {

View File

@ -178,11 +178,7 @@ fn find_username_color(message: &str) -> Option<(String, String, Color)> {
} }
fn write_backspace(len: usize) { fn replace_input(cursor: usize, len: usize, text: &str) {
write_backspace_with_text(len, "")
}
fn write_backspace_with_text(len: usize, text: &str) {
let spaces = if text.chars().count() < len { let spaces = if text.chars().count() < len {
len-text.chars().count() len-text.chars().count()
} else { } else {
@ -190,7 +186,7 @@ fn write_backspace_with_text(len: usize, text: &str) {
}; };
write!(stdout(), write!(stdout(),
"{}{}{}{}", "{}{}{}{}",
MoveLeft(1).to_string().repeat(len), MoveLeft(1).to_string().repeat(cursor),
text, text,
" ".repeat(spaces), " ".repeat(spaces),
MoveLeft(1).to_string().repeat(spaces) MoveLeft(1).to_string().repeat(spaces)
@ -222,11 +218,13 @@ fn poll_events(ctx: Arc<Context>) -> Result<(), Box<dyn Error>> {
let message = input.read().unwrap().clone(); let message = input.read().unwrap().clone();
if !message.is_empty() { if !message.is_empty() {
write_backspace(message.chars().count()); replace_input(cursor, message.chars().count(), "");
input.write().unwrap().clear(); input.write().unwrap().clear();
history.insert(history_cursor, message.clone()); cursor = 0;
history_cursor += 1;
history_cursor = history.len()-1;
history.push(String::new());
if message.starts_with("/") && !ctx.disable_commands { if message.starts_with("/") && !ctx.disable_commands {
on_command(ctx.clone(), &message)?; on_command(ctx.clone(), &message)?;
@ -245,9 +243,11 @@ fn poll_events(ctx: Arc<Context>) -> Result<(), Box<dyn Error>> {
} }
} }
KeyCode::Backspace => { KeyCode::Backspace => {
let len = input.read().unwrap().chars().count();
if input.write().unwrap().pop().is_some() { if input.write().unwrap().pop().is_some() {
history[history_cursor].pop(); history[history_cursor].pop();
write_backspace(1); replace_input(cursor, len, &history[history_cursor]);
cursor -= 1;
} }
} }
KeyCode::Esc => { KeyCode::Esc => {
@ -256,13 +256,14 @@ fn poll_events(ctx: Arc<Context>) -> Result<(), Box<dyn Error>> {
} }
KeyCode::Up | KeyCode::Down => { KeyCode::Up | KeyCode::Down => {
history_cursor = if event.code == KeyCode::Up { history_cursor = if event.code == KeyCode::Up {
max(history_cursor + 1, 1) - 1 max(history_cursor, 1) - 1
} else { } else {
min(history_cursor + 1, history.len() - 1) 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(); *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 => { KeyCode::PageUp => {
@ -271,10 +272,10 @@ fn poll_events(ctx: Arc<Context>) -> Result<(), Box<dyn Error>> {
} }
KeyCode::Left => { KeyCode::Left => {
cursor = max(1, cursor + 1) - 1;
} }
KeyCode::Right => { KeyCode::Right => {
cursor += 1;
} }
KeyCode::Char(c) => { KeyCode::Char(c) => {
if event.modifiers.contains(KeyModifiers::CONTROL) && "zxcZXCячсЯЧС".contains(c) { if event.modifiers.contains(KeyModifiers::CONTROL) && "zxcZXCячсЯЧС".contains(c) {
@ -285,6 +286,7 @@ fn poll_events(ctx: Arc<Context>) -> Result<(), Box<dyn Error>> {
input.write().unwrap().push(c); input.write().unwrap().push(c);
write!(stdout(), "{}", c).unwrap(); write!(stdout(), "{}", c).unwrap();
stdout().lock().flush().unwrap(); stdout().lock().flush().unwrap();
cursor += 1;
} }
_ => {} _ => {}
} }