Fixed history and cd without args
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -175,7 +175,7 @@ checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sqish"
|
name = "sqish"
|
||||||
version = "0.1.0"
|
version = "1.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"dirs",
|
"dirs",
|
||||||
"gethostname",
|
"gethostname",
|
||||||
|
21
src/lib.rs
21
src/lib.rs
@ -45,7 +45,8 @@ pub mod shell {
|
|||||||
|
|
||||||
match command {
|
match command {
|
||||||
"cd" => {
|
"cd" => {
|
||||||
let new_dir = args.peekable().peek().map_or("/", |x| *x);
|
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 root = Path::new(new_dir);
|
let root = Path::new(new_dir);
|
||||||
if let Err(e) = env::set_current_dir(&root) {
|
if let Err(e) = env::set_current_dir(&root) {
|
||||||
eprintln!(" Err: {}", e);
|
eprintln!(" Err: {}", e);
|
||||||
@ -113,8 +114,7 @@ pub mod shell {
|
|||||||
.expect("Could not get retcode")
|
.expect("Could not get retcode")
|
||||||
.to_string();
|
.to_string();
|
||||||
previous_command = None;
|
previous_command = None;
|
||||||
let format_res = format!("{}...Exit: {}\r\n",
|
let format_res = format!("{}",
|
||||||
color::Fg(color::Cyan),
|
|
||||||
status);
|
status);
|
||||||
let _ = &mut resultat.push_str(&format_res);
|
let _ = &mut resultat.push_str(&format_res);
|
||||||
}
|
}
|
||||||
@ -218,14 +218,14 @@ pub mod shell {
|
|||||||
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);
|
RawTerminal::suspend_raw_mode(&stdout);
|
||||||
let _res = handle_input(&comm);
|
let res = handle_input(&comm);
|
||||||
RawTerminal::activate_raw_mode(&stdout);
|
RawTerminal::activate_raw_mode(&stdout);
|
||||||
mycommand.clear();
|
mycommand.clear();
|
||||||
//for line in res.split('\n') {
|
for line in res.split('\n') {
|
||||||
// if line != "\r" {
|
if line != "\r" {
|
||||||
// write!(stdout, "\r\n{}", line);
|
write!(stdout, "\r\n{}", line);
|
||||||
// }
|
}
|
||||||
//}
|
}
|
||||||
conf.update_prompt(get_curr_history_number());
|
conf.update_prompt(get_curr_history_number());
|
||||||
write!(stdout, "\r\n{}", conf.promptline).unwrap();
|
write!(stdout, "\r\n{}", conf.promptline).unwrap();
|
||||||
stdout.flush();
|
stdout.flush();
|
||||||
@ -314,7 +314,7 @@ pub mod shell {
|
|||||||
for c in stdin.keys() {
|
for c in stdin.keys() {
|
||||||
let k = c.unwrap();
|
let k = c.unwrap();
|
||||||
match k {
|
match k {
|
||||||
Key::Char('\t') => {
|
Key::Char('\t') => {
|
||||||
//Do NOT search on an empty command
|
//Do NOT search on an empty command
|
||||||
if *&mycommand.len() == 0 {
|
if *&mycommand.len() == 0 {
|
||||||
continue;
|
continue;
|
||||||
@ -330,7 +330,6 @@ pub mod shell {
|
|||||||
write!(stdout, "\r\n{}{}", conf.promptline, res);
|
write!(stdout, "\r\n{}{}", conf.promptline, res);
|
||||||
stdout.flush();
|
stdout.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
Key::Char('\n') => {
|
Key::Char('\n') => {
|
||||||
current_pos = 0;
|
current_pos = 0;
|
||||||
max_pos = 0;
|
max_pos = 0;
|
||||||
|
@ -289,3 +289,4 @@ fn find_common_chars(mut strvec: Vec<String>) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ use std::io::{prelude::*, BufReader};
|
|||||||
|
|
||||||
pub fn write_to_history(command: &str) -> Result<(), std::io::Error> {
|
pub 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.sqish");
|
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)
|
||||||
@ -26,7 +26,7 @@ pub fn write_to_history(command: &str) -> Result<(), std::io::Error> {
|
|||||||
|
|
||||||
pub fn get_history_number() -> Result<i32, std::io::Error> {
|
pub 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.sqish");
|
let filepath = dirs::home_dir().unwrap().join(".history.sqish");
|
||||||
let file = File::open(filepath)?;
|
let file = File::open(filepath)?;
|
||||||
let reader = BufReader::new(file).lines();
|
let 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")))?);
|
||||||
@ -59,7 +59,7 @@ pub fn get_history() -> Result<String, std::io::Error> {
|
|||||||
|
|
||||||
pub fn get_hist_from_number(number: &i32) -> Option<String> {
|
pub 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.sqish");
|
let filepath = dirs::home_dir().unwrap().join(".history.sqish");
|
||||||
|
|
||||||
if filepath.exists() == false {
|
if filepath.exists() == false {
|
||||||
return None;
|
return None;
|
||||||
|
Reference in New Issue
Block a user