style: add config for symlink arrow

This commit is contained in:
zwPapEr 2020-10-09 11:12:10 +08:00 committed by Abin Simon
parent 8ed0611bf8
commit ce6ebc170a
4 changed files with 74 additions and 3 deletions

View file

@ -286,7 +286,7 @@ fn get_output<'a>(
ANSIStrings(&[
meta.name.render(colors, icons, &display_option),
meta.indicator.render(&flags),
meta.symlink.render(colors),
meta.symlink.render(colors, &flags.styles),
])
.to_string()
};

View file

@ -33,6 +33,7 @@ pub use sorting::SortColumn;
pub use sorting::SortOrder;
pub use sorting::Sorting;
pub use symlinks::NoSymlink;
pub use symlinks::SymlinkArrow;
pub use total_size::TotalSize;
use crate::config_file::Config;
@ -59,6 +60,13 @@ pub struct Flags {
pub size: SizeFlag,
pub sorting: Sorting,
pub total_size: TotalSize,
pub styles: Styles,
}
/// A struct to hold the style flags for the application.
#[derive(Clone, Debug, Default)]
pub struct Styles {
pub symlink_arrow: SymlinkArrow,
}
impl Flags {
@ -84,6 +92,9 @@ impl Flags {
recursion: Recursion::configure_from(matches, config)?,
sorting: Sorting::configure_from(matches, config),
total_size: TotalSize::configure_from(matches, config),
styles: Styles {
symlink_arrow: SymlinkArrow::configure_from(matches, config),
},
})
}
}

View file

@ -46,6 +46,53 @@ impl Configurable<Self> for NoSymlink {
}
}
/// The flag showing how to display symbolic arrow.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SymlinkArrow(String);
impl Configurable<Self> for SymlinkArrow {
/// `SymlinkArrow` can not be configured by [ArgMatches]
///
/// Return `None`
fn from_arg_matches(_: &ArgMatches) -> Option<Self> {
None
}
/// Get a potential `SymlinkArrow` value from a [Config].
///
/// If the Config's [Yaml] contains the [String](Yaml::String) value pointed to by
/// "symlink-arrow", this returns its value as the value of the `SymlinkArrow`, in a [Some].
/// Otherwise this returns [None].
fn from_config(config: &Config) -> Option<Self> {
if let Some(yaml) = &config.yaml {
match &yaml["styles"]["symlink-arrow"] {
Yaml::BadValue => None,
Yaml::String(value) => Some(SymlinkArrow(value.to_string())),
_ => {
config.print_wrong_type_warning("symlink-arrow", "string");
None
}
}
} else {
None
}
}
}
/// The default value for the `SymlinkArrow` is `\u{21d2}(⇒)`
impl Default for SymlinkArrow {
fn default() -> Self {
Self(String::from("\u{21d2}")) // ⇒
}
}
use std::fmt;
impl fmt::Display for SymlinkArrow {
// This trait requires `fmt` with this exact signature.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod test {
use super::NoSymlink;
@ -101,4 +148,16 @@ mod test {
NoSymlink::from_config(&Config::with_yaml(yaml))
);
}
use super::SymlinkArrow;
#[test]
fn test_from_config_arrow_utf8() {
let yaml_string = "styles:
symlink-arrow: ";
let yaml = YamlLoader::load_from_str(yaml_string).unwrap()[0].clone();
assert_eq!(
Some(SymlinkArrow(String::from("\u{21B9}"))),
SymlinkArrow::from_config(&Config::with_yaml(yaml))
);
}
}

View file

@ -1,4 +1,5 @@
use crate::color::{ColoredString, Colors, Elem};
use crate::flags::Styles;
use ansi_term::{ANSIString, ANSIStrings};
use std::fs::read_link;
use std::path::Path;
@ -51,7 +52,7 @@ impl SymLink {
}
}
pub fn render(&self, colors: &Colors) -> ColoredString {
pub fn render(&self, colors: &Colors, styles: &Styles) -> ColoredString {
if let Some(target_string) = self.symlink_string() {
let elem = if self.valid {
&Elem::SymLink
@ -60,7 +61,7 @@ impl SymLink {
};
let strings: &[ColoredString] = &[
ColoredString::from(" \u{21d2} "), // ⇒
ColoredString::from(format!(" {} ", styles.symlink_arrow)), // ⇒ \u{21d2}
colors.colorize(target_string, elem),
];