Add support for an init file ~/.lesskeyrc

This commit is contained in:
Oleksandr Kozachuk
2022-12-15 19:02:05 +01:00
parent f41200050e
commit 18faa1d766
3 changed files with 32 additions and 5 deletions
+23 -1
View File
@@ -8,14 +8,36 @@ mod repl;
mod skey; mod skey;
mod structs; mod structs;
use rpassword::prompt_password;
use rustyline::Editor; use rustyline::Editor;
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
use crate::lk::LK; use crate::lk::LK;
use crate::repl::LKRead; use crate::parser::command_parser;
use crate::repl::{LKEval, LKRead};
use crate::structs::{Command, LKErr, INIT_FILE};
pub fn main() { pub fn main() {
let lk = Rc::new(RefCell::new(LK::new())); let lk = Rc::new(RefCell::new(LK::new()));
match std::fs::read_to_string(INIT_FILE.as_path().to_str().unwrap()) {
Ok(script) => match command_parser::script(&script) {
Ok(cmd_list) => {
for cmd in cmd_list {
LKEval::new(cmd, lk.clone(), prompt_password).eval().print();
}
}
Err(err) => {
LKEval::new(Command::Error(LKErr::ParseError(err)), lk.clone(), prompt_password).eval().print();
}
},
Err(err) if err.kind() == std::io::ErrorKind::NotFound => (),
Err(err) => {
LKEval::new(Command::Error(LKErr::Error(format!("Failed to read init file {:?}: {}", INIT_FILE.as_path(), err).as_str())), lk.clone(), prompt_password)
.eval()
.print();
}
}
let mut lkread = LKRead::new(Editor::<()>::new().unwrap(), String::from(" "), lk.clone()); let mut lkread = LKRead::new(Editor::<()>::new().unwrap(), String::from(" "), lk.clone());
while lkread.read().eval().print() { while lkread.read().eval().print() {
+2 -4
View File
@@ -1,4 +1,3 @@
use home::home_dir;
use regex::Regex; use regex::Regex;
use rpassword::prompt_password; use rpassword::prompt_password;
use rustyline::error::ReadlineError; use rustyline::error::ReadlineError;
@@ -8,7 +7,7 @@ use std::{cell::RefCell, rc::Rc};
use crate::lk::LK; use crate::lk::LK;
use crate::parser::command_parser; use crate::parser::command_parser;
use crate::password::{fix_password_recursion, PasswordRef}; use crate::password::{fix_password_recursion, PasswordRef};
use crate::structs::{Command, LKErr, Radix}; use crate::structs::{Command, LKErr, Radix, HISTORY_FILE};
#[derive(Debug)] #[derive(Debug)]
pub struct LKRead { pub struct LKRead {
@@ -45,8 +44,7 @@ impl LKRead {
} }
pub fn read(&mut self) -> LKEval { pub fn read(&mut self) -> LKEval {
let history_file_path = home_dir().unwrap().join(".lesskey_history"); let history_file = HISTORY_FILE.as_path().to_str().unwrap();
let history_file = history_file_path.as_path().to_str().unwrap();
self.rl.clear_history(); self.rl.clear_history();
match self.rl.load_history(&history_file) { match self.rl.load_history(&history_file) {
Ok(_) => (), Ok(_) => (),
+7
View File
@@ -1,5 +1,12 @@
use crate::password::{Comment, Name, PasswordRef}; use crate::password::{Comment, Name, PasswordRef};
use home::home_dir;
use std::fmt; use std::fmt;
use std::path::PathBuf;
lazy_static! {
pub static ref HISTORY_FILE: PathBuf = home_dir().unwrap().join(".lesskey_history");
pub static ref INIT_FILE: PathBuf = home_dir().unwrap().join(".lesskeyrc");
}
#[derive(thiserror::Error, Debug, PartialEq)] #[derive(thiserror::Error, Debug, PartialEq)]
pub enum LKErr<'a> { pub enum LKErr<'a> {