implemented push to stack entry by number

This commit is contained in:
2025-01-27 23:57:48 +01:00
parent 7b60a8a3a9
commit f4676eb2c3
4 changed files with 33 additions and 20 deletions
+1 -1
View File
@@ -39,7 +39,7 @@ pub struct PushArgs {
pub show_stack: Option<bool>, pub show_stack: Option<bool>,
/// change to <path> /// change to <path>
pub path: Option<PathBuf>, pub path: Option<String>,
} }
#[derive(Debug, Clone, Args)] #[derive(Debug, Clone, Args)]
+24 -10
View File
@@ -11,10 +11,12 @@ use clap::Parser;
use config::*; use config::*;
use bookmarks::*; use bookmarks::*;
use config_parser::*; use config_parser::*;
use dirs::home_dir;
use output::Output; use output::Output;
use stack::Stack; use stack::Stack;
use util::to_rooted; use util::to_rooted;
use std::env::{current_dir, var}; use std::char;
use std::env::current_dir;
use std::io::{Error, Result}; use std::io::{Error, Result};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
@@ -72,17 +74,29 @@ fn main() -> Result<()> {
} }
fn handle_push(args: &PushArgs, config: &Config, stack: &mut Stack, output: &mut Output) -> Result<()> { fn handle_push(args: &PushArgs, config: &Config, stack: &mut Stack, output: &mut Output) -> Result<()> {
let path = match args.path.clone() { const PREFIX: char = '=';
let mut path_string = match args.path.clone() {
Some(value) => value, Some(value) => value,
None => { None => {
let home_dir = match var("HOME") { String::new()
Ok(value) => value, }
Err(error) => return Err(Error::other(error.to_string())), };
}; let path: PathBuf = if path_string.is_empty() {
match PathBuf::from_str(&home_dir) { match home_dir() {
Ok(value) => value, Some(value) => value,
Err(error) => return Err(Error::other(error.to_string())), None => return Err(Error::other("-- failed to determine home directory")),
} }
} else if path_string.starts_with(PREFIX) {
path_string = path_string.trim_start_matches(PREFIX).to_string();
let number: usize = match path_string.parse() {
Ok(value) => value,
Err(_) => return Err(Error::other("-- push : failed to convert path argument to number")),
};
stack.get_entry_by_number(number)?.to_path_buf()
} else {
match PathBuf::from_str(&path_string) {
Ok(value) => value,
Err(_) => return Err(Error::other("-- failed to create PathBuf from argument")),
} }
}; };
push_path(&path, stack, config, output)?; push_path(&path, stack, config, output)?;
+7 -8
View File
@@ -1,6 +1,6 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::fs; use std::{fs, usize};
use std::fs::File; use std::fs::File;
use std::io::{Error, Result}; use std::io::{Error, Result};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@@ -112,13 +112,12 @@ impl Stack {
/// return nth last entry /// return nth last entry
pub fn get_entry_by_number(&mut self, entry_number: usize) -> Result<&PathBuf> { pub fn get_entry_by_number(&mut self, entry_number: usize) -> Result<&PathBuf> {
// index from the end of the vector as new entries are appended at the end of the list // index from the end of the vector as new entries are appended at the end of the list
let index = match self.stack.len().checked_sub(entry_number) { if entry_number >= self.stack.len() {
Some(value) => value, return Err(Error::other(format!("-- requested item ({entry_number}) out of bounds (stack.len() = {})", self.stack.len())));
None => return Err(Error::other("-- no entry found at request index")), }
}; match self.stack.iter().rev().nth(entry_number) {
match self.stack.get(index) { Some(value) => Ok(value),
Some(item) => Ok(item), None => return Err(Error::other("-- failed to retrieve stack element #{entry_number}")),
None => Err(Error::other("-- failed to retrieve stack entry by number")),
} }
} }
+1 -1
View File
@@ -15,7 +15,7 @@
- [x] color option for punctuation (mostly '/') - [x] color option for punctuation (mostly '/')
- [x] bookmarks - [x] bookmarks
- [x] do not resolve links in bookmarks - [x] do not resolve links in bookmarks
- [ ] push <number> to push path in stack - [x] push <number> to push path in stack
- [x] write documentation - [x] write documentation
- [x] change config file extension to `.toml` - [x] change config file extension to `.toml`
- [x] add bash completions - [x] add bash completions