Some code reformat
This commit is contained in:
parent
b5ef54c8f4
commit
aec1641024
@ -15,6 +15,7 @@ Sqish is now my default shell on my home computer, and I'll keep adding features
|
|||||||
Also keep in mind I'm a beginner in Rust, so the code is pretty dirty; I will probably come around to reformating it once I have all the features I want and ironed out all the bugs.
|
Also keep in mind I'm a beginner in Rust, so the code is pretty dirty; I will probably come around to reformating it once I have all the features I want and ironed out all the bugs.
|
||||||
|
|
||||||
TODO:
|
TODO:
|
||||||
|
* Before anything : keep reformating the code. The main loop could use a struct containing current_pos, max_pos, etc.
|
||||||
* Allow redirecting >> to the end of a file ?
|
* Allow redirecting >> to the end of a file ?
|
||||||
* Allow && in commands ?
|
* Allow && in commands ?
|
||||||
* Improve word jumping
|
* Improve word jumping
|
||||||
|
137
src/lib.rs
137
src/lib.rs
@ -33,50 +33,62 @@ pub mod shell {
|
|||||||
extern crate shell_words;
|
extern crate shell_words;
|
||||||
extern crate ctrlc;
|
extern crate ctrlc;
|
||||||
|
|
||||||
|
fn change_dir(args: &Vec<String>, previous_command: &mut Option<Stdio>) {
|
||||||
|
let homedir = dirs::home_dir().unwrap().into_os_string().into_string().unwrap();
|
||||||
|
//let new_dir = args.peekable().peek().map_or(homedir.as_str(), |x| *x);
|
||||||
|
let mut new_dir = String::new();
|
||||||
|
if args.len() > 0 {
|
||||||
|
*&mut new_dir = args.join(" ");
|
||||||
|
} else {
|
||||||
|
*&mut new_dir = String::from(homedir.as_str());
|
||||||
|
}
|
||||||
|
|
||||||
fn process_line(input: &str) -> Result<String, shell_words::ParseError> {
|
let root = Path::new(&new_dir);
|
||||||
|
if let Err(e) = env::set_current_dir(&root) {
|
||||||
|
eprintln!(" Err: {}", e);
|
||||||
|
} else {
|
||||||
|
println!("");
|
||||||
|
}
|
||||||
|
*previous_command = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_hist(previous_command: &mut Option<Stdio>, command_next: bool) {
|
||||||
|
//let stdout = Stdio::inherit();
|
||||||
|
let res = get_history();
|
||||||
|
match res {
|
||||||
|
Ok(r) => {
|
||||||
|
let filepath = dirs::home_dir().unwrap().join(".history.sqish");
|
||||||
|
let file = File::open(filepath).unwrap();
|
||||||
|
*previous_command = Some(Stdio::from(file));
|
||||||
|
//if !commands.peek().is_some() {
|
||||||
|
if !command_next {
|
||||||
|
print!("{}", r);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => eprintln!(" Err reading history: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_line(input: &str) -> String {
|
||||||
let mut commands = input.trim().split("|").peekable();
|
let mut commands = input.trim().split("|").peekable();
|
||||||
let mut previous_command = None;
|
let mut previous_command = None;
|
||||||
let mut resultat = String::new();
|
let mut resultat = String::new();
|
||||||
|
|
||||||
while let Some(command) = commands.next() {
|
while let Some(command) = commands.next() {
|
||||||
let parts = shell_words::split(&command.trim())?;
|
let parts = match shell_words::split(&command.trim()) {
|
||||||
|
Ok(w) => w,
|
||||||
|
Err(e) => { return format!("Error parsing the command : {:?}", e); },
|
||||||
|
};
|
||||||
let command = parts[0].as_str();
|
let command = parts[0].as_str();
|
||||||
let args = Vec::from(&parts[1..]);
|
let args = Vec::from(&parts[1..]);
|
||||||
|
|
||||||
match command {
|
match command {
|
||||||
"cd" => {
|
"cd" => {
|
||||||
let homedir = dirs::home_dir().unwrap().into_os_string().into_string().unwrap();
|
change_dir(&args, &mut previous_command);
|
||||||
//let new_dir = args.peekable().peek().map_or(homedir.as_str(), |x| *x);
|
|
||||||
let mut new_dir = String::new();
|
|
||||||
if args.len() > 0 {
|
|
||||||
*&mut new_dir = args.join(" ");
|
|
||||||
} else {
|
|
||||||
*&mut new_dir = String::from(homedir.as_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
let root = Path::new(&new_dir);
|
|
||||||
if let Err(e) = env::set_current_dir(&root) {
|
|
||||||
eprintln!(" Err: {}", e);
|
|
||||||
} else {
|
|
||||||
println!("");
|
|
||||||
}
|
|
||||||
previous_command = None;
|
|
||||||
},
|
},
|
||||||
"history" => {
|
"history" => {
|
||||||
//let stdout = Stdio::inherit();
|
let next = commands.peek().is_some();
|
||||||
let res = get_history();
|
print_hist(&mut previous_command, next);
|
||||||
match res {
|
|
||||||
Ok(r) => {
|
|
||||||
let filepath = dirs::home_dir().unwrap().join(".history.sqish");
|
|
||||||
let file = File::open(filepath).unwrap();
|
|
||||||
*&mut previous_command = Some(Stdio::from(file));
|
|
||||||
if !commands.peek().is_some() {
|
|
||||||
print!("{}", r);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(e) => eprintln!(" Err reading history: {}", e),
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
command => {
|
command => {
|
||||||
if commands.peek().is_some() {
|
if commands.peek().is_some() {
|
||||||
@ -114,7 +126,7 @@ pub mod shell {
|
|||||||
Ok(h) => h,
|
Ok(h) => h,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
let err_string = format!("Not found : {}", command);
|
let err_string = format!("Not found : {}", command);
|
||||||
return Ok(String::from(err_string));
|
return String::from(err_string);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -133,7 +145,7 @@ pub mod shell {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return Ok(resultat);
|
return resultat;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replace_signs(line: &String) -> String {
|
fn replace_signs(line: &String) -> String {
|
||||||
@ -160,7 +172,7 @@ pub mod shell {
|
|||||||
match command_found {
|
match command_found {
|
||||||
Some(c) => {
|
Some(c) => {
|
||||||
write_to_history(&c);
|
write_to_history(&c);
|
||||||
let outp = process_line(&c).unwrap();
|
let outp = process_line(&c);
|
||||||
return outp;
|
return outp;
|
||||||
},
|
},
|
||||||
None => return String::from(" "),
|
None => return String::from(" "),
|
||||||
@ -168,7 +180,7 @@ pub mod shell {
|
|||||||
//Regular command
|
//Regular command
|
||||||
} else if line.len() > 0 {
|
} else if line.len() > 0 {
|
||||||
write_to_history(line);
|
write_to_history(line);
|
||||||
return process_line(line).unwrap();
|
return process_line(line);
|
||||||
//Nothing
|
//Nothing
|
||||||
} else {
|
} else {
|
||||||
//Command was empty, new line
|
//Command was empty, new line
|
||||||
@ -330,30 +342,8 @@ pub mod shell {
|
|||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handle_conf(stdout: &mut Stdout) -> SqishConf {
|
||||||
|
let conf = match SqishConf::from_sqishrc() {
|
||||||
|
|
||||||
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
|
|
||||||
//Resetted by pressing Enter
|
|
||||||
let mut current_number = get_curr_history_number();
|
|
||||||
|
|
||||||
//Used to track the location of the cursor inside the command
|
|
||||||
let mut current_pos: usize = 0;
|
|
||||||
let mut max_pos: usize = 0;
|
|
||||||
|
|
||||||
|
|
||||||
//Handle Ctrl+C
|
|
||||||
ctrlc::set_handler(|| {
|
|
||||||
();
|
|
||||||
}).unwrap();
|
|
||||||
|
|
||||||
let mut conf = match SqishConf::from_sqishrc() {
|
|
||||||
Ok(c) => c,
|
Ok(c) => c,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let conf = SqishConf {
|
let conf = SqishConf {
|
||||||
@ -369,6 +359,35 @@ pub mod shell {
|
|||||||
conf
|
conf
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
return conf;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_ctrlc() {
|
||||||
|
ctrlc::set_handler(|| {
|
||||||
|
();
|
||||||
|
}).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
//Resetted by pressing Enter
|
||||||
|
let mut current_number = get_curr_history_number();
|
||||||
|
|
||||||
|
//Used to track the location of the cursor inside the command
|
||||||
|
let mut current_pos: usize = 0;
|
||||||
|
let mut max_pos: usize = 0;
|
||||||
|
|
||||||
|
//Get conf
|
||||||
|
let mut conf = handle_conf(&mut stdout);
|
||||||
|
|
||||||
|
//Handle Ctrl+C
|
||||||
|
set_ctrlc();
|
||||||
|
|
||||||
&conf.update_prompt(get_curr_history_number());
|
&conf.update_prompt(get_curr_history_number());
|
||||||
|
|
||||||
//Initializing
|
//Initializing
|
||||||
|
Loading…
x
Reference in New Issue
Block a user