Better input / output from commands (direct return), fixed Ctrl+E/A bug

This commit is contained in:
Justine
2023-01-31 16:58:31 +01:00
parent 6583128022
commit 847c8bf736
2 changed files with 62 additions and 18 deletions

6
out.txt Normal file
View File

@ -0,0 +1,6 @@
Cargo.lock
Cargo.toml
out.txt
README.md
src
target

View File

@ -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(&current_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) => {