diff --git a/src/arguments.rs b/src/arguments.rs index 71dca65..1367709 100644 --- a/src/arguments.rs +++ b/src/arguments.rs @@ -1,6 +1,5 @@ use clap::{Args, Parser, Subcommand}; -use std::{path::PathBuf}; - +use std::path::PathBuf; /// implements stack for cd wrapper script #[derive(Parser, Debug)] @@ -43,14 +42,25 @@ pub struct PushArgs { #[derive(Debug, Clone, Args)] pub struct PopArgs { + /// pop multiple values, either a specified amount with a number, or all entries with 'all' + #[command(subcommand)] + pub action: Option, + /// show stack #[arg(short, long)] pub show_stack: Option, - /// pop multiple entries and navigate to last retrieved path + /// pop specified number of entries pub num_entries: Option, } +#[derive(Debug, Clone, Subcommand)] +#[allow(non_camel_case_types)] +pub enum PopAction { + /// pop all entries and move to first entry in stack + all, +} + #[derive(Debug, Clone, Args)] pub struct StackArgs { /// hide entry numbers @@ -93,7 +103,7 @@ pub enum BookmarkAction { add(BookmarkSubArgs), /// remove a bookmark by name `book remove ` - remove(BookmarkSubArgs) + remove(BookmarkSubArgs), } #[derive(Debug, Clone, Args)] diff --git a/src/config.rs b/src/config.rs index 03775ce..250ecfc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -31,14 +31,18 @@ pub struct GeneralSettings { pub show_stack_on_bookmark: bool, } +#[allow(dead_code)] +#[derive(Debug, Clone)] +pub struct FormatSettings { + pub stack_separator: String, + pub bookmarks_separator: String, +} + #[allow(dead_code)] #[derive(Debug, Clone)] pub struct StyleSettings { - pub note: String, - pub warning: String, - pub error: String, pub stack_number: String, - pub stack_seperator: String, + pub stack_separator: String, pub stack_path: String, pub bookmarks_key: String, pub bookmarks_seperator: String, @@ -60,11 +64,8 @@ impl Config { show_stack_on_bookmark: false, }, styles: StyleSettings { - note: "".to_owned(), - warning: "".to_owned(), - error: "".to_owned(), stack_number: "".to_owned(), - stack_seperator: "".to_owned(), + stack_separator: "".to_owned(), stack_path: "".to_owned(), bookmarks_key: "".to_owned(), bookmarks_seperator: "".to_owned(), @@ -93,5 +94,4 @@ impl Config { fn build_config(&mut self) -> Result<()> { Ok(()) } - } diff --git a/src/main.rs b/src/main.rs index 23d4ea2..a92ce1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,7 +86,15 @@ fn handle_push(args: &PushArgs, _config: &Config, stack: &mut Stack) -> Result<( } fn handle_pop(args: &PopArgs, _config: &Config, stack: &mut Stack) -> Result<()> { - let path = stack.pop_entry(args.num_entries)?; + let mut num : Option = None; + if let Some(a) = &args.action { + match a { + PopAction::all => num = Some(0), + } + } else if let Some(n) = &args.num_entries { + num = Some(*n); + } + let path = stack.pop_entry(num)?; println!( "cd -- {}", match path.to_str() { @@ -111,11 +119,11 @@ fn handle_stack(args: &StackArgs, config: &Config, stack: &mut Stack) -> Result< fn handle_bookmark(args: &BookmarkArgs, config: &Config, bookmarks: &mut Bookmarks, stack: &mut Stack) -> Result<()> { // if args.bookmark_action.is_some() { - if args.bookmark_action.is_some() { - match args.bookmark_action.clone().unwrap() { + if let Some(action) = &args.bookmark_action { + match action { BookmarkAction::list(_) => list_bookmarks(config, bookmarks)?, - BookmarkAction::add(args) => add_bookmarks(&args, config, bookmarks)?, - BookmarkAction::remove(args) => remove_bookmarks(&args, config, bookmarks)?, + BookmarkAction::add(args) => add_bookmarks(args, config, bookmarks)?, + BookmarkAction::remove(args) => remove_bookmarks(args, config, bookmarks)?, }; } else if args.name.is_some() { let path = match bookmarks.get_bookmarks().get(args.name.as_ref().unwrap()) { diff --git a/src/stack.rs b/src/stack.rs index 038533f..889581c 100644 --- a/src/stack.rs +++ b/src/stack.rs @@ -43,7 +43,8 @@ impl Stack { fs::remove_file(self.path.clone())?; print!( "echo '{}stack cleared successfully.{}'", - config.settings.styles.note, config.settings.styles.reset + "", + "" // TODO implement styling ); Ok(()) } @@ -61,12 +62,10 @@ impl Stack { /// return popped entry pub fn pop_entry(&mut self, num_entries: Option) -> Result { let mut num = num_entries.unwrap_or(1); - if num < 1 { - num = 1; - } else if num > self.stack.len() { + if num == 0 || num > self.stack.len() { num = self.stack.len(); } - let mut dropped_entries = self.stack.drain((self.stack.len()-num)..); + let mut dropped_entries = self.stack.drain((self.stack.len() - num)..); let entry = dropped_entries.nth(0); drop(dropped_entries); self.write_stack_file()?; diff --git a/todo.md b/todo.md index 9fabb9a..2e89c90 100644 --- a/todo.md +++ b/todo.md @@ -1,11 +1,12 @@ # todos for 'navigation' - [x] replace `expect` statements in 'stack.rs' with actual error handling -- [x] pop several entries at a time +- [x] option to pop several entries at a time and option to pop the entire stack - [x] drop stack - [ ] config file - [ ] dedup stack option - [ ] parse config file - [ ] apply config - [ ] colored output > make it configurable through config file + - [ ] setting for separator string when displaying stack/bookmarks - [x] bookmarks