diff --git a/src/bookmarks.rs b/src/bookmarks.rs index 1e642a2..3b4dc8b 100644 --- a/src/bookmarks.rs +++ b/src/bookmarks.rs @@ -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", diff --git a/src/config.rs b/src/config.rs index 598b1de..3b295f2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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 diff --git a/src/main.rs b/src/main.rs index c4a71c1..492bbb4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(¤t_path)?; if config.general.show_stack_on_push { output.push_info(&stack.to_formatted_string(&config)?); diff --git a/src/stack.rs b/src/stack.rs index db8259d..ba9e839 100644 --- a/src/stack.rs +++ b/src/stack.rs @@ -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 { 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> { - 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( diff --git a/src/util.rs b/src/util.rs index 6919965..16a68c2 100644 --- a/src/util.rs +++ b/src/util.rs @@ -3,7 +3,8 @@ use std::{ path::{Component, PathBuf}, }; -pub fn to_lexical_absolute(path: PathBuf) -> Result { +/// 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 { component @ _ => absolute.push(component.as_os_str()), } } - Ok(absolute) + *path = absolute; + Ok(()) } + diff --git a/todo.md b/todo.md index e3a38d1..5932743 100644 --- a/todo.md +++ b/todo.md @@ -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 :)