bookmarks and stack can now hold paths with links

This commit is contained in:
2025-01-15 23:06:24 +01:00
parent 39ad2de67c
commit db8c62c476
6 changed files with 43 additions and 34 deletions
+6 -3
View File
@@ -8,8 +8,8 @@ use std::path::PathBuf;
use std::str::FromStr;
use dirs::config_dir;
use super::config::*;
use config_parser::{apply_format, make_padding_string, RESET_ARG, RESET_SEQ};
use super::{config::*, util::to_rooted};
use config_parser::{apply_format, make_padding_string, RESET_SEQ};
#[derive(Debug, Clone)]
pub struct Bookmarks {
@@ -44,10 +44,11 @@ impl Bookmarks {
continue;
}
let key: String = String::from(tokens[0]);
let path = match PathBuf::from_str(tokens[1]) {
let mut path: PathBuf = match PathBuf::from_str(tokens[1]) {
Ok(value) => value,
Err(err) => return Err(Error::other(err.to_string())),
};
to_rooted(&mut path)?;
if !path.is_dir() {
continue;
}
@@ -69,6 +70,8 @@ impl Bookmarks {
/// adds a key/value pair to bookmarks and writes the bookmarks file
pub fn add_bookmark(&mut self, name: &String, path: &PathBuf) -> Result<()> {
let mut path = path.to_path_buf();
to_rooted(&mut path)?;
if !path.is_dir() {
return Err(Error::other(
"-- provided path argument does not point to a valid directory",
+18 -14
View File
@@ -27,9 +27,11 @@ pub struct GeneralSettings {
#[derive(Debug, Clone, Default, ConfigParser)]
pub struct FormatSettings {
pub stack_separator: String,
pub bookmarks_separator: String,
pub align_separators: bool,
pub stack_separator: String,
pub stack_home_as_tilde: bool,
pub bookmarks_separator: String,
pub book_home_as_tilde: bool,
}
#[derive(Debug, Clone, Default, ConfigParser)]
@@ -68,21 +70,23 @@ impl Config {
show_books_on_bookmark: false,
},
format: FormatSettings {
bookmarks_separator: String::new(),
stack_separator: String::new(),
align_separators: false,
stack_separator: " - ".to_owned(),
stack_home_as_tilde: true,
bookmarks_separator: " - ".to_owned(),
book_home_as_tilde: true,
},
styles: StyleSettings {
warning_style: String::new(),
error_style: String::new(),
stack_number_style: String::new(),
stack_separator_style: String::new(),
stack_path_style: String::new(),
stack_punct_style: String::new(),
bookmarks_name_style: String::new(),
bookmarks_seperator_style: String::new(),
bookmarks_path_style: String::new(),
bookmarks_punct_style: String::new(),
warning_style: "default".to_owned(),
error_style: "default".to_owned(),
stack_number_style: "default".to_owned(),
stack_separator_style: "default".to_owned(),
stack_path_style: "default".to_owned(),
stack_punct_style: "default".to_owned(),
bookmarks_name_style: "default".to_owned(),
bookmarks_seperator_style: "default".to_owned(),
bookmarks_path_style: "default".to_owned(),
bookmarks_punct_style: "default".to_owned(),
},
};
// get configuration directory
+2 -6
View File
@@ -153,14 +153,10 @@ fn list_bookmarks(config: &Config, bookmarks: &mut Bookmarks, output: &mut Outpu
}
fn add_bookmarks(args: &BookmarkSubArgs, config: &Config, bookmarks: &mut Bookmarks, output: &mut Output) -> Result<()> {
let mut path = match args.path.clone() {
let path = match args.path.clone() {
Some(value) => value,
None => return Err(Error::other("-- missing path argument")),
};
path = match path.canonicalize() {
Ok(value) => value,
Err(error) => return Err(Error::other(error.to_string())),
};
bookmarks.add_bookmark(&args.name, &path)?;
if config.general.show_books_on_bookmark {
@@ -188,7 +184,7 @@ fn push_path(path: &Path, stack: &mut Stack, config: &Config, output: &mut Outpu
if !path.is_dir() {
return Err(Error::other("-- invalid path argument"));
}
let current_path = current_dir()?;
let current_path: PathBuf = current_dir()?;
stack.push_entry(&current_path)?;
if config.general.show_stack_on_push {
output.push_info(&stack.to_formatted_string(&config)?);
+10 -7
View File
@@ -5,11 +5,11 @@ use std::fs::File;
use std::io::{Error, Result};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use config_parser::{RESET_SEQ, STYLES};
use config_parser::RESET_SEQ;
use sysinfo::{Pid, System};
use crate::make_padding_string;
use super::{apply_format, config::*};
use super::{apply_format, config::*, util::to_rooted};
#[derive(Debug, Clone)]
pub struct Stack {
@@ -19,6 +19,8 @@ pub struct Stack {
}
impl Stack {
const STACK_FILE_DIRECTORY: &str = "/tmp/navigate/";
pub fn new(process_id: u32) -> Result<Self> {
let mut stack: Stack = Stack {
pid: process_id,
@@ -67,10 +69,11 @@ impl Stack {
/// push entry to stack
/// returns updated stack
pub fn push_entry(&mut self, path: &Path) -> Result<&Vec<PathBuf>> {
let abs_path = path.canonicalize()?;
//let abs_path = super::util::to_lexical_absolute(path.to_path_buf())?; TODO: fix paths and
//stuff
self.stack.push(abs_path);
let mut path: PathBuf = path.to_path_buf();
to_rooted(&mut path)?;
// append path to stack and write stack file to save changes
self.stack.push(path);
self.write_stack_file()?;
Ok(&self.stack)
}
@@ -110,7 +113,7 @@ impl Stack {
/// clean up dead stack files, parse and build stack
fn build_stack(&mut self) -> Result<()> {
let stack_dir: PathBuf = match PathBuf::from_str("/tmp/navigation/") {
let stack_dir: PathBuf = match PathBuf::from_str(Self::STACK_FILE_DIRECTORY) {
Ok(value) => value,
Err(_) => {
return Err(Error::other(
+5 -2
View File
@@ -3,7 +3,8 @@ use std::{
path::{Component, PathBuf},
};
pub fn to_lexical_absolute(path: PathBuf) -> Result<PathBuf> {
/// returns absolute path but keeps links
pub fn to_rooted(path: &mut PathBuf) -> Result<()> {
let mut absolute = if path.is_absolute() {
std::path::PathBuf::new()
} else {
@@ -15,5 +16,7 @@ pub fn to_lexical_absolute(path: PathBuf) -> Result<PathBuf> {
component @ _ => absolute.push(component.as_os_str()),
}
}
Ok(absolute)
*path = absolute;
Ok(())
}
+2 -2
View File
@@ -5,8 +5,8 @@
- [x] drop stack
- [x] config file
- [x] implement procedural macro for config
- [x] to parse config
- [ ] to write default config
- [x] .. to parse config
- [ ] .. to write default config
- [ ] dedup stack option
- [x] parse config file
- [ ] apply config -- partially more done than before :)