Allow set the path to history and init file with env variables: LESSKEY_HISTORY and LESSKEY_INIT.

This commit is contained in:
Oleksandr Kozachuk
2022-12-17 20:05:57 +01:00
parent 982e3c3de3
commit 5ef5ef20ec
3 changed files with 16 additions and 6 deletions
+2 -2
View File
@@ -21,7 +21,7 @@ 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()) { match std::fs::read_to_string(INIT_FILE.to_str().unwrap()) {
Ok(script) => match command_parser::script(&script) { Ok(script) => match command_parser::script(&script) {
Ok(cmd_list) => { Ok(cmd_list) => {
for cmd in cmd_list { for cmd in cmd_list {
@@ -36,7 +36,7 @@ pub fn main() {
Err(err) => { Err(err) => {
LKEval::new( LKEval::new(
Command::Error(LKErr::Error( Command::Error(LKErr::Error(
format!("Failed to read init file {:?}: {}", INIT_FILE.as_path(), err).as_str(), format!("Failed to read init file {:?}: {}", INIT_FILE.to_str(), err).as_str(),
)), )),
lk.clone(), lk.clone(),
prompt_password, prompt_password,
+1 -1
View File
@@ -45,7 +45,7 @@ impl LKRead {
} }
pub fn read(&mut self) -> LKEval { pub fn read(&mut self) -> LKEval {
let history_file = HISTORY_FILE.as_path().to_str().unwrap(); let history_file = HISTORY_FILE.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(_) => (),
+13 -3
View File
@@ -1,11 +1,21 @@
use crate::password::{Comment, Name, PasswordRef}; use crate::password::{Comment, Name, PasswordRef};
use home::home_dir; use home::home_dir;
use std::fmt; use std::fmt;
use std::path::PathBuf; use std::path::Path;
lazy_static! { lazy_static! {
pub static ref HISTORY_FILE: PathBuf = home_dir().unwrap().join(".lesskey_history"); pub static ref HISTORY_FILE: Box<Path> = {
pub static ref INIT_FILE: PathBuf = home_dir().unwrap().join(".lesskeyrc"); match std::env::var("LESSKEY_HISTORY") {
Ok(v) => Path::new(shellexpand::full(&v).unwrap().into_owned().as_str()).to_path_buf().into_boxed_path(),
_ => home_dir().unwrap().join(".lesskey_history").into_boxed_path(),
}
};
pub static ref INIT_FILE: Box<Path> = {
match std::env::var("LESSKEY_INIT") {
Ok(v) => Path::new(shellexpand::full(&v).unwrap().into_owned().as_str()).to_path_buf().into_boxed_path(),
_ => home_dir().unwrap().join(".lesskeyrc").into_boxed_path(),
}
};
} }
#[derive(thiserror::Error, Debug, PartialEq)] #[derive(thiserror::Error, Debug, PartialEq)]