"Ctrl + P and O more or less do the same thing as Ctrl + Right and left

Also remove a warning about a useless mut"
This commit is contained in:
Justine 2023-02-22 16:03:51 +01:00
parent d903bb4bc8
commit 6f63294857
3 changed files with 43 additions and 2 deletions

View File

@ -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"

View File

@ -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();
@ -524,6 +545,25 @@ pub mod shell {
}
},
Key::Ctrl('p') => {
let steps = find_next_space(&current_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::<String>();
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 {
'a'..='z' => {

View File

@ -94,7 +94,7 @@ pub fn get_hist_from_number(number: &i32) -> Option<String> {
}
pub fn treat_history_callback(line: &str) -> Option<String> {
let mut mystring = String::from(line);
let mystring = String::from(line);
let temp = mystring.split_whitespace().collect::<Vec<&str>>();
let mystring = temp.first().unwrap_or(&line);
let mut chars = mystring.chars();