From 3f4d13769e3cda8dd76909e1443801f748924305 Mon Sep 17 00:00:00 2001 From: Justine Date: Sun, 19 Feb 2023 15:47:42 +0100 Subject: [PATCH] Hotkeys now take \n as a press on enter --- README.md | 2 +- src/lib.rs | 87 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 57 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 0b629c6..9749c28 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Rust Shell. This is an attempt to create a simple shell in Rust, because why not TO DO: * Add prompt RGB support -* Add hotkeys in sqishrc (albeit in the same manner as per colors, the conf Struct already has an hashmap field for it) +* Implement Alt+z cancels last hotkey, and also "$ENTER_" is like pressing enter * Add aliases in sqishrc (same old same old) * rc file shoudl probably just be called .sqishrc, and history .sqishrc.hist * Add an Ascii header when starting with a waifu or smth I don't know I'm tired diff --git a/src/lib.rs b/src/lib.rs index c8afcb7..17e4b4a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #[allow(unused_must_use)] pub mod shell { + use std::io::Stdout; use std::process::Command; use std::io::stdin; use std::io::stdout; @@ -222,6 +223,40 @@ pub mod shell { stdout.flush(); } + fn run_cmd(mycommand: &mut String, + current_number: &mut i32, + current_pos: &mut usize, + max_pos: &mut usize, + conf: &mut SqishConf, + stdout: &mut RawTerminal) { + *current_pos = 0; + *max_pos = 0; + if (mycommand != &String::from("\n")) && (mycommand != &String::from("exit")) { + let comm = replace_signs(&mycommand); + RawTerminal::suspend_raw_mode(&stdout); + let _res = handle_input(&comm); + RawTerminal::activate_raw_mode(&stdout); + mycommand.clear(); + //Proper printing of return code + //for line in res.split('\n') { + // if line != "\r" { + // write!(stdout, "\r\n{}", line); + // } + //} + conf.update_prompt(get_curr_history_number()); + write!(stdout, "\r\n{}", conf.promptline).unwrap(); + stdout.flush(); + *current_number += 1; + } else if mycommand == &String::from("exit") { + write!(stdout, "\r\n Sayonara \r\n"); + std::process::exit(0); + } else { + conf.update_prompt(get_curr_history_number()); + write!(stdout, "\r\n{}", conf.promptline).unwrap(); + stdout.flush(); + } + } + fn write_letter(mycommand: &mut String, current_pos: &mut usize, max_pos: &mut usize, c: char) { let mut stdout = stdout().into_raw_mode().unwrap(); if *current_pos == *max_pos { @@ -302,32 +337,12 @@ pub mod shell { } Key::Char('\n') => { - current_pos = 0; - max_pos = 0; - if (mycommand != String::from("\n")) && (mycommand != String::from("exit")) { - let comm = replace_signs(&mycommand); - RawTerminal::suspend_raw_mode(&stdout); - let _res = handle_input(&comm); - RawTerminal::activate_raw_mode(&stdout); - mycommand.clear(); - //Proper printing of return code - //for line in res.split('\n') { - // if line != "\r" { - // write!(stdout, "\r\n{}", line); - // } - //} - &conf.update_prompt(get_curr_history_number()); - write!(stdout, "\r\n{}", conf.promptline).unwrap(); - stdout.flush(); - current_number += 1; - } else if mycommand == String::from("exit") { - write!(stdout, "\r\n Sayonara \r\n"); - break; - } else { - &conf.update_prompt(get_curr_history_number()); - write!(stdout, "\r\n{}", conf.promptline).unwrap(); - stdout.flush(); - } + run_cmd(&mut mycommand, + &mut current_number, + &mut current_pos, + &mut max_pos, + &mut conf, + &mut stdout); }, Key::Ctrl('q') => { @@ -468,12 +483,22 @@ pub mod shell { 'a'..='y' => { let x = String::from(x); if conf.hotkeys.contains_key(&x) { - let hotcmd = &conf.hotkeys[&x]; + let hotcmd = &conf.hotkeys[&x].clone(); for c in hotcmd.chars() { - write_letter(&mut mycommand, - &mut current_pos, - &mut max_pos, - c); + if c != '\n' { + write_letter(&mut mycommand, + &mut current_pos, + &mut max_pos, + c); + } else { + run_cmd(&mut mycommand, + &mut current_number, + &mut current_pos, + &mut max_pos, + &mut conf, + &mut stdout); + } + } } },