Now it is possible to use row number from last ls output, instead of the name to reffer passwords.

This commit is contained in:
Oleksandr Kozachuk
2022-12-12 13:26:50 +01:00
parent a4a9c2afd0
commit 72cb38929b
+19 -5
View File
@@ -74,16 +74,28 @@ impl<'a> LKEval<'a> {
Self { cmd, state } Self { cmd, state }
} }
fn get_password(&self, name: &String) -> Option<PasswordRef> {
match self.state.borrow().db.get(name) {
Some(pwd) => Some(pwd.clone()),
None => match self.state.borrow().ls.get(name) {
Some(pwd) => Some(pwd.clone()),
None => None,
}
}
}
fn cmd_ls(&mut self, out: &mut Vec<String>) { fn cmd_ls(&mut self, out: &mut Vec<String>) {
let mut tmp: Vec<PasswordRef> = vec![]; let mut tmp: Vec<PasswordRef> = vec![];
for (_, name) in &self.state.borrow().db { for (_, name) in &self.state.borrow().db {
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));
self.state.borrow_mut().ls.clear();
let mut counter = 1; let mut counter = 1;
for pwd in tmp { for pwd in tmp {
out.push(format!("{:>3} {}", Radix::new(counter, 36).unwrap().to_string(), pwd.borrow().to_string())); let key = Radix::new(counter, 36).unwrap().to_string(); counter += 1;
counter += 1; self.state.borrow_mut().ls.insert(key.clone(), pwd.clone());
out.push(format!("{:>3} {}", key, pwd.borrow().to_string()));
} }
} }
@@ -105,7 +117,7 @@ impl<'a> LKEval<'a> {
self.state.borrow().fix_hierarchy(); self.state.borrow().fix_hierarchy();
} }
} }
Command::Comment(name, comment) => match self.state.borrow().db.get(name) { Command::Comment(name, comment) => match self.get_password(name) {
Some(pwd) => { Some(pwd) => {
pwd.borrow_mut().comment = match comment { pwd.borrow_mut().comment = match comment {
Some(c) => Some(c.to_string()), Some(c) => Some(c.to_string()),
@@ -114,8 +126,8 @@ impl<'a> LKEval<'a> {
} }
None => out.push("error: password not found".to_string()), None => out.push("error: password not found".to_string()),
}, },
Command::Rm(name) => match self.state.borrow_mut().db.remove(name) { Command::Rm(name) => match self.get_password(name) {
Some(pwd) => out.push(format!("removed {}", pwd.borrow().name)), Some(pwd) => { self.state.borrow_mut().db.remove(&pwd.borrow().name); out.push(format!("removed {}", pwd.borrow().name)); },
None => out.push("error: password not found".to_string()), None => out.push("error: password not found".to_string()),
}, },
Command::Help => { Command::Help => {
@@ -212,5 +224,7 @@ mod tests {
LKEval::new(Command::Ls, lk.clone()).eval(), LKEval::new(Command::Ls, lk.clone()).eval(),
LKPrint::new(vec![" 1 t1 R 99 2022-12-30 comment".to_string(), " 2 t2 R 99 2022-12-31 bli blup".to_string()], false, lk.clone()) LKPrint::new(vec![" 1 t1 R 99 2022-12-30 comment".to_string(), " 2 t2 R 99 2022-12-31 bli blup".to_string()], false, lk.clone())
); );
assert_eq!(LKEval::new(Command::Rm("2".to_string()), lk.clone()).eval(), LKPrint::new(vec!["removed t2".to_string()], false, lk.clone()));
assert_eq!(LKEval::new(Command::Ls, lk.clone()).eval(), LKPrint::new(vec![" 1 t1 R 99 2022-12-30 comment".to_string()], false, lk.clone()));
} }
} }