implemented option 'all' to pop the entire stack
This commit is contained in:
+14
-4
@@ -1,6 +1,5 @@
|
|||||||
use clap::{Args, Parser, Subcommand};
|
use clap::{Args, Parser, Subcommand};
|
||||||
use std::{path::PathBuf};
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
|
||||||
/// implements stack for cd wrapper script
|
/// implements stack for cd wrapper script
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
@@ -43,14 +42,25 @@ pub struct PushArgs {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct PopArgs {
|
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
|
/// show stack
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
pub show_stack: Option<bool>,
|
pub show_stack: Option<bool>,
|
||||||
|
|
||||||
/// pop multiple entries and navigate to last retrieved path
|
/// pop specified number of entries
|
||||||
pub num_entries: Option<usize>,
|
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)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct StackArgs {
|
pub struct StackArgs {
|
||||||
/// hide entry numbers
|
/// hide entry numbers
|
||||||
@@ -93,7 +103,7 @@ pub enum BookmarkAction {
|
|||||||
add(BookmarkSubArgs),
|
add(BookmarkSubArgs),
|
||||||
|
|
||||||
/// remove a bookmark by name `book remove <name>`
|
/// remove a bookmark by name `book remove <name>`
|
||||||
remove(BookmarkSubArgs)
|
remove(BookmarkSubArgs),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
|
|||||||
+9
-9
@@ -31,14 +31,18 @@ pub struct GeneralSettings {
|
|||||||
pub show_stack_on_bookmark: bool,
|
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)]
|
#[allow(dead_code)]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct StyleSettings {
|
pub struct StyleSettings {
|
||||||
pub note: String,
|
|
||||||
pub warning: String,
|
|
||||||
pub error: String,
|
|
||||||
pub stack_number: String,
|
pub stack_number: String,
|
||||||
pub stack_seperator: String,
|
pub stack_separator: String,
|
||||||
pub stack_path: String,
|
pub stack_path: String,
|
||||||
pub bookmarks_key: String,
|
pub bookmarks_key: String,
|
||||||
pub bookmarks_seperator: String,
|
pub bookmarks_seperator: String,
|
||||||
@@ -60,11 +64,8 @@ impl Config {
|
|||||||
show_stack_on_bookmark: false,
|
show_stack_on_bookmark: false,
|
||||||
},
|
},
|
||||||
styles: StyleSettings {
|
styles: StyleSettings {
|
||||||
note: "".to_owned(),
|
|
||||||
warning: "".to_owned(),
|
|
||||||
error: "".to_owned(),
|
|
||||||
stack_number: "".to_owned(),
|
stack_number: "".to_owned(),
|
||||||
stack_seperator: "".to_owned(),
|
stack_separator: "".to_owned(),
|
||||||
stack_path: "".to_owned(),
|
stack_path: "".to_owned(),
|
||||||
bookmarks_key: "".to_owned(),
|
bookmarks_key: "".to_owned(),
|
||||||
bookmarks_seperator: "".to_owned(),
|
bookmarks_seperator: "".to_owned(),
|
||||||
@@ -93,5 +94,4 @@ impl Config {
|
|||||||
fn build_config(&mut self) -> Result<()> {
|
fn build_config(&mut self) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-5
@@ -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<()> {
|
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!(
|
println!(
|
||||||
"cd -- {}",
|
"cd -- {}",
|
||||||
match path.to_str() {
|
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<()> {
|
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() {
|
||||||
if args.bookmark_action.is_some() {
|
if let Some(action) = &args.bookmark_action {
|
||||||
match args.bookmark_action.clone().unwrap() {
|
match action {
|
||||||
BookmarkAction::list(_) => list_bookmarks(config, bookmarks)?,
|
BookmarkAction::list(_) => list_bookmarks(config, bookmarks)?,
|
||||||
BookmarkAction::add(args) => add_bookmarks(&args, config, bookmarks)?,
|
BookmarkAction::add(args) => add_bookmarks(args, config, bookmarks)?,
|
||||||
BookmarkAction::remove(args) => remove_bookmarks(&args, config, bookmarks)?,
|
BookmarkAction::remove(args) => remove_bookmarks(args, config, bookmarks)?,
|
||||||
};
|
};
|
||||||
} else if args.name.is_some() {
|
} else if args.name.is_some() {
|
||||||
let path = match bookmarks.get_bookmarks().get(args.name.as_ref().unwrap()) {
|
let path = match bookmarks.get_bookmarks().get(args.name.as_ref().unwrap()) {
|
||||||
|
|||||||
+3
-4
@@ -43,7 +43,8 @@ impl Stack {
|
|||||||
fs::remove_file(self.path.clone())?;
|
fs::remove_file(self.path.clone())?;
|
||||||
print!(
|
print!(
|
||||||
"echo '{}stack cleared successfully.{}'",
|
"echo '{}stack cleared successfully.{}'",
|
||||||
config.settings.styles.note, config.settings.styles.reset
|
"",
|
||||||
|
"" // TODO implement styling
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -61,9 +62,7 @@ impl Stack {
|
|||||||
/// return popped entry
|
/// return popped entry
|
||||||
pub fn pop_entry(&mut self, num_entries: Option<usize>) -> Result<PathBuf> {
|
pub fn pop_entry(&mut self, num_entries: Option<usize>) -> Result<PathBuf> {
|
||||||
let mut num = num_entries.unwrap_or(1);
|
let mut num = num_entries.unwrap_or(1);
|
||||||
if num < 1 {
|
if num == 0 || num > self.stack.len() {
|
||||||
num = 1;
|
|
||||||
} else if num > self.stack.len() {
|
|
||||||
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)..);
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
# todos for 'navigation'
|
# todos for 'navigation'
|
||||||
|
|
||||||
- [x] replace `expect` statements in 'stack.rs' with actual error handling
|
- [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
|
- [x] drop stack
|
||||||
- [ ] config file
|
- [ ] config file
|
||||||
- [ ] dedup stack option
|
- [ ] dedup stack option
|
||||||
- [ ] parse config file
|
- [ ] parse config file
|
||||||
- [ ] apply config
|
- [ ] apply config
|
||||||
- [ ] colored output > make it configurable through config file
|
- [ ] colored output > make it configurable through config file
|
||||||
|
- [ ] setting for separator string when displaying stack/bookmarks
|
||||||
- [x] bookmarks
|
- [x] bookmarks
|
||||||
|
|||||||
Reference in New Issue
Block a user