From ac013074210a0708c7d63fa2fd95b5b45ac7cb50 Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Sun, 11 Dec 2022 16:23:22 +0100 Subject: [PATCH] Refactor ls command, to move the code to a separate function and display the line number in 36 radix, as preparation to remember the output. Sort currently not work any more. --- src/repl.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/repl.rs b/src/repl.rs index 541f4ff..95a85cd 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -5,7 +5,7 @@ use std::{cell::RefCell, rc::Rc}; use crate::lk::LK; use crate::parser::command_parser; use crate::password::fix_password_recursion; -use crate::structs::{Command, LKErr}; +use crate::structs::{Command, LKErr, Radix}; #[derive(Debug)] pub struct LKRead { @@ -74,6 +74,14 @@ impl<'a> LKEval<'a> { Self { cmd, state } } + fn cmd_ls(&mut self, out: &mut Vec) { + let mut counter = 0; + for (_, name) in &self.state.borrow().db { + out.push(format!("{:>2} {}", Radix::new(counter, 36).unwrap().to_string(), name.borrow().to_string())); + counter += 1; + } + } + pub fn eval(&mut self) -> LKPrint { let mut out: Vec = vec![]; let mut quit: bool = false; @@ -83,12 +91,7 @@ impl<'a> LKEval<'a> { out.push("Bye!".to_string()); quit = true; } - Command::Ls => { - for (_, name) in &self.state.borrow().db { - out.push(name.borrow().to_string()); - } - out.sort(); - } + Command::Ls => self.cmd_ls(&mut out), Command::Add(name) => { if self.state.borrow().db.get(&name.borrow().name).is_some() { out.push("error: password already exist".to_string());