Fleche du haut marche (plus ou moins)
This commit is contained in:
parent
09806c7463
commit
addf71edd2
80
src/lib.rs
80
src/lib.rs
@ -97,7 +97,7 @@ pub mod shell {
|
|||||||
|
|
||||||
fn write_to_history(command: &str) -> Result<(), std::io::Error> {
|
fn write_to_history(command: &str) -> Result<(), std::io::Error> {
|
||||||
//Write a command to history
|
//Write a command to history
|
||||||
let filepath = dirs::home_dir().unwrap().join("history.rshell");
|
let filepath = dirs::home_dir().unwrap().join("history.sqish");
|
||||||
let mut file = OpenOptions::new()
|
let mut file = OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
.append(true)
|
.append(true)
|
||||||
@ -116,7 +116,7 @@ pub mod shell {
|
|||||||
|
|
||||||
fn get_history_number() -> Result<i32, std::io::Error> {
|
fn get_history_number() -> Result<i32, std::io::Error> {
|
||||||
//Get the latest number found in history file
|
//Get the latest number found in history file
|
||||||
let filepath = dirs::home_dir().unwrap().join("history.rshell");
|
let filepath = dirs::home_dir().unwrap().join("history.sqish");
|
||||||
let file = File::open(filepath)?;
|
let file = File::open(filepath)?;
|
||||||
let mut reader = BufReader::new(file).lines();
|
let mut reader = BufReader::new(file).lines();
|
||||||
let myline = String::from(reader.last().unwrap_or(Ok(String::from("1")))?);
|
let myline = String::from(reader.last().unwrap_or(Ok(String::from("1")))?);
|
||||||
@ -125,7 +125,7 @@ pub mod shell {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_history() -> Result<String, std::io::Error> {
|
fn get_history() -> Result<String, std::io::Error> {
|
||||||
let filepath = dirs::home_dir().unwrap().join("history.rshell");
|
let filepath = dirs::home_dir().unwrap().join("history.sqish");
|
||||||
let file = File::open(filepath)?;
|
let file = File::open(filepath)?;
|
||||||
let contents = BufReader::new(file).lines();
|
let contents = BufReader::new(file).lines();
|
||||||
let mut res = String::new();
|
let mut res = String::new();
|
||||||
@ -138,7 +138,7 @@ pub mod shell {
|
|||||||
|
|
||||||
fn get_hist_from_number(number: &i32) -> Option<String> {
|
fn get_hist_from_number(number: &i32) -> Option<String> {
|
||||||
//Returns a command from its number in hist file
|
//Returns a command from its number in hist file
|
||||||
let filepath = dirs::home_dir().unwrap().join("history.rshell");
|
let filepath = dirs::home_dir().unwrap().join("history.sqish");
|
||||||
|
|
||||||
if filepath.exists() == false {
|
if filepath.exists() == false {
|
||||||
return None;
|
return None;
|
||||||
@ -261,7 +261,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();
|
||||||
let prompt = build_prompt();
|
let mut prompt = build_prompt();
|
||||||
let mut mycommand = String::new();
|
let mut mycommand = String::new();
|
||||||
|
|
||||||
//Initialize
|
//Initialize
|
||||||
@ -288,9 +288,11 @@ pub mod shell {
|
|||||||
for line in res.split('\n') {
|
for line in res.split('\n') {
|
||||||
write!(stdout, "\r\n{}", line);
|
write!(stdout, "\r\n{}", line);
|
||||||
}
|
}
|
||||||
write!(stdout, "\r\n{}", prompt).unwrap();
|
prompt = build_prompt();
|
||||||
|
write!(stdout, "{}", prompt).unwrap();
|
||||||
stdout.flush();
|
stdout.flush();
|
||||||
} else if mycommand == String::from("exit") {
|
} else if mycommand == String::from("exit") {
|
||||||
|
write!(stdout, "\r\n Sayonara \r\n");
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
write!(stdout, "\r\n{}", prompt).unwrap();
|
write!(stdout, "\r\n{}", prompt).unwrap();
|
||||||
@ -298,7 +300,10 @@ pub mod shell {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
Key::Ctrl('q') => break,
|
Key::Ctrl('q') => {
|
||||||
|
writeln!(stdout, "\r\n Sayonara \r\n");
|
||||||
|
break;
|
||||||
|
},
|
||||||
|
|
||||||
Key::Char(C) => {
|
Key::Char(C) => {
|
||||||
mycommand.push(C);
|
mycommand.push(C);
|
||||||
@ -317,6 +322,20 @@ pub mod shell {
|
|||||||
stdout.flush();
|
stdout.flush();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Key::Up => {
|
||||||
|
let lastnumber = match get_history_number() {
|
||||||
|
Ok(e) => e,
|
||||||
|
Err(_) => continue,
|
||||||
|
};
|
||||||
|
let lastcmd = match get_hist_from_number(&lastnumber) {
|
||||||
|
Some(c) => c,
|
||||||
|
None => continue
|
||||||
|
};
|
||||||
|
let prompt = build_prompt();
|
||||||
|
write!(stdout, "\r\n{}{}", prompt, lastcmd.trim());
|
||||||
|
mycommand = lastcmd;
|
||||||
|
},
|
||||||
|
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -325,56 +344,13 @@ pub mod shell {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn run(){
|
|
||||||
// loop {
|
|
||||||
// print!("{}", build_prompt());
|
|
||||||
// stdout().flush();
|
|
||||||
// let mut input = String::new();
|
|
||||||
// let bytes = stdin().read_line(&mut input).unwrap();
|
|
||||||
// input = replace_signs(input);
|
|
||||||
//
|
|
||||||
// //history callback
|
|
||||||
// if (bytes > 0) && (input != String::from("\n")) && (input.starts_with('!')) {
|
|
||||||
// let command_found = treat_history_callback(&input);
|
|
||||||
// match command_found {
|
|
||||||
// Some(c) => {
|
|
||||||
// write_to_history(&c);
|
|
||||||
// process_line(&c);
|
|
||||||
// },
|
|
||||||
// None => ()
|
|
||||||
// };
|
|
||||||
// //Regular command
|
|
||||||
// } else if (bytes > 0) && (input != String::from("\n")) {
|
|
||||||
// write_to_history(&input);
|
|
||||||
// if process_line(input) {
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// //Nothing
|
|
||||||
// } else {
|
|
||||||
// //Command was empty, new line
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn inittests() {
|
fn inittests() {
|
||||||
let filepath = dirs::home_dir().unwrap().join("history.rshell");
|
let filepath = dirs::home_dir().unwrap().join("history.sqish");
|
||||||
|
|
||||||
let file = File::create(&filepath).expect("Could not create test history");
|
let file = File::create(&filepath).expect("Could not create test history");
|
||||||
writeln!(&file, "1 ls");
|
writeln!(&file, "1 ls");
|
||||||
@ -416,7 +392,7 @@ pub mod shell {
|
|||||||
write_to_history(&towrite);
|
write_to_history(&towrite);
|
||||||
|
|
||||||
//Now check directly against the file
|
//Now check directly against the file
|
||||||
let filepath = dirs::home_dir().unwrap().join("history.rshell");
|
let filepath = dirs::home_dir().unwrap().join("history.sqish");
|
||||||
let file = File::open(filepath).expect("Error");
|
let file = File::open(filepath).expect("Error");
|
||||||
let mut reader = BufReader::new(file).lines();
|
let mut reader = BufReader::new(file).lines();
|
||||||
let myline = String::from(reader.last().unwrap().expect("Error"));
|
let myline = String::from(reader.last().unwrap().expect("Error"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user