implemented option 'all' to pop the entire stack

This commit is contained in:
2024-12-08 12:54:36 +01:00
parent e3d43755b1
commit 61e3ded722
5 changed files with 42 additions and 24 deletions
+14 -4
View File
@@ -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<PopAction>,
/// show stack
#[arg(short, long)]
pub show_stack: Option<bool>,
/// pop multiple entries and navigate to last retrieved path
/// pop specified number of entries
pub num_entries: Option<usize>,
}
#[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 <name>`
remove(BookmarkSubArgs)
remove(BookmarkSubArgs),
}
#[derive(Debug, Clone, Args)]
+9 -9
View File
@@ -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(())
}
}
+13 -5
View File
@@ -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<usize> = 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()) {
+4 -5
View File
@@ -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<usize>) -> Result<PathBuf> {
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()?;
+2 -1
View File
@@ -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