implemented displaying invalid path and according styles
This commit is contained in:
@@ -62,8 +62,8 @@ Style settings accept styles and one color in the following formats:
|
|||||||
- [x] color option for punctuation (mostly '/')
|
- [x] color option for punctuation (mostly '/')
|
||||||
- [x] bookmarks
|
- [x] bookmarks
|
||||||
- [x] do not resolve links in bookmarks
|
- [x] do not resolve links in bookmarks
|
||||||
- [ ] option to show invalid paths
|
- [x] option to show invalid paths
|
||||||
- [ ] style option for invalid paths
|
- [x] style option for invalid paths
|
||||||
- [ ] option & subcommand to remove invalid paths
|
- [ ] option & subcommand to remove invalid paths
|
||||||
- [x] push <number> to push path in stack
|
- [x] push <number> to push path in stack
|
||||||
- [x] write documentation
|
- [x] write documentation
|
||||||
|
|||||||
@@ -105,8 +105,16 @@ pub enum ColorContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// prepends input with style string and appends the reset sequence at the end
|
/// prepends input with style string and appends the reset sequence at the end
|
||||||
pub fn apply_format(input: &str, style: &str) -> String {
|
pub fn apply_format(input: &String, style: &String) -> Result<String> {
|
||||||
format!("{}{}{}", style, input, RESET_SEQ)
|
let style_set: String = match parse_ansi_set(style) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(error) => return Err(error),
|
||||||
|
};
|
||||||
|
let style_reset: String = match parse_ansi_reset(style) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(error) => return Err(error),
|
||||||
|
};
|
||||||
|
Ok(format!("{}{}{}", style_set, input, style_reset))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// generates a common style sequence of format
|
/// generates a common style sequence of format
|
||||||
@@ -185,11 +193,11 @@ pub fn make_padding_string(len: usize) -> String {
|
|||||||
String::from_utf8(vec![b' '; len]).unwrap()
|
String::from_utf8(vec![b' '; len]).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// convert color setting to ansi escape sequence
|
/// convert color setting to ansi escape sequence to set style
|
||||||
/// input format is a quoted string (either double or single)
|
/// input format is a quoted string (either double or single)
|
||||||
/// the style can be a combination of **one** color and
|
/// the style can be a combination of **one** color and
|
||||||
/// one or more style options (bold, italic, underlined, strikethrough)
|
/// one or more style options (bold, italic, underlined, strikethrough)
|
||||||
pub fn parse_style(arg: &String) -> Result<String> {
|
pub fn parse_ansi_set(arg: &String) -> Result<String> {
|
||||||
let mut colors: Vec<String> = Vec::<String>::new();
|
let mut colors: Vec<String> = Vec::<String>::new();
|
||||||
let mut styles: Vec<String> = Vec::<String>::new();
|
let mut styles: Vec<String> = Vec::<String>::new();
|
||||||
|
|
||||||
@@ -246,6 +254,66 @@ pub fn parse_style(arg: &String) -> Result<String> {
|
|||||||
Ok(styles.join(""))
|
Ok(styles.join(""))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// convert color setting to ansi escape sequence to reset style
|
||||||
|
/// input format is a quoted string (either double or single)
|
||||||
|
/// the style can be a combination of **one** color and
|
||||||
|
/// one or more style options (bold, italic, underlined, strikethrough)
|
||||||
|
pub fn parse_ansi_reset(arg: &String) -> Result<String> {
|
||||||
|
let mut colors: Vec<String> = Vec::<String>::new();
|
||||||
|
let mut styles: Vec<String> = Vec::<String>::new();
|
||||||
|
|
||||||
|
// separate style options
|
||||||
|
let mut tokens: Vec<String> = arg.split([' ', ',', '\"', '\'']).map(|entry| entry.trim().to_lowercase()).collect();
|
||||||
|
tokens.retain(|entry| !entry.is_empty());
|
||||||
|
|
||||||
|
// parse options
|
||||||
|
for option in tokens {
|
||||||
|
// parse numbered colors
|
||||||
|
if let Ok(_) = parse_numbered_color(&option) {
|
||||||
|
colors.push(generate_style_sequence(None, Some(COLORS.fg.default), None));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse rgb colors
|
||||||
|
if let Ok(_) = parse_rgb_color(&option) {
|
||||||
|
colors.push(generate_style_sequence(None, Some(COLORS.fg.default), None));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse styles and named colors
|
||||||
|
match option.as_str() {
|
||||||
|
// styles
|
||||||
|
"bold" => styles.push(generate_style_sequence(Some(STYLES.reset.bold), None, None)),
|
||||||
|
"dim" => styles.push(generate_style_sequence(Some(STYLES.reset.dim), None, None)),
|
||||||
|
"italic" => styles.push(generate_style_sequence(Some(STYLES.reset.italic), None, None)),
|
||||||
|
"underlined" => styles.push(generate_style_sequence(Some(STYLES.reset.underlined), None, None)),
|
||||||
|
"blinking" => styles.push(generate_style_sequence(Some(STYLES.reset.blinking), None, None)),
|
||||||
|
"reversed" => styles.push(generate_style_sequence(Some(STYLES.reset.reversed), None, None)),
|
||||||
|
"invisible" => styles.push(generate_style_sequence(Some(STYLES.reset.invisible), None, None)),
|
||||||
|
"strikethrough" => styles.push(generate_style_sequence(Some(STYLES.reset.strikethrough), None, None)),
|
||||||
|
// named colors
|
||||||
|
"black" => colors.push(generate_style_sequence(None, Some(COLORS.fg.default), None)),
|
||||||
|
"red" => colors.push(generate_style_sequence(None, Some(COLORS.fg.default), None)),
|
||||||
|
"green" => colors.push(generate_style_sequence(None, Some(COLORS.fg.default), None)),
|
||||||
|
"yellow" => colors.push(generate_style_sequence(None, Some(COLORS.fg.default), None)),
|
||||||
|
"blue" => colors.push(generate_style_sequence(None, Some(COLORS.fg.default), None)),
|
||||||
|
"magenta" => colors.push(generate_style_sequence(None, Some(COLORS.fg.default), None)),
|
||||||
|
"cyan" => colors.push(generate_style_sequence(None, Some(COLORS.fg.default), None)),
|
||||||
|
"white" => colors.push(generate_style_sequence(None, Some(COLORS.fg.default), None)),
|
||||||
|
"default" => colors.push(generate_style_sequence(None, Some(COLORS.fg.default), None)),
|
||||||
|
_ => return Err(Error::other(format!("-- could not parse style token `{}` in config file", option))),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if colors.len() > 1 {
|
||||||
|
return Err(Error::other(format!("-- too many colors found in setting <{}>", arg)));
|
||||||
|
}
|
||||||
|
if !colors.is_empty() {
|
||||||
|
styles.push(colors.pop().unwrap());
|
||||||
|
}
|
||||||
|
Ok(styles.join(""))
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_numbered_color(string: &String) -> Result<String> {
|
fn parse_numbered_color(string: &String) -> Result<String> {
|
||||||
// check for numbered color
|
// check for numbered color
|
||||||
if let Ok(number) = string.parse::<u8>() {
|
if let Ok(number) = string.parse::<u8>() {
|
||||||
|
|||||||
@@ -8,3 +8,4 @@ pub use common::{
|
|||||||
parse_config_file,
|
parse_config_file,
|
||||||
remove_inline_comment,
|
remove_inline_comment,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ pub fn gen_to_ansi_sequences(fields: &Punctuated<Field, Comma>) -> TokenStream {
|
|||||||
match attr_name.first() {
|
match attr_name.first() {
|
||||||
Some(value) => if value.ident == "style_config" {
|
Some(value) => if value.ident == "style_config" {
|
||||||
conversions.extend(quote! {
|
conversions.extend(quote! {
|
||||||
self.#name = match config_parser::parse_style(&self.#name) {
|
self.#name = match config_parser::parse_ansi_set(&self.#name) {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(_) => return Err(std::io::Error::other(format!("failed to convert '{}' to ansi escape sequence", self.#name))),
|
Err(_) => return Err(std::io::Error::other(format!("failed to convert '{}' to ansi escape sequence", self.#name))),
|
||||||
};
|
};
|
||||||
|
|||||||
+2
-3
@@ -2,7 +2,6 @@
|
|||||||
#![allow(non_camel_case_types)]
|
#![allow(non_camel_case_types)]
|
||||||
|
|
||||||
use clap::{Args, Parser, Subcommand};
|
use clap::{Args, Parser, Subcommand};
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
/// implements stack for cd wrapper script
|
/// implements stack for cd wrapper script
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
@@ -32,7 +31,7 @@ pub enum Action {
|
|||||||
bookmark(BookmarkArgs),
|
bookmark(BookmarkArgs),
|
||||||
|
|
||||||
/// display current configuartion (mostly for debugging)
|
/// display current configuartion (mostly for debugging)
|
||||||
configuration(ConfigArgs),
|
configuration,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
@@ -105,7 +104,7 @@ pub enum BookmarkAction {
|
|||||||
remove(BookmarkSubArgs),
|
remove(BookmarkSubArgs),
|
||||||
|
|
||||||
/// get bookmarknames for shell completions
|
/// get bookmarknames for shell completions
|
||||||
names(EmptyArgs),
|
completions(EmptyArgs),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
|
|||||||
+34
-18
@@ -9,7 +9,7 @@ use std::str::FromStr;
|
|||||||
use dirs::{config_dir, home_dir};
|
use dirs::{config_dir, home_dir};
|
||||||
|
|
||||||
use super::{config::*, util::to_rooted};
|
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)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Bookmarks {
|
pub struct Bookmarks {
|
||||||
@@ -49,9 +49,11 @@ impl Bookmarks {
|
|||||||
Err(err) => return Err(Error::other(err.to_string())),
|
Err(err) => return Err(Error::other(err.to_string())),
|
||||||
};
|
};
|
||||||
to_rooted(&mut path)?;
|
to_rooted(&mut path)?;
|
||||||
if !path.is_dir() {
|
|
||||||
continue;
|
// if !path.is_dir() {
|
||||||
}
|
// continue;
|
||||||
|
// }
|
||||||
|
|
||||||
bookmarks.bookmarks.insert(key, path);
|
bookmarks.bookmarks.insert(key, path);
|
||||||
}
|
}
|
||||||
Ok(bookmarks)
|
Ok(bookmarks)
|
||||||
@@ -110,17 +112,14 @@ impl Bookmarks {
|
|||||||
Some(value) => value,
|
Some(value) => value,
|
||||||
None => return Err(Error::other("-- failed to determine maximum bookmark name length")),
|
None => return Err(Error::other("-- failed to determine maximum bookmark name length")),
|
||||||
};
|
};
|
||||||
for (mark, path) in &self.bookmarks {
|
for (raw_name, raw_path) in &self.bookmarks {
|
||||||
let padding = make_padding_string(max_name_len - mark.len());
|
let padding: String = make_padding_string(max_name_len - raw_name.len());
|
||||||
let name = apply_format(mark, &config.styles.bookmarks_name_style);
|
let mut name: String = raw_name.clone();
|
||||||
let separator = apply_format(
|
let mut separator: String = config.format.bookmarks_separator.clone();
|
||||||
&config.format.bookmarks_separator,
|
let mut path: String = raw_path.clone().into_os_string().into_string().unwrap();
|
||||||
&config.styles.bookmarks_seperator_style,
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut path = apply_format(path.to_str().unwrap(), &config.styles.bookmarks_path_style);
|
|
||||||
if config.format.show_home_as_tilde {
|
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() {
|
Some(value) => match value.into_os_string().into_string() {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(error) => return Err(Error::other(format!("-- failed to conver home directory to string: {}", error.to_str().unwrap()))),
|
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(&home, "~");
|
||||||
}
|
}
|
||||||
path = path.replace('/', &format!("{}{}/{}{}", RESET_SEQ, config.styles.bookmarks_punct_style, RESET_SEQ, &config.styles.bookmarks_path_style));
|
|
||||||
|
|
||||||
if config.format.align_separators {
|
if raw_path.is_dir() {
|
||||||
buffer.push_str(&format!("{}{}{}{}\n", name, padding, separator, path));
|
let slash: String = apply_format(&"/".to_owned(), &config.styles.bookmarks_punct_style)?;
|
||||||
} else {
|
let mut segments: Vec<String> = path.split('/').map(|element| element.to_owned()).collect();
|
||||||
buffer.push_str(&format!("{}{}{}{}\n", name, separator, padding, path));
|
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)
|
Ok(buffer)
|
||||||
|
|||||||
+42
-12
@@ -5,7 +5,12 @@
|
|||||||
|
|
||||||
use dirs::config_dir;
|
use dirs::config_dir;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::{Error, Result};
|
use std::{
|
||||||
|
io::{
|
||||||
|
Error, Result
|
||||||
|
},
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
use config_parser::*;
|
use config_parser::*;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, ConfigParser)]
|
#[derive(Debug, Clone, Default, ConfigParser)]
|
||||||
@@ -28,9 +33,21 @@ pub struct GeneralSettings {
|
|||||||
#[default_value(false)]
|
#[default_value(false)]
|
||||||
pub show_stack_on_pop: bool,
|
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)]
|
#[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)]
|
#[derive(Debug, Clone, Default, ConfigParser)]
|
||||||
@@ -88,6 +105,11 @@ pub struct StyleSettings {
|
|||||||
#[default_value("'magenta'")]
|
#[default_value("'magenta'")]
|
||||||
pub stack_punct_style: String,
|
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
|
/// (string) style applied to bookmark names when displaying the bookmarks
|
||||||
#[style_config]
|
#[style_config]
|
||||||
#[default_value("'default'")]
|
#[default_value("'default'")]
|
||||||
@@ -107,6 +129,11 @@ pub struct StyleSettings {
|
|||||||
#[style_config]
|
#[style_config]
|
||||||
#[default_value("'magenta'")]
|
#[default_value("'magenta'")]
|
||||||
pub bookmarks_punct_style: String,
|
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 {
|
impl Config {
|
||||||
@@ -123,23 +150,30 @@ impl Config {
|
|||||||
";
|
";
|
||||||
|
|
||||||
/// generates and populates a new instance of 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();
|
let mut config: Config = Self::default();
|
||||||
// get configuration directory
|
// get configuration directory
|
||||||
let mut config_file = match config_dir() {
|
let mut config_file: PathBuf = match config_dir() {
|
||||||
Some(value) => value,
|
Some(value) => value,
|
||||||
None => {
|
None => {
|
||||||
return Err(Error::other("-- failed to retrieve configuration directory"))
|
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
|
// expand path to configuration file and default configuration file
|
||||||
let mut default_file = config_file.clone();
|
let mut default_file = config_file.clone();
|
||||||
default_file.push(format!("{}/{}", Self::CONFIG_DIRECTORY_NAME, Self::DEFAULT_CONFIG_NAME));
|
default_file.push(Self::DEFAULT_CONFIG_NAME);
|
||||||
config_file.push(format!("{}/{}", Self::CONFIG_DIRECTORY_NAME, Self::CONFIG_FILE_NAME));
|
config_file.push(Self::CONFIG_FILE_NAME);
|
||||||
|
|
||||||
// write default configuration file if it does not exist
|
// write default configuration file if it does not exist
|
||||||
if !default_file.is_file() {
|
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());
|
default_string.push_str(&config.to_string());
|
||||||
_ = fs::write(&default_file, default_string);
|
_ = fs::write(&default_file, default_string);
|
||||||
}
|
}
|
||||||
@@ -161,10 +195,6 @@ impl Config {
|
|||||||
_ = config.parse_from_string(&default_config);
|
_ = config.parse_from_string(&default_config);
|
||||||
}
|
}
|
||||||
|
|
||||||
if styles_as_ansi_sequences {
|
|
||||||
config.to_ansi_sequences()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(config)
|
Ok(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+7
-12
@@ -22,7 +22,7 @@ use std::str::FromStr;
|
|||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let mut output = Output::new();
|
let mut output = Output::new();
|
||||||
let config = match Config::new(true) {
|
let config = match Config::new() {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
// config object is not ready at this point so the style
|
// 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::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::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::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() {
|
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<()> {
|
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 {
|
if let Some(action) = &args.bookmark_action {
|
||||||
match action {
|
match action {
|
||||||
BookmarkAction::list(_) => list_bookmarks(config, bookmarks, output)?,
|
BookmarkAction::list(_) => list_bookmarks(config, bookmarks, output)?,
|
||||||
BookmarkAction::add(args) => add_bookmarks(args, config, bookmarks, output)?,
|
BookmarkAction::add(args) => add_bookmarks(args, config, bookmarks, output)?,
|
||||||
BookmarkAction::remove(args) => remove_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`
|
} else if args.name.is_some() { // handle `change to bookmark`
|
||||||
let path = bookmarks.get_path_by_name(args.name.as_ref().unwrap())?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_config(args: &ConfigArgs, output: &mut Output) -> Result<()> {
|
fn handle_config(output: &mut Output) -> Result<()> {
|
||||||
let convert: bool = match args.convert {
|
let config = Config::new();
|
||||||
Some(value) => value,
|
|
||||||
None => false,
|
|
||||||
};
|
|
||||||
let config = Config::new(convert);
|
|
||||||
output.push_info(&format!("config = {:#?}", config));
|
output.push_info(&format!("config = {:#?}", config));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -201,7 +196,7 @@ fn add_bookmarks(args: &BookmarkSubArgs, config: &Config, bookmarks: &mut Bookma
|
|||||||
}
|
}
|
||||||
bookmarks.add_bookmark(&args.name, &path)?;
|
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)?);
|
output.push_info(&bookmarks.to_formatted_string(config)?);
|
||||||
} else {
|
} else {
|
||||||
_ = to_rooted(&mut path);
|
_ = 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<()> {
|
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_entries_on_bookmark {
|
||||||
output.push_info(&bookmarks.to_formatted_string(config)?);
|
output.push_info(&bookmarks.to_formatted_string(config)?);
|
||||||
} else {
|
} else {
|
||||||
output.push_info(&format!("remove bookmark `{}{}{}`.", generate_style_sequence(Some(STYLES.set.bold), None, None), args.name, RESET_SEQ));
|
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::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;
|
|
||||||
use sysinfo::{Pid, System};
|
use sysinfo::{Pid, System};
|
||||||
use dirs::home_dir;
|
use dirs::home_dir;
|
||||||
|
|
||||||
@@ -41,17 +40,15 @@ impl Stack {
|
|||||||
buffer.push_str("-- the stack is empty");
|
buffer.push_str("-- the stack is empty");
|
||||||
} else {
|
} else {
|
||||||
// print stack to string
|
// 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() {
|
for (n, item) in self.stack.iter().rev().enumerate() {
|
||||||
let padding = make_padding_string(max_num_len - n.to_string().len());
|
let padding: String = make_padding_string(max_num_len - n.to_string().len());
|
||||||
let number = apply_format(&n.to_string(), &config.styles.stack_number_style);
|
let mut number: String = n.to_string();
|
||||||
let separator = apply_format(
|
let mut separator: String = config.format.stack_separator.clone();
|
||||||
&config.format.stack_separator,
|
let mut path: String = item.clone().into_os_string().into_string().unwrap();
|
||||||
&config.styles.stack_separator_style,
|
|
||||||
);
|
|
||||||
let mut path = apply_format(item.to_str().unwrap(), &config.styles.stack_path_style);
|
|
||||||
if config.format.show_home_as_tilde {
|
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() {
|
Some(value) => match value.into_os_string().into_string() {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(error) => return Err(Error::other(format!("-- failed to conver home directory to string: {}", error.to_str().unwrap()))),
|
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(&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 {
|
if item.is_dir() {
|
||||||
buffer.push_str(&format!("{}\n", path));
|
let slash: String = apply_format(&"/".to_owned(), &config.styles.stack_punct_style)?;
|
||||||
} else if config.format.align_separators {
|
let mut segments: Vec<String> = path.split('/').map(|element| element.to_owned()).collect();
|
||||||
buffer.push_str(&format!("{}{}{}{}\n", number, padding, separator, path));
|
for element in segments.iter_mut() {
|
||||||
} else {
|
*element = apply_format(&element, &config.styles.stack_path_style)?;
|
||||||
buffer.push_str(&format!("{}{}{}{}\n", number, separator, padding, path));
|
}
|
||||||
|
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)
|
Ok(buffer)
|
||||||
|
|||||||
Reference in New Issue
Block a user