Add support for an init file ~/.lesskeyrc
This commit is contained in:
+23
-1
@@ -8,14 +8,36 @@ mod repl;
|
||||
mod skey;
|
||||
mod structs;
|
||||
|
||||
use rpassword::prompt_password;
|
||||
use rustyline::Editor;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
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() {
|
||||
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());
|
||||
|
||||
while lkread.read().eval().print() {
|
||||
|
||||
+2
-4
@@ -1,4 +1,3 @@
|
||||
use home::home_dir;
|
||||
use regex::Regex;
|
||||
use rpassword::prompt_password;
|
||||
use rustyline::error::ReadlineError;
|
||||
@@ -8,7 +7,7 @@ use std::{cell::RefCell, rc::Rc};
|
||||
use crate::lk::LK;
|
||||
use crate::parser::command_parser;
|
||||
use crate::password::{fix_password_recursion, PasswordRef};
|
||||
use crate::structs::{Command, LKErr, Radix};
|
||||
use crate::structs::{Command, LKErr, Radix, HISTORY_FILE};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct LKRead {
|
||||
@@ -45,8 +44,7 @@ impl LKRead {
|
||||
}
|
||||
|
||||
pub fn read(&mut self) -> LKEval {
|
||||
let history_file_path = home_dir().unwrap().join(".lesskey_history");
|
||||
let history_file = history_file_path.as_path().to_str().unwrap();
|
||||
let history_file = HISTORY_FILE.as_path().to_str().unwrap();
|
||||
self.rl.clear_history();
|
||||
match self.rl.load_history(&history_file) {
|
||||
Ok(_) => (),
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
use crate::password::{Comment, Name, PasswordRef};
|
||||
use home::home_dir;
|
||||
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)]
|
||||
pub enum LKErr<'a> {
|
||||
|
||||
Reference in New Issue
Block a user