From e3d43755b1cef3f004001454dccd05114a44d018 Mon Sep 17 00:00:00 2001 From: zegonix Date: Sun, 8 Dec 2024 11:29:15 +0100 Subject: [PATCH] implemented popping multiple entries at once --- src/bookmarks.rs | 8 +++++++- src/main.rs | 2 +- src/stack.rs | 15 ++++++++------- todo.md | 2 +- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/bookmarks.rs b/src/bookmarks.rs index 3a73324..a4d4a8a 100644 --- a/src/bookmarks.rs +++ b/src/bookmarks.rs @@ -17,7 +17,6 @@ pub struct Bookmarks { bookmarks: HashMap, } - impl Bookmarks { const BOOKMARK_FILE_NAME: &str = "bookmarks.conf"; @@ -46,9 +45,16 @@ impl Bookmarks { /// reads and parses the bookmarks file fn build_bookmarks(&mut self) -> Result<()> { + // check if configuration directory exists, if not create it + if !self.conf_dir.is_dir() { + fs::create_dir(self.conf_dir.clone())?; + } + let mut bookmark_file = self.conf_dir.clone(); + bookmark_file.push(Self::BOOKMARK_FILE_NAME); + // check if bookmarks file exists, if not create it if !bookmark_file.is_file() { _ = File::create(bookmark_file.clone())?; } diff --git a/src/main.rs b/src/main.rs index 4d470c6..23d4ea2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ fn main() -> Result<()> { return Ok(()); } }; - let mut config = match Config::new() { + let config = match Config::new() { Ok(value) => value, Err(error) => { print!("echo '{}{}{}' && false", style_error, error, RESET_SEQ); diff --git a/src/stack.rs b/src/stack.rs index 0a339d3..038533f 100644 --- a/src/stack.rs +++ b/src/stack.rs @@ -60,14 +60,15 @@ impl Stack { /// pop entry from stack /// return popped entry pub fn pop_entry(&mut self, num_entries: Option) -> Result { - let mut num = 1; - let mut entry: Option; - if let Some(value) = num_entries { - num = value; - } - for _ in 0..num { - entry = self.stack.pop(); + let mut num = num_entries.unwrap_or(1); + if num < 1 { + num = 1; + } else if num > self.stack.len() { + num = self.stack.len(); } + let mut dropped_entries = self.stack.drain((self.stack.len()-num)..); + let entry = dropped_entries.nth(0); + drop(dropped_entries); self.write_stack_file()?; match entry { Some(entry) => Ok(entry), diff --git a/todo.md b/todo.md index 6b554c8..9fabb9a 100644 --- a/todo.md +++ b/todo.md @@ -1,7 +1,7 @@ # todos for 'navigation' - [x] replace `expect` statements in 'stack.rs' with actual error handling -- [ ] pop several entries at a time +- [x] pop several entries at a time - [x] drop stack - [ ] config file - [ ] dedup stack option