From 847c8bf73656ba1d20a48264e0df1cedb3a1995a Mon Sep 17 00:00:00 2001 From: Justine Date: Tue, 31 Jan 2023 16:58:31 +0100 Subject: [PATCH] Better input / output from commands (direct return), fixed Ctrl+E/A bug --- out.txt | 6 +++++ src/lib.rs | 74 +++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 out.txt diff --git a/out.txt b/out.txt new file mode 100644 index 0000000..a6d50c4 --- /dev/null +++ b/out.txt @@ -0,0 +1,6 @@ +Cargo.lock +Cargo.toml +out.txt +README.md +src +target diff --git a/src/lib.rs b/src/lib.rs index 21850c7..5a79d24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub mod shell { use std::process::Command; use std::io::stdin; use std::io::stdout; + use std::io::Stdout; use std::io::Write; use std::path::Path; use std::env; @@ -11,7 +12,10 @@ pub mod shell { use termion::event::Key; use termion::input::TermRead; use termion::raw::IntoRawMode; + use termion::raw::RawTerminal; use termion::cursor; + use termion::color; + use termion; use unicode_segmentation::UnicodeSegmentation; mod history; @@ -86,23 +90,48 @@ pub mod shell { None => Stdio::inherit(), Some(o) => o, }; - let output = Command::new(command) + + print!("\r\n"); + + let child = match Command::new(command) .args(args) .stdin(stdin) - .output(); + .spawn() { + Ok(h) => h, + Err(e) => { + let err_string = format!("Command not found : {}\r\n", command); + return String::from(err_string); + }, + }; - let command_result = match output { - Ok(o) => o, - Err(e) => { - eprintln!("\r\nGot error {}", e); - return String::from("!"); - }, - }; + let output = child + .wait_with_output() + .expect("Failed to wait on child"); + //RawTerminal::activate_raw_mode(stdout); - let _ = &mut resultat.push_str(str::from_utf8(&command_result.stdout) - .expect("Could not convert command to str") - ); +// let output = Command::new(command) +// .args(args) +// .stdin(stdin) +// .output(); + +// let command_result = match output { +// Ok(o) => o, +// Err(e) => { +// eprintln!("\r\nGot error {}", e); +// return String::from("!"); +// }, +// }; +// +// let _ = &mut resultat.push_str(str::from_utf8(&command_result.stdout) + let status = output.status + .code() + .expect("Could not get code") + .to_string(); previous_command = None; + let format_res = format!("{}...Exit: {}\r\n", + color::Fg(color::Cyan), + status); + let _ = &mut resultat.push_str(&format_res); } }, }; @@ -191,6 +220,7 @@ pub mod shell { pub fn run_raw() { let stdin = stdin(); let mut stdout = stdout().into_raw_mode().unwrap(); + //RawTerminal::suspend_raw_mode(&stdout); let mut mycommand = String::new(); //Used in conjunction with Up arrow to go back in history @@ -227,11 +257,15 @@ pub mod shell { 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 for line in res.split('\n') { - write!(stdout, "\r\n{}", line); + if line != "\r" { + write!(stdout, "\r\n{}", line); + } } current_number = get_curr_history_number(); prompt = build_prompt(¤t_number); @@ -269,14 +303,18 @@ pub mod shell { }, Key::Ctrl('a') => { - write!(stdout, "{}", cursor::Left(current_pos as u16)); - current_pos = 0; + if current_pos != 0 { + write!(stdout, "{}", cursor::Left(current_pos as u16)); + current_pos = 0; + } }, Key::Ctrl('e') => { - let chars_left = max_pos - current_pos; - write!(stdout, "{}", cursor::Right(chars_left as u16)); - current_pos = max_pos; + if current_pos != max_pos { + let chars_left = max_pos - current_pos; + write!(stdout, "{}", cursor::Right(chars_left as u16)); + current_pos = max_pos; + } }, Key::Char(c) => {