implemented (separate) punctuation-style for stack and bookmarks

This commit is contained in:
2025-01-15 00:11:54 +01:00
parent b0e2e430b5
commit 39ad2de67c
7 changed files with 38 additions and 145 deletions
+3 -2
View File
@@ -9,7 +9,7 @@ use std::str::FromStr;
use dirs::config_dir; use dirs::config_dir;
use super::config::*; use super::config::*;
use config_parser::{make_padding_string, apply_format}; use config_parser::{apply_format, make_padding_string, RESET_ARG, RESET_SEQ};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Bookmarks { pub struct Bookmarks {
@@ -111,7 +111,8 @@ impl Bookmarks {
&config.format.bookmarks_separator, &config.format.bookmarks_separator,
&config.styles.bookmarks_seperator_style, &config.styles.bookmarks_seperator_style,
); );
let path = apply_format(path.to_str().unwrap(), &config.styles.bookmarks_path_style); let mut path = apply_format(path.to_str().unwrap(), &config.styles.bookmarks_path_style);
path = path.replace('/', &format!("{}/{}", config.styles.bookmarks_punct_style, RESET_SEQ));
if config.format.align_separators { if config.format.align_separators {
buffer.push_str(&format!("{}{}{}{}\n", name, padding, separator, path)); buffer.push_str(&format!("{}{}{}{}\n", name, padding, separator, path));
} else { } else {
+6
View File
@@ -45,11 +45,15 @@ pub struct StyleSettings {
#[style_config] #[style_config]
pub stack_path_style: String, pub stack_path_style: String,
#[style_config] #[style_config]
pub stack_punct_style: String,
#[style_config]
pub bookmarks_name_style: String, pub bookmarks_name_style: String,
#[style_config] #[style_config]
pub bookmarks_seperator_style: String, pub bookmarks_seperator_style: String,
#[style_config] #[style_config]
pub bookmarks_path_style: String, pub bookmarks_path_style: String,
#[style_config]
pub bookmarks_punct_style: String,
} }
impl Config { impl Config {
@@ -74,9 +78,11 @@ impl Config {
stack_number_style: String::new(), stack_number_style: String::new(),
stack_separator_style: String::new(), stack_separator_style: String::new(),
stack_path_style: String::new(), stack_path_style: String::new(),
stack_punct_style: String::new(),
bookmarks_name_style: String::new(), bookmarks_name_style: String::new(),
bookmarks_seperator_style: String::new(), bookmarks_seperator_style: String::new(),
bookmarks_path_style: String::new(), bookmarks_path_style: String::new(),
bookmarks_punct_style: String::new(),
}, },
}; };
// get configuration directory // get configuration directory
+2 -1
View File
@@ -3,6 +3,7 @@ mod config;
mod bookmarks; mod bookmarks;
mod stack; mod stack;
mod output; mod output;
mod util;
mod debug; mod debug;
use arguments::*; use arguments::*;
@@ -173,7 +174,7 @@ fn add_bookmarks(args: &BookmarkSubArgs, config: &Config, bookmarks: &mut Bookma
fn remove_bookmarks(args: &BookmarkSubArgs, config: &Config, bookmarks: &mut Bookmarks, output: &mut Output) -> Result<()> { fn remove_bookmarks(args: &BookmarkSubArgs, config: &Config, bookmarks: &mut Bookmarks, output: &mut Output) -> Result<()> {
bookmarks.remove_bookmark(&args.name)?; bookmarks.remove_bookmark(&args.name)?;
if config.general.show_books_on_bookmark { if config.general.show_books_on_bookmark {
output.push_info(&bookmarks.to_formatted_string(config)?); output.push_info(&bookmarks.to_formatted_string(config)?);
} else { } else {
+6 -3
View File
@@ -5,11 +5,11 @@ use std::fs::File;
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;
use config_parser::{RESET_SEQ, STYLES};
use sysinfo::{Pid, System}; use sysinfo::{Pid, System};
use crate::make_padding_string; use crate::make_padding_string;
use super::{apply_format, config::*};
use super::{apply_format, config::*, output::Output};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Stack { pub struct Stack {
@@ -46,7 +46,8 @@ impl Stack {
&config.format.stack_separator, &config.format.stack_separator,
&config.styles.stack_separator_style, &config.styles.stack_separator_style,
); );
let path = apply_format(item.to_str().unwrap(), &config.styles.stack_path_style); let mut path = apply_format(item.to_str().unwrap(), &config.styles.stack_path_style);
path = path.replace('/', &format!("{}/{}", config.styles.stack_punct_style, RESET_SEQ));
if config.format.align_separators { if config.format.align_separators {
buffer.push_str(&format!("{}{}{}{}\n", number, padding, separator, path)); buffer.push_str(&format!("{}{}{}{}\n", number, padding, separator, path));
} else { } else {
@@ -67,6 +68,8 @@ impl Stack {
/// returns updated stack /// returns updated stack
pub fn push_entry(&mut self, path: &Path) -> Result<&Vec<PathBuf>> { pub fn push_entry(&mut self, path: &Path) -> Result<&Vec<PathBuf>> {
let abs_path = path.canonicalize()?; 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); self.stack.push(abs_path);
self.write_stack_file()?; self.write_stack_file()?;
Ok(&self.stack) Ok(&self.stack)
-137
View File
@@ -1,137 +0,0 @@
#![allow(dead_code)]
use dirs::config_dir;
use std::collections::HashMap;
use std::fs;
use std::io::{Error, Result};
use std::path::PathBuf;
const DEFAULT_SETTINGS: &[&str] = &[
"show_stack_on_push=false",
"show_stack_on_pop=false",
"show_stack_on_bookmark=false",
"stack_separator= - ",
"bookmark_separator= - ",
"stack_number=default, bold",
"stack_separator=cyan",
"stack_path=default",
"bookmark_name=default",
"bookmark_separator=cyan",
"bookmark_path=default",
];
#[derive(Debug, Clone)]
pub struct Config {
conf_file_path: PathBuf,
pub settings: HashMap<&'static str, &'static str>,
}
impl Config {
const CONFIG_DIR_NAME: &str = "navigate/";
const CONFIG_FILE_NAME: &str = "navigate.conf";
pub fn new() -> Result<Self> {
let mut settings = HashMap::<&str, &str>::new();
// fill the hashmaps with the defined settings and their default values
for item in DEFAULT_SETTINGS {
let tokens = item.split(['=']).collect::<Vec<&str>>();
if tokens.len() != 2 {
panic!("-- fix default format settings")
}
settings.insert(tokens.first().unwrap(), tokens.last().unwrap());
}
let mut config = Config {
conf_file_path: PathBuf::new(),
settings,
};
// get configuration directory
config.conf_file_path = match config_dir() {
Some(value) => value,
None => {
return Err(Error::other(
"-- failed to retrieve configuration directory",
))
}
};
// expand path to configuration file
config.conf_file_path.push(format!(
"{}{}",
Self::CONFIG_DIR_NAME,
Self::CONFIG_FILE_NAME,
));
// parse configuration file and populate config struct
if config.parse_config().is_err() {
config.write_config()?;
}
// config.parse_color_settings()?;
Ok(config)
}
/// formats and prints config to string
pub fn to_formatted_string(&self) -> Result<String> {
// TODO implement
Ok("hi".to_owned())
}
/// parse config file
fn parse_config(&mut self) -> Result<()> {
if !self.conf_file_path.is_file() {
return Err(Error::other("-- config file does not exist"));
}
let config = match fs::read_to_string(&self.conf_file_path.clone()) {
Ok(value) => value,
Err(error) => return Err(error),
};
for line in config.lines() {
// ignore comments
if line.starts_with("#") {
continue;
}
let token = line.split(['=']).collect::<Vec<&str>>();
let key = match token.first() {
Some(value) => value,
None => return Err(Error::other(format!("-- failed to parse '{}'", line))),
};
let value = match token.last() {
Some(value) => value,
None => return Err(Error::other(format!("-- failed to parse '{}'", line))),
};
if self.settings.contains_key(key) {
if let Some(entry) = self.settings.get_mut(key) {
*entry = value.clone();
}
} else {
println!("-- ignored unknown setting : {}", line);
}
}
// self.settings.entry(key).and_modify(|entry| *entry = value);
Ok(())
}
/// write configuration file
fn write_config(&self) -> Result<()> {
let mut buffer = String::new();
buffer.push_str("# `navigate` settings\n");
for (name, value) in self.settings.clone() {
buffer.push_str(&format!("{}={}\n", name, value));
}
buffer.push('\n');
if fs::write(self.conf_file_path.clone(), buffer).is_err() {
return Err(Error::other("-- failed to write configuration file"));
}
Ok(())
}
}
+19
View File
@@ -0,0 +1,19 @@
use std::{
io::Result,
path::{Component, PathBuf},
};
pub fn to_lexical_absolute(path: PathBuf) -> Result<PathBuf> {
let mut absolute = if path.is_absolute() {
std::path::PathBuf::new()
} else {
std::env::current_dir()?
}; for component in path.components() {
match component {
Component::CurDir => {},
Component::ParentDir => { absolute.pop(); },
component @ _ => absolute.push(component.as_os_str()),
}
}
Ok(absolute)
}
+2 -2
View File
@@ -10,8 +10,8 @@
- [ ] dedup stack option - [ ] dedup stack option
- [x] parse config file - [x] parse config file
- [ ] apply config -- partially more done than before :) - [ ] apply config -- partially more done than before :)
- [ ] `show-bookmarks-on-book` - [x] `show-bookmarks-on-book`
- [x] setting for separator string when displaying stack/bookmarks - [x] setting for separator string when displaying stack/bookmarks
- [ ] color option for punctuation (mostly '/') - [x] color option for punctuation (mostly '/')
- [x] bookmarks - [x] bookmarks
- [ ] do not resolve links in bookmarks - [ ] do not resolve links in bookmarks