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>,
/// change to <path>
pub path: Option<PathBuf>,
pub path: Option<String>,
}
#[derive(Debug, Clone, Args)]
+23 -9
View File
@@ -11,10 +11,12 @@ use clap::Parser;
use config::*;
use bookmarks::*;
use config_parser::*;
use dirs::home_dir;
use output::Output;
use stack::Stack;
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::path::{Path, PathBuf};
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<()> {
let path = match args.path.clone() {
const PREFIX: char = '=';
let mut path_string = match args.path.clone() {
Some(value) => value,
None => {
let home_dir = match var("HOME") {
Ok(value) => value,
Err(error) => return Err(Error::other(error.to_string())),
};
match PathBuf::from_str(&home_dir) {
Ok(value) => value,
Err(error) => return Err(Error::other(error.to_string())),
String::new()
}
};
let path: PathBuf = if path_string.is_empty() {
match home_dir() {
Some(value) => value,
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)?;
+7 -8
View File
@@ -1,6 +1,6 @@
#![allow(dead_code)]
use std::fs;
use std::{fs, usize};
use std::fs::File;
use std::io::{Error, Result};
use std::path::{Path, PathBuf};
@@ -112,13 +112,12 @@ impl Stack {
/// return nth last entry
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
let index = match self.stack.len().checked_sub(entry_number) {
Some(value) => value,
None => return Err(Error::other("-- no entry found at request index")),
};
match self.stack.get(index) {
Some(item) => Ok(item),
None => Err(Error::other("-- failed to retrieve stack entry by number")),
if entry_number >= self.stack.len() {
return Err(Error::other(format!("-- requested item ({entry_number}) out of bounds (stack.len() = {})", self.stack.len())));
}
match self.stack.iter().rev().nth(entry_number) {
Some(value) => Ok(value),
None => return Err(Error::other("-- failed to retrieve stack element #{entry_number}")),
}
}
+1 -1
View File
@@ -15,7 +15,7 @@
- [x] color option for punctuation (mostly '/')
- [x] 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] change config file extension to `.toml`
- [x] add bash completions