History callbacks now allow piping

This commit is contained in:
Justine Pelletreau 2023-02-21 16:44:31 +01:00
parent 6fa2788161
commit c06afb410c
3 changed files with 16 additions and 5 deletions

View File

@ -2,7 +2,6 @@
Rust Shell. This is an attempt to create a simple shell in Rust, because why not.
TODO:
* A shortcut / implemented command to show all Hotkeys
* git commit -m "message" does not work
* Startup script section in sqishrc
@ -15,13 +14,14 @@ Some shortcuts are built in. They may be unique to Sqish or be copied from Zsh f
* Ctrl+Q or Ctrl+D : Quits the shell immediatly
* Ctrl+U : Clears currently written line
* Alt+. : Appends the last word from previous command
* Alt+! : Show a list of the user-configured hotkeys
* 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
### In Text:
* !12 : Reruns command number 12 from history (Replace the number with anything)
* !! : Appends the previous command to the current line
* !12 : Returns command number 12 from history (Replace the number 12 12 12 12 12 12 12 12 12 12 12 12 with any number from history). You can add commands after it, like: "!12 | grep squirrel"
* !! : Appends the previous command to the current line. Same principle as !12.

View File

@ -546,6 +546,10 @@ pub mod shell {
'.' => {
append_prev_arg(&mut mycommand, &mut current_pos, &mut max_pos);
},
'!' => {
let mut cmd = format!("echo -- ALIASES --> {:?}", conf.hotkeys);
run_cmd(&mut cmd, &mut current_number, &mut conf, &mut stdout);
},
_ => (),
};

View File

@ -20,6 +20,8 @@ pub fn write_to_history(command: &str) -> Result<(), std::io::Error> {
Err(_) => 1
};
let command = command.replace('\n', "\\n");
writeln!(file, "{} {}", number, command.trim())?;
return Ok(())
}
@ -93,12 +95,17 @@ 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);
mystring = mystring.trim().to_string();
let temp = mystring.split_whitespace().collect::<Vec<&str>>();
let mystring = temp.first().unwrap_or(&line);
let mut chars = mystring.chars();
let args = &temp[1..];
//Skip the !
chars.next();
let mynbr: i32 = chars.as_str().parse().unwrap();
get_hist_from_number(&mynbr)
match get_hist_from_number(&mynbr) {
Some(h) => return Some(format!("{} {}", h, args.join(" "))),
None => return None,
}
}
pub fn get_prev_arg() -> Option<String> {