implemented displaying invalid path and according styles
This commit is contained in:
+2
-3
@@ -2,7 +2,6 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// implements stack for cd wrapper script
|
||||
#[derive(Parser, Debug)]
|
||||
@@ -32,7 +31,7 @@ pub enum Action {
|
||||
bookmark(BookmarkArgs),
|
||||
|
||||
/// display current configuartion (mostly for debugging)
|
||||
configuration(ConfigArgs),
|
||||
configuration,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Args)]
|
||||
@@ -105,7 +104,7 @@ pub enum BookmarkAction {
|
||||
remove(BookmarkSubArgs),
|
||||
|
||||
/// get bookmarknames for shell completions
|
||||
names(EmptyArgs),
|
||||
completions(EmptyArgs),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Args)]
|
||||
|
||||
+34
-18
@@ -9,7 +9,7 @@ use std::str::FromStr;
|
||||
use dirs::{config_dir, home_dir};
|
||||
|
||||
use super::{config::*, util::to_rooted};
|
||||
use config_parser::{apply_format, generate_style_sequence, make_padding_string, RESET_SEQ, STYLES};
|
||||
use config_parser::{apply_format, make_padding_string};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Bookmarks {
|
||||
@@ -49,9 +49,11 @@ impl Bookmarks {
|
||||
Err(err) => return Err(Error::other(err.to_string())),
|
||||
};
|
||||
to_rooted(&mut path)?;
|
||||
if !path.is_dir() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// if !path.is_dir() {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
bookmarks.bookmarks.insert(key, path);
|
||||
}
|
||||
Ok(bookmarks)
|
||||
@@ -110,17 +112,14 @@ impl Bookmarks {
|
||||
Some(value) => value,
|
||||
None => return Err(Error::other("-- failed to determine maximum bookmark name length")),
|
||||
};
|
||||
for (mark, path) in &self.bookmarks {
|
||||
let padding = make_padding_string(max_name_len - mark.len());
|
||||
let name = apply_format(mark, &config.styles.bookmarks_name_style);
|
||||
let separator = apply_format(
|
||||
&config.format.bookmarks_separator,
|
||||
&config.styles.bookmarks_seperator_style,
|
||||
);
|
||||
for (raw_name, raw_path) in &self.bookmarks {
|
||||
let padding: String = make_padding_string(max_name_len - raw_name.len());
|
||||
let mut name: String = raw_name.clone();
|
||||
let mut separator: String = config.format.bookmarks_separator.clone();
|
||||
let mut path: String = raw_path.clone().into_os_string().into_string().unwrap();
|
||||
|
||||
let mut path = apply_format(path.to_str().unwrap(), &config.styles.bookmarks_path_style);
|
||||
if config.format.show_home_as_tilde {
|
||||
let home = match home_dir() {
|
||||
let home: String = match home_dir() {
|
||||
Some(value) => match value.into_os_string().into_string() {
|
||||
Ok(value) => value,
|
||||
Err(error) => return Err(Error::other(format!("-- failed to conver home directory to string: {}", error.to_str().unwrap()))),
|
||||
@@ -129,13 +128,30 @@ impl Bookmarks {
|
||||
};
|
||||
path = path.replace(&home, "~");
|
||||
}
|
||||
path = path.replace('/', &format!("{}{}/{}{}", RESET_SEQ, config.styles.bookmarks_punct_style, RESET_SEQ, &config.styles.bookmarks_path_style));
|
||||
|
||||
if config.format.align_separators {
|
||||
buffer.push_str(&format!("{}{}{}{}\n", name, padding, separator, path));
|
||||
} else {
|
||||
buffer.push_str(&format!("{}{}{}{}\n", name, separator, padding, path));
|
||||
if raw_path.is_dir() {
|
||||
let slash: String = apply_format(&"/".to_owned(), &config.styles.bookmarks_punct_style)?;
|
||||
let mut segments: Vec<String> = path.split('/').map(|element| element.to_owned()).collect();
|
||||
for element in segments.iter_mut() {
|
||||
*element = apply_format(&element, &config.styles.bookmarks_path_style)?;
|
||||
}
|
||||
path = segments.join(&slash);
|
||||
|
||||
name = apply_format(&name, &config.styles.bookmarks_name_style)?;
|
||||
separator = apply_format(&separator, &config.styles.stack_separator_style)?;
|
||||
}
|
||||
|
||||
let mut line: String;
|
||||
if config.format.align_separators {
|
||||
line = format!("{}{}{}{}\n", name, padding, separator, path);
|
||||
} else {
|
||||
line = format!("{}{}{}{}\n", name, separator, padding, path);
|
||||
}
|
||||
if !raw_path.is_dir() {
|
||||
line = apply_format(&line, &config.styles.bookmarks_invalid_style)?;
|
||||
}
|
||||
|
||||
buffer.push_str(&line);
|
||||
}
|
||||
}
|
||||
Ok(buffer)
|
||||
|
||||
+42
-12
@@ -5,7 +5,12 @@
|
||||
|
||||
use dirs::config_dir;
|
||||
use std::fs;
|
||||
use std::io::{Error, Result};
|
||||
use std::{
|
||||
io::{
|
||||
Error, Result
|
||||
},
|
||||
path::PathBuf,
|
||||
};
|
||||
use config_parser::*;
|
||||
|
||||
#[derive(Debug, Clone, Default, ConfigParser)]
|
||||
@@ -28,9 +33,21 @@ pub struct GeneralSettings {
|
||||
#[default_value(false)]
|
||||
pub show_stack_on_pop: bool,
|
||||
|
||||
/// (bool) show book marks when adding, removing or changing to a bookmark
|
||||
/// (bool) show invalid stack entries
|
||||
#[default_value(false)]
|
||||
pub show_books_on_bookmark: bool,
|
||||
pub show_invalid_stack_entries: bool,
|
||||
|
||||
/// (bool) show bookmarks when adding, removing or changing to a bookmark
|
||||
#[default_value(false)]
|
||||
pub show_entries_on_bookmark: bool,
|
||||
|
||||
/// (bool) show invalid bookmarks when displaying bookmarks
|
||||
#[default_value(true)]
|
||||
pub show_invalid_bookmarks: bool,
|
||||
|
||||
/// (bool) remove invalid bookmarks on call
|
||||
#[default_value(false)]
|
||||
pub cleanup_bookmarks: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, ConfigParser)]
|
||||
@@ -88,6 +105,11 @@ pub struct StyleSettings {
|
||||
#[default_value("'magenta'")]
|
||||
pub stack_punct_style: String,
|
||||
|
||||
/// (string) style applied to punctuation (i.e. '/') when displaying the stack
|
||||
#[style_config]
|
||||
#[default_value("'default, strikethrough'")]
|
||||
pub stack_invalid_style: String,
|
||||
|
||||
/// (string) style applied to bookmark names when displaying the bookmarks
|
||||
#[style_config]
|
||||
#[default_value("'default'")]
|
||||
@@ -107,6 +129,11 @@ pub struct StyleSettings {
|
||||
#[style_config]
|
||||
#[default_value("'magenta'")]
|
||||
pub bookmarks_punct_style: String,
|
||||
|
||||
/// (string) style applied to punctuation (i.e. '/') when displaying the bookmarks
|
||||
#[style_config]
|
||||
#[default_value("'strikethrough'")]
|
||||
pub bookmarks_invalid_style: String,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@@ -123,23 +150,30 @@ impl Config {
|
||||
";
|
||||
|
||||
/// generates and populates a new instance of Config
|
||||
pub fn new(styles_as_ansi_sequences: bool) -> Result<Self> {
|
||||
pub fn new() -> Result<Self> {
|
||||
let mut config: Config = Self::default();
|
||||
// get configuration directory
|
||||
let mut config_file = match config_dir() {
|
||||
let mut config_file: PathBuf = match config_dir() {
|
||||
Some(value) => value,
|
||||
None => {
|
||||
return Err(Error::other("-- failed to retrieve configuration directory"))
|
||||
}
|
||||
};
|
||||
config_file.push(Self::CONFIG_DIRECTORY_NAME);
|
||||
if !config_file.is_dir() {
|
||||
if fs::create_dir(&config_file).is_err() {
|
||||
return Err(Error::other("-- failed to create a directory for the configuration files"));
|
||||
}
|
||||
}
|
||||
|
||||
// expand path to configuration file and default configuration file
|
||||
let mut default_file = config_file.clone();
|
||||
default_file.push(format!("{}/{}", Self::CONFIG_DIRECTORY_NAME, Self::DEFAULT_CONFIG_NAME));
|
||||
config_file.push(format!("{}/{}", Self::CONFIG_DIRECTORY_NAME, Self::CONFIG_FILE_NAME));
|
||||
default_file.push(Self::DEFAULT_CONFIG_NAME);
|
||||
config_file.push(Self::CONFIG_FILE_NAME);
|
||||
|
||||
// write default configuration file if it does not exist
|
||||
if !default_file.is_file() {
|
||||
let mut default_string = Self::DEFAULT_FILE_HEADER.to_string();
|
||||
let mut default_string = Self::DEFAULT_FILE_HEADER.to_owned();
|
||||
default_string.push_str(&config.to_string());
|
||||
_ = fs::write(&default_file, default_string);
|
||||
}
|
||||
@@ -161,10 +195,6 @@ impl Config {
|
||||
_ = config.parse_from_string(&default_config);
|
||||
}
|
||||
|
||||
if styles_as_ansi_sequences {
|
||||
config.to_ansi_sequences()?;
|
||||
}
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
|
||||
+7
-12
@@ -22,7 +22,7 @@ use std::str::FromStr;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut output = Output::new();
|
||||
let config = match Config::new(true) {
|
||||
let config = match Config::new() {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
// config object is not ready at this point so the style
|
||||
@@ -70,7 +70,7 @@ fn main() -> Result<()> {
|
||||
Action::pop(pop_args) => handle_pop(&pop_args, &config, &mut stack, &mut output),
|
||||
Action::stack(stack_args) => handle_stack(&stack_args, &config, &mut stack, &mut output),
|
||||
Action::bookmark(bookmark_args) => handle_bookmark(&bookmark_args, &config, &mut bookmarks, &mut stack, &mut output),
|
||||
Action::configuration(config_args) => handle_config(&config_args, &mut output),
|
||||
Action::configuration => handle_config(&mut output),
|
||||
};
|
||||
|
||||
if res.is_err() {
|
||||
@@ -159,13 +159,12 @@ fn handle_stack(args: &StackArgs, config: &Config, stack: &mut Stack, output: &m
|
||||
}
|
||||
|
||||
fn handle_bookmark(args: &BookmarkArgs, config: &Config, bookmarks: &mut Bookmarks, stack: &mut Stack, output: &mut Output) -> Result<()> {
|
||||
// if args.bookmark_action.is_some() {
|
||||
if let Some(action) = &args.bookmark_action {
|
||||
match action {
|
||||
BookmarkAction::list(_) => list_bookmarks(config, bookmarks, output)?,
|
||||
BookmarkAction::add(args) => add_bookmarks(args, config, bookmarks, output)?,
|
||||
BookmarkAction::remove(args) => remove_bookmarks(args, config, bookmarks, output)?,
|
||||
BookmarkAction::names(_) => println!("echo '{}'", bookmarks.get_bookmarknames()),
|
||||
BookmarkAction::completions(_) => println!("echo '{}'", bookmarks.get_bookmarknames()),
|
||||
};
|
||||
} else if args.name.is_some() { // handle `change to bookmark`
|
||||
let path = bookmarks.get_path_by_name(args.name.as_ref().unwrap())?;
|
||||
@@ -176,12 +175,8 @@ fn handle_bookmark(args: &BookmarkArgs, config: &Config, bookmarks: &mut Bookmar
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_config(args: &ConfigArgs, output: &mut Output) -> Result<()> {
|
||||
let convert: bool = match args.convert {
|
||||
Some(value) => value,
|
||||
None => false,
|
||||
};
|
||||
let config = Config::new(convert);
|
||||
fn handle_config(output: &mut Output) -> Result<()> {
|
||||
let config = Config::new();
|
||||
output.push_info(&format!("config = {:#?}", config));
|
||||
Ok(())
|
||||
}
|
||||
@@ -201,7 +196,7 @@ fn add_bookmarks(args: &BookmarkSubArgs, config: &Config, bookmarks: &mut Bookma
|
||||
}
|
||||
bookmarks.add_bookmark(&args.name, &path)?;
|
||||
|
||||
if config.general.show_books_on_bookmark {
|
||||
if config.general.show_entries_on_bookmark {
|
||||
output.push_info(&bookmarks.to_formatted_string(config)?);
|
||||
} else {
|
||||
_ = to_rooted(&mut path);
|
||||
@@ -216,7 +211,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<()> {
|
||||
bookmarks.remove_bookmark(&args.name)?;
|
||||
|
||||
if config.general.show_books_on_bookmark {
|
||||
if config.general.show_entries_on_bookmark {
|
||||
output.push_info(&bookmarks.to_formatted_string(config)?);
|
||||
} else {
|
||||
output.push_info(&format!("remove bookmark `{}{}{}`.", generate_style_sequence(Some(STYLES.set.bold), None, None), args.name, RESET_SEQ));
|
||||
|
||||
+32
-17
@@ -5,7 +5,6 @@ use std::fs::File;
|
||||
use std::io::{Error, Result};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::str::FromStr;
|
||||
use config_parser::RESET_SEQ;
|
||||
use sysinfo::{Pid, System};
|
||||
use dirs::home_dir;
|
||||
|
||||
@@ -41,17 +40,15 @@ impl Stack {
|
||||
buffer.push_str("-- the stack is empty");
|
||||
} else {
|
||||
// print stack to string
|
||||
let max_num_len = self.stack.len().to_string().len();
|
||||
let max_num_len: usize = self.stack.len().to_string().len();
|
||||
for (n, item) in self.stack.iter().rev().enumerate() {
|
||||
let padding = make_padding_string(max_num_len - n.to_string().len());
|
||||
let number = apply_format(&n.to_string(), &config.styles.stack_number_style);
|
||||
let separator = apply_format(
|
||||
&config.format.stack_separator,
|
||||
&config.styles.stack_separator_style,
|
||||
);
|
||||
let mut path = apply_format(item.to_str().unwrap(), &config.styles.stack_path_style);
|
||||
let padding: String = make_padding_string(max_num_len - n.to_string().len());
|
||||
let mut number: String = n.to_string();
|
||||
let mut separator: String = config.format.stack_separator.clone();
|
||||
let mut path: String = item.clone().into_os_string().into_string().unwrap();
|
||||
|
||||
if config.format.show_home_as_tilde {
|
||||
let home = match home_dir() {
|
||||
let home: String = match home_dir() {
|
||||
Some(value) => match value.into_os_string().into_string() {
|
||||
Ok(value) => value,
|
||||
Err(error) => return Err(Error::other(format!("-- failed to conver home directory to string: {}", error.to_str().unwrap()))),
|
||||
@@ -60,14 +57,32 @@ impl Stack {
|
||||
};
|
||||
path = path.replace(&home, "~");
|
||||
}
|
||||
path = path.replace('/', &format!("{}{}/{}{}", RESET_SEQ, config.styles.stack_punct_style, RESET_SEQ, config.styles.stack_path_style));
|
||||
if config.format.stack_hide_numbers {
|
||||
buffer.push_str(&format!("{}\n", path));
|
||||
} else if config.format.align_separators {
|
||||
buffer.push_str(&format!("{}{}{}{}\n", number, padding, separator, path));
|
||||
} else {
|
||||
buffer.push_str(&format!("{}{}{}{}\n", number, separator, padding, path));
|
||||
|
||||
if item.is_dir() {
|
||||
let slash: String = apply_format(&"/".to_owned(), &config.styles.stack_punct_style)?;
|
||||
let mut segments: Vec<String> = path.split('/').map(|element| element.to_owned()).collect();
|
||||
for element in segments.iter_mut() {
|
||||
*element = apply_format(&element, &config.styles.stack_path_style)?;
|
||||
}
|
||||
path = segments.join(&slash);
|
||||
|
||||
number = apply_format(&number, &config.styles.stack_number_style)?;
|
||||
separator = apply_format(&separator, &config.styles.stack_separator_style)?;
|
||||
}
|
||||
|
||||
let mut line: String;
|
||||
if config.format.stack_hide_numbers {
|
||||
line = format!("{}\n", path);
|
||||
} else if config.format.align_separators {
|
||||
line = format!("{}{}{}{}\n", number, padding, separator, path);
|
||||
} else {
|
||||
line = format!("{}{}{}{}\n", number, separator, padding, path);
|
||||
}
|
||||
if !item.is_dir() {
|
||||
line = apply_format(&line, &config.styles.stack_invalid_style)?;
|
||||
}
|
||||
|
||||
buffer.push_str(&line);
|
||||
}
|
||||
}
|
||||
Ok(buffer)
|
||||
|
||||
Reference in New Issue
Block a user