Add ld command, like ls but sort by date. Fix quit command in scripts, output passwords with spaces when no prefix given (align output by name, somewhat).

This commit is contained in:
Oleksandr Kozachuk
2022-12-24 13:53:53 +01:00
parent f0ce3b4d96
commit a69fceb126
6 changed files with 23 additions and 14 deletions
+12 -7
View File
@@ -215,20 +215,20 @@ impl<'a> LKEval<'a> {
}; };
} }
pub fn cmd_source(&self, out: &LKOut, source: &String) { pub fn cmd_source(&self, out: &LKOut, source: &String) -> bool {
let script = if source.trim().ends_with("|") { let script = if source.trim().ends_with("|") {
let (cmd, args) = match get_cmd_args_from_command(source.trim().trim_end_matches('|')) { let (cmd, args) = match get_cmd_args_from_command(source.trim().trim_end_matches('|')) {
Ok(c) => c, Ok(c) => c,
Err(e) => { Err(e) => {
out.e(format!("error: failed to parse command {:?}: {}", source, e.to_string())); out.e(format!("error: failed to parse command {:?}: {}", source, e.to_string()));
return; return false;
} }
}; };
match call_cmd_with_input(&cmd, &args, "") { match call_cmd_with_input(&cmd, &args, "") {
Ok(o) => o, Ok(o) => o,
Err(e) => { Err(e) => {
out.e(format!("error: failed to execute command {}: {}", cmd, e.to_string())); out.e(format!("error: failed to execute command {}: {}", cmd, e.to_string()));
return; return false;
} }
} }
} else { } else {
@@ -237,7 +237,7 @@ impl<'a> LKEval<'a> {
Ok(script) => script, Ok(script) => script,
Err(e) => { Err(e) => {
out.e(format!("error: failed to read file {}: {}", source, e.to_string())); out.e(format!("error: failed to read file {}: {}", source, e.to_string()));
return; return false;
} }
} }
}; };
@@ -246,13 +246,14 @@ impl<'a> LKEval<'a> {
for cmd in cmd_list { for cmd in cmd_list {
let print = LKEval::new(cmd, self.state.clone(), prompt_password).eval(); let print = LKEval::new(cmd, self.state.clone(), prompt_password).eval();
print.out.copy(&out); print.out.copy(&out);
if print.quit { return true; }
} }
} }
Err(e) => { Err(e) => {
out.e(format!("error: {}", e.to_string())); out.e(format!("error: {}", e.to_string()));
return;
} }
}; };
false
} }
pub fn cmd_dump(&self, out: &LKOut, script: &Option<String>) { pub fn cmd_dump(&self, out: &LKOut, script: &Option<String>) {
@@ -308,7 +309,10 @@ impl<'a> LKEval<'a> {
} }
} }
pub fn cmd_ls(&self, out: &LKOut, filter: String) { pub fn cmd_ls<F>(&self, out: &LKOut, filter: String, sort_by: F)
where
F: Fn(&PasswordRef, &PasswordRef) -> std::cmp::Ordering,
{
let re = match Regex::new(&filter) { let re = match Regex::new(&filter) {
Ok(re) => re, Ok(re) => re,
Err(e) => { Err(e) => {
@@ -326,7 +330,8 @@ impl<'a> LKEval<'a> {
tmp.push(name.clone()); tmp.push(name.clone());
} }
} }
tmp.sort_by(|a, b| a.borrow().name.cmp(&b.borrow().name)); tmp.sort_by(|a,b| a.borrow().name.cmp(&b.borrow().name));
tmp.sort_by(sort_by);
self.state.borrow_mut().ls.clear(); self.state.borrow_mut().ls.clear();
let mut counter = 1; let mut counter = 1;
for pwd in tmp { for pwd in tmp {
+1 -1
View File
@@ -16,7 +16,7 @@ mod utils;
use crate::structs::init; use crate::structs::init;
pub fn main() { pub fn main() {
let mut lkread = init(); let mut lkread = match init() { Some(r) => r, None => { return; } };
while lkread.read().eval().print() { while lkread.read().eval().print() {
lkread.refresh(); lkread.refresh();
+2
View File
@@ -14,6 +14,7 @@ peg::parser! {
/ quit_cmd() / quit_cmd()
/ error_cmd() / error_cmd()
/ ls_cmd() / ls_cmd()
/ ld_cmd()
/ mv_cmd() / mv_cmd()
/ rm_cmd() / rm_cmd()
/ pb_cmd() / pb_cmd()
@@ -90,6 +91,7 @@ peg::parser! {
rule dump_def_cmd() -> Command<'input> = "dump" { Command::Dump(None) } rule dump_def_cmd() -> Command<'input> = "dump" { Command::Dump(None) }
rule source_cmd() -> Command<'input> = "source" _ s:$(([' '..='~'])+) { Command::Source(s.to_string()) } rule source_cmd() -> Command<'input> = "source" _ s:$(([' '..='~'])+) { Command::Source(s.to_string()) }
rule ls_cmd() -> Command<'input> = "ls" f:comment()? { Command::Ls(f.unwrap_or(".".to_string())) } rule ls_cmd() -> Command<'input> = "ls" f:comment()? { Command::Ls(f.unwrap_or(".".to_string())) }
rule ld_cmd() -> Command<'input> = "ld" f:comment()? { Command::Ld(f.unwrap_or(".".to_string())) }
rule add_cmd() -> Command<'input> = "add" _ name:name() { Command::Add(Rc::new(RefCell::new(name))) } rule add_cmd() -> Command<'input> = "add" _ name:name() { Command::Add(Rc::new(RefCell::new(name))) }
rule gen_cmd() -> Command<'input> = "gen" n:num()? _ name:name() { rule gen_cmd() -> Command<'input> = "gen" n:num()? _ name:name() {
Command::Gen(match n { Some(n) => n, None => 10_u32 }, Rc::new(RefCell::new(name))) Command::Gen(match n { Some(n) => n, None => 10_u32 }, Rc::new(RefCell::new(name)))
+1 -1
View File
@@ -113,7 +113,7 @@ impl std::string::ToString for Password {
Some(s) => format!(" ^{}", s.borrow().name), Some(s) => format!(" ^{}", s.borrow().name),
None => "".to_string(), None => "".to_string(),
}; };
format!("{}{} {}{} {} {}{}{}", prefix, self.name, length, self.mode, self.seq, self.date, comment, parent) format!("{:>6}{} {}{} {} {}{}{}", prefix, self.name, length, self.mode, self.seq, self.date, comment, parent)
} }
} }
+3 -2
View File
@@ -96,7 +96,8 @@ impl<'a> LKEval<'a> {
out.e("Bye!".to_string()); out.e("Bye!".to_string());
quit = true; quit = true;
} }
Command::Ls(filter) => self.cmd_ls(&out, filter.to_string()), Command::Ls(filter) => self.cmd_ls(&out, filter.to_string(), |a,b| a.borrow().name.cmp(&b.borrow().name)),
Command::Ld(filter) => self.cmd_ls(&out, filter.to_string(), |a,b| b.borrow().date.cmp(&a.borrow().date)),
Command::Add(name) => self.cmd_add(&out, &name), Command::Add(name) => self.cmd_add(&out, &name),
Command::Comment(name, comment) => self.cmd_comment(&out, &name, &comment), Command::Comment(name, comment) => self.cmd_comment(&out, &name, &comment),
Command::Rm(name) => match self.get_password(name) { Command::Rm(name) => match self.get_password(name) {
@@ -109,7 +110,7 @@ impl<'a> LKEval<'a> {
Command::Enc(name) => { self.cmd_enc(&out, name); } Command::Enc(name) => { self.cmd_enc(&out, name); }
Command::Gen(num, name) => self.cmd_gen(&out, &num, &name), Command::Gen(num, name) => self.cmd_gen(&out, &num, &name),
Command::PasteBuffer(command) => self.cmd_pb(&out, command), Command::PasteBuffer(command) => self.cmd_pb(&out, command),
Command::Source(script) => self.cmd_source(&out, script), Command::Source(script) => { quit = self.cmd_source(&out, script); }
Command::Dump(script) => self.cmd_dump(&out, script), Command::Dump(script) => self.cmd_dump(&out, script),
Command::Pass(name) => self.cmd_pass(&out, &name), Command::Pass(name) => self.cmd_pass(&out, &name),
Command::UnPass(name) => match self.state.borrow_mut().secrets.remove(name) { Command::UnPass(name) => match self.state.borrow_mut().secrets.remove(name) {
+4 -3
View File
@@ -57,6 +57,7 @@ pub enum LKErr<'a> {
pub enum Command<'a> { pub enum Command<'a> {
Add(PasswordRef), Add(PasswordRef),
Ls(String), Ls(String),
Ld(String),
Mv(Name, Name), Mv(Name, Name),
Rm(Name), Rm(Name),
Enc(Name), Enc(Name),
@@ -259,14 +260,14 @@ impl fmt::Display for Radix {
} }
} }
pub fn init() -> LKRead { pub fn init() -> Option<LKRead> {
let lk = Rc::new(RefCell::new(LK::new())); let lk = Rc::new(RefCell::new(LK::new()));
match std::fs::read_to_string(INIT_FILE.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 {
LKEval::new(cmd, lk.clone(), prompt_password).eval().print(); if !LKEval::new(cmd, lk.clone(), prompt_password).eval().print() { return None; }
} }
} }
Err(err) => { Err(err) => {
@@ -286,7 +287,7 @@ pub fn init() -> LKRead {
.print(); .print();
} }
} }
LKRead::new(Editor::<()>::new().unwrap(), PROMPT_SETTING.to_string(), lk.clone()) Some(LKRead::new(Editor::<()>::new().unwrap(), PROMPT_SETTING.to_string(), lk.clone()))
} }
#[cfg(test)] #[cfg(test)]