From a7f8d8d689c2dbe0bfabb68b1a43017e5afd0c86 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Tue, 17 Sep 2024 15:49:08 +0000 Subject: [PATCH] passphrase++ --- src/main.rs | 18 +++++++++--------- src/storage.rs | 16 +++++++++++++++- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index cc9ddc5..12845f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,8 +65,8 @@ fn get_prompt(question: &str) -> io::Result { } // TODO: change password functionality +// TODO: return storage inited fn login() -> io::Result { - // TODO: check if inited print!("Enter passphrase for storage: "); io::stdout().flush()?; // TODO: check in db @@ -75,6 +75,8 @@ fn login() -> io::Result { if password != "pass" { return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong passphrase")); } + print!("\x1B[1A\x1B[2K"); // Move cursor up one line and clear that line + io::stdout().flush()?; // Ensure the changes are reflected immediately Ok(String::from(password)) } @@ -100,12 +102,11 @@ fn init() -> io::Result<()> { } fn list() -> io:: Result<()> { + // TODO: get storage from login let passphrase = login()?; let st = Storage::from_db(passphrase)?; - let mut vec: Vec<_> = st.items.iter().collect(); - vec.sort(); - for item in &vec { - println!("{}", item.id); + for id in st.ids() { + println!("{}", id); } Ok(()) } @@ -123,7 +124,7 @@ fn add(id: &String) -> io::Result<()> { } let content = editor::open_to_edit()?; - st.items.insert(Item::from(id.clone(), content)); + st.add(Item::from(id.clone(), content)); st.dump()?; Ok(()) @@ -140,7 +141,7 @@ fn show(id: &String) -> io::Result<()> { } let item = st.get(id); - editor::open_to_show(&item.content); + editor::open_to_show(&item.content)?; Ok(()) } @@ -166,8 +167,7 @@ fn run_command() -> io::Result<()> { PROMPT::NO => Storage::print_init_hint(), } } else { - login()?; - // TODO: list() + list()? } } } diff --git a/src/storage.rs b/src/storage.rs index 14d5b58..fbba804 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -98,7 +98,7 @@ impl Encoder { } pub struct Storage { - pub items: HashSet::, + items: HashSet::, encoder: Encoder } @@ -110,6 +110,7 @@ impl Storage { } } pub fn from_db(passphrase: String) -> io::Result { + // TODO: throw error if it's not inited let encoder = Encoder::from(passphrase); // TODO: throw error is password is incorrect let file = fs::File::open(&*STORAGE_PATH)?; @@ -175,6 +176,15 @@ impl Storage { let path = Path::new(&*STORAGE_FOLDER); return path.exists(); } + + pub fn ids(&self) -> Vec { + let mut result = Vec::new(); + for item in self.items.iter() { + result.push(item.id.clone()); + } + result.sort(); + result + } pub fn contains(&self, id: &String) -> bool { let item = Item::from_empty(id.clone()); @@ -186,6 +196,10 @@ impl Storage { self.items.get(&item).unwrap() } + pub fn add(&mut self, item: Item) { + self.items.insert(item); + } + pub fn dump(&self) -> io::Result<()> { let mut file = fs::OpenOptions::new() .write(true)