From 6f63294857684a7e1624619660b5f678943a627a Mon Sep 17 00:00:00 2001 From: Justine Date: Wed, 22 Feb 2023 16:03:51 +0100 Subject: [PATCH] "Ctrl + P and O more or less do the same thing as Ctrl + Right and left Also remove a warning about a useless mut" --- README.md | 3 ++- src/lib.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/shell/history.rs | 2 +- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2261b92..3113248 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ Rust Shell. This is an attempt to create a simple but usable shell in Rust, beca TODO: * git commit -m "message" does not work * Allow redirecting >> to the end of a file ? -* Implement Ctrl+left and right to jump from word to word ## sqishrc (Config) See the included sqishrc file included, and copy it as ~/.sqishrc @@ -19,6 +18,8 @@ Some shortcuts are built in. They may be unique to Sqish or be copied from Zsh f * Ctrl+A : Jumps to the end of the line * Ctrl+E : Jumps to the start of the line * Ctrl+C : Clears the current line and starts a new one +* Ctrl+P : Jump to the next word +* Ctrl+O : Jump to the previous word ### In Text: * !12 : Returns command number 12 from history (Replace the number 12 with any number from history). You can add commands after it, like: "!12 | grep squirrel" diff --git a/src/lib.rs b/src/lib.rs index 6a81e03..e91526d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -314,6 +314,27 @@ pub mod shell { }; } + fn find_next_space(current_pos: &usize, max_pos: &usize, command: &String) -> usize { + let mut steps: usize = 0; + if *current_pos == *max_pos { return steps; } + for letter in command.chars().skip(*current_pos) { + //println!("L : {:?}, S : {:?} C: {:?} M {:?}", letter, steps, current_pos, max_pos); + //Skip if we start on a space + if steps == 0 && (letter == ' ' || letter == ',' || letter == ';') { + continue; + } else if letter != ' ' && *current_pos + steps < *max_pos { + steps += 1; + } else { + return steps; + } + } + let max: usize = (0 + max_pos) - current_pos; + return max; + } + + + + pub fn run_raw() { let stdin = stdin(); let mut stdout = stdout().into_raw_mode().unwrap(); @@ -523,6 +544,25 @@ pub mod shell { current_pos += 1; } }, + + Key::Ctrl('p') => { + let steps = find_next_space(¤t_pos, &max_pos, &mycommand); + //println!("steps: {:?} cur {:?} max {:?}", steps, current_pos, max_pos); + if steps > 0 { + print!("{}", cursor::Right(steps as u16)); + current_pos += steps; + } + }, + + Key::Ctrl('o') => { + let command_rev = mycommand.chars().rev().collect::(); + let curr_rev = max_pos - current_pos + 1; + let steps = find_next_space(&curr_rev, &max_pos, &command_rev); + if steps > 0 { + print!("{}", cursor::Left(steps as u16)); + current_pos -= steps; + } + }, Key::Alt(x) => { match x { diff --git a/src/shell/history.rs b/src/shell/history.rs index 5810c00..f55c7f6 100644 --- a/src/shell/history.rs +++ b/src/shell/history.rs @@ -94,7 +94,7 @@ pub fn get_hist_from_number(number: &i32) -> Option { } pub fn treat_history_callback(line: &str) -> Option { - let mut mystring = String::from(line); + let mystring = String::from(line); let temp = mystring.split_whitespace().collect::>(); let mystring = temp.first().unwrap_or(&line); let mut chars = mystring.chars();