Better input / output from commands (direct return), fixed Ctrl+E/A bug
This commit is contained in:
74
src/lib.rs
74
src/lib.rs
@ -3,6 +3,7 @@ pub mod shell {
|
|||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::io::stdin;
|
use std::io::stdin;
|
||||||
use std::io::stdout;
|
use std::io::stdout;
|
||||||
|
use std::io::Stdout;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::env;
|
use std::env;
|
||||||
@ -11,7 +12,10 @@ pub mod shell {
|
|||||||
use termion::event::Key;
|
use termion::event::Key;
|
||||||
use termion::input::TermRead;
|
use termion::input::TermRead;
|
||||||
use termion::raw::IntoRawMode;
|
use termion::raw::IntoRawMode;
|
||||||
|
use termion::raw::RawTerminal;
|
||||||
use termion::cursor;
|
use termion::cursor;
|
||||||
|
use termion::color;
|
||||||
|
use termion;
|
||||||
use unicode_segmentation::UnicodeSegmentation;
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
|
|
||||||
mod history;
|
mod history;
|
||||||
@ -86,23 +90,48 @@ pub mod shell {
|
|||||||
None => Stdio::inherit(),
|
None => Stdio::inherit(),
|
||||||
Some(o) => o,
|
Some(o) => o,
|
||||||
};
|
};
|
||||||
let output = Command::new(command)
|
|
||||||
|
print!("\r\n");
|
||||||
|
|
||||||
|
let child = match Command::new(command)
|
||||||
.args(args)
|
.args(args)
|
||||||
.stdin(stdin)
|
.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 {
|
let output = child
|
||||||
Ok(o) => o,
|
.wait_with_output()
|
||||||
Err(e) => {
|
.expect("Failed to wait on child");
|
||||||
eprintln!("\r\nGot error {}", e);
|
//RawTerminal::activate_raw_mode(stdout);
|
||||||
return String::from("!");
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
let _ = &mut resultat.push_str(str::from_utf8(&command_result.stdout)
|
// let output = Command::new(command)
|
||||||
.expect("Could not convert command to str")
|
// .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;
|
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() {
|
pub fn run_raw() {
|
||||||
let stdin = stdin();
|
let stdin = stdin();
|
||||||
let mut stdout = stdout().into_raw_mode().unwrap();
|
let mut stdout = stdout().into_raw_mode().unwrap();
|
||||||
|
//RawTerminal::suspend_raw_mode(&stdout);
|
||||||
let mut mycommand = String::new();
|
let mut mycommand = String::new();
|
||||||
|
|
||||||
//Used in conjunction with Up arrow to go back in history
|
//Used in conjunction with Up arrow to go back in history
|
||||||
@ -227,11 +257,15 @@ pub mod shell {
|
|||||||
max_pos = 0;
|
max_pos = 0;
|
||||||
if (mycommand != String::from("\n")) && (mycommand != String::from("exit")) {
|
if (mycommand != String::from("\n")) && (mycommand != String::from("exit")) {
|
||||||
let comm = replace_signs(&mycommand);
|
let comm = replace_signs(&mycommand);
|
||||||
|
RawTerminal::suspend_raw_mode(&stdout);
|
||||||
let res = handle_input(&comm);
|
let res = handle_input(&comm);
|
||||||
|
RawTerminal::activate_raw_mode(&stdout);
|
||||||
mycommand.clear();
|
mycommand.clear();
|
||||||
//Proper printing
|
//Proper printing
|
||||||
for line in res.split('\n') {
|
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();
|
current_number = get_curr_history_number();
|
||||||
prompt = build_prompt(¤t_number);
|
prompt = build_prompt(¤t_number);
|
||||||
@ -269,14 +303,18 @@ pub mod shell {
|
|||||||
},
|
},
|
||||||
|
|
||||||
Key::Ctrl('a') => {
|
Key::Ctrl('a') => {
|
||||||
write!(stdout, "{}", cursor::Left(current_pos as u16));
|
if current_pos != 0 {
|
||||||
current_pos = 0;
|
write!(stdout, "{}", cursor::Left(current_pos as u16));
|
||||||
|
current_pos = 0;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
Key::Ctrl('e') => {
|
Key::Ctrl('e') => {
|
||||||
let chars_left = max_pos - current_pos;
|
if current_pos != max_pos {
|
||||||
write!(stdout, "{}", cursor::Right(chars_left as u16));
|
let chars_left = max_pos - current_pos;
|
||||||
current_pos = max_pos;
|
write!(stdout, "{}", cursor::Right(chars_left as u16));
|
||||||
|
current_pos = max_pos;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
Key::Char(c) => {
|
Key::Char(c) => {
|
||||||
|
Reference in New Issue
Block a user