Reorganisation, plus de warnings

This commit is contained in:
Justine
2022-12-21 12:26:03 +01:00
parent addf71edd2
commit 3d52e22056
3 changed files with 128 additions and 109 deletions

View File

@ -0,0 +1,8 @@
pub fn autocomplete(input: &String) -> String {
let faketab = format!(" --This is a fake output for autocomplete from {input}-- ");
//In reality, we would keep the input and append to it
let aaa = String::from(faketab);
return aaa;
}

91
src/shell/history.rs Normal file
View File

@ -0,0 +1,91 @@
use std::io::Write;
use std::str;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::{prelude::*, BufReader};
pub fn write_to_history(command: &str) -> Result<(), std::io::Error> {
//Write a command to history
let filepath = dirs::home_dir().unwrap().join("history.sqish");
let mut file = OpenOptions::new()
.write(true)
.append(true)
.create(true)
.open(&filepath)
.unwrap();
let number = match get_history_number() {
Ok(n) => n + 1,
Err(_) => 1
};
writeln!(file, "{} {}", number, command.trim())?;
return Ok(())
}
pub fn get_history_number() -> Result<i32, std::io::Error> {
//Get the latest number found in history file
let filepath = dirs::home_dir().unwrap().join("history.sqish");
let file = File::open(filepath)?;
let reader = BufReader::new(file).lines();
let myline = String::from(reader.last().unwrap_or(Ok(String::from("1")))?);
let number = myline.split(' ').next().expect("???").parse().unwrap();
return Ok(number);
}
pub fn get_history() -> Result<String, std::io::Error> {
let filepath = dirs::home_dir().unwrap().join("history.sqish");
let file = File::open(filepath)?;
let contents = BufReader::new(file).lines();
let mut res = String::new();
for line in contents {
let myline = format!("\r\n{}", line.unwrap());
res.push_str(&myline);
}
return Ok(res);
}
pub fn get_hist_from_number(number: &i32) -> Option<String> {
//Returns a command from its number in hist file
let filepath = dirs::home_dir().unwrap().join("history.sqish");
if filepath.exists() == false {
return None;
}
let file = File::open(filepath)
.expect("Error opening history file...");
if file.metadata().unwrap().len() < 1 {
return None;
}
let reader = BufReader::new(file).lines();
for line in reader {
let current_line = line
.expect("Empty history file ? Please rm it");
let mut s = current_line.split_whitespace();
let n: i32 = s.next()
.expect("Error reading a line in hist file")
.parse()
.unwrap();
let c = s.next().expect("No command!");
if &n == number {
return Some(String::from(c));
}
}
return None;
}
pub fn treat_history_callback(line: &str) -> Option<String> {
let mut mystring = String::from(line);
mystring = mystring.trim().to_string();
let mut chars = mystring.chars();
//Skip the !
chars.next();
let mynbr: i32 = chars.as_str().parse().unwrap();
get_hist_from_number(&mynbr)
}