diff --git a/src/app.rs b/src/app.rs index 65d1a1d..ddfcbc0 100644 --- a/src/app.rs +++ b/src/app.rs @@ -329,6 +329,11 @@ pub fn build() -> App<'static, 'static> { .number_of_values(1) .help("Attach hyperlink to filenames"), ) + .arg( + Arg::with_name("header") + .long("header") + .help("Display block headers"), + ) } fn validate_date_argument(arg: String) -> Result<(), String> { diff --git a/src/config_file.rs b/src/config_file.rs index b4820c6..ac996ac 100644 --- a/src/config_file.rs +++ b/src/config_file.rs @@ -44,6 +44,7 @@ pub struct Config { pub total_size: Option, pub symlink_arrow: Option, pub hyperlink: Option, + pub header: Option, } #[derive(Eq, PartialEq, Debug, Deserialize)] @@ -95,6 +96,7 @@ impl Config { total_size: None, symlink_arrow: None, hyperlink: None, + header: None } } @@ -388,6 +390,7 @@ mod tests { total_size: Some(false), symlink_arrow: Some("⇒".into()), hyperlink: Some(HyperlinkOption::Never), + header: None }, c ); diff --git a/src/flags.rs b/src/flags.rs index 75f3c63..81de10d 100644 --- a/src/flags.rs +++ b/src/flags.rs @@ -15,6 +15,7 @@ pub mod sorting; pub mod symlink_arrow; pub mod symlinks; pub mod total_size; +pub mod header; pub use blocks::Block; pub use blocks::Blocks; @@ -41,6 +42,7 @@ pub use sorting::Sorting; pub use symlink_arrow::SymlinkArrow; pub use symlinks::NoSymlink; pub use total_size::TotalSize; +pub use header::Header; use crate::config_file::Config; @@ -69,6 +71,7 @@ pub struct Flags { pub total_size: TotalSize, pub symlink_arrow: SymlinkArrow, pub hyperlink: HyperlinkOption, + pub header: Header, } impl Flags { @@ -97,6 +100,7 @@ impl Flags { total_size: TotalSize::configure_from(matches, config), symlink_arrow: SymlinkArrow::configure_from(matches, config), hyperlink: HyperlinkOption::configure_from(matches, config), + header: Header::configure_from(matches, config), }) } } diff --git a/src/flags/header.rs b/src/flags/header.rs new file mode 100644 index 0000000..973c45b --- /dev/null +++ b/src/flags/header.rs @@ -0,0 +1,77 @@ +//! This module defines the [Header] flag. To set it up from [ArgMatches], a [Config] and its +//! [Default] value, use the [configure_from](Configurable::configure_from) method. + +use super::Configurable; + +use crate::config_file::Config; + +use clap::ArgMatches; + +/// The flag showing whether to display block headers. +#[derive(Clone, Debug, Copy, PartialEq, Eq, Default)] +pub struct Header(pub bool); + +impl Configurable for Header { + /// Get a potential `Header` value from [ArgMatches]. + /// + /// If the "header" argument is passed, this returns a `Header` with value `true` in a + /// [Some]. Otherwise this returns [None]. + fn from_arg_matches(matches: &ArgMatches) -> Option { + if matches.is_present("header") { + Some(Self(true)) + } else { + None + } + } + + /// Get a potential `Header` value from a [Config]. + /// + /// If the `Config::header` has value, + /// this returns it as the value of the `Header`, in a [Some]. + /// Otherwise this returns [None]. + fn from_config(config: &Config) -> Option { + config.header.map(Self) + } +} + +#[cfg(test)] +mod test { + use super::Header; + + use crate::app; + use crate::config_file::Config; + use crate::flags::Configurable; + + #[test] + fn test_from_arg_matches_none() { + let argv = vec!["lsd"]; + let matches = app::build().get_matches_from_safe(argv).unwrap(); + assert_eq!(None, Header::from_arg_matches(&matches)); + } + + #[test] + fn test_from_arg_matches_true() { + let argv = vec!["lsd", "--header"]; + let matches = app::build().get_matches_from_safe(argv).unwrap(); + assert_eq!(Some(Header(true)), Header::from_arg_matches(&matches)); + } + + #[test] + fn test_from_config_none() { + assert_eq!(None, Header::from_config(&Config::with_none())); + } + + #[test] + fn test_from_config_true() { + let mut c = Config::with_none(); + c.header = Some(true); + assert_eq!(Some(Header(true)), Header::from_config(&c)); + } + + #[test] + fn test_from_config_false() { + let mut c = Config::with_none(); + c.header = Some(false); + assert_eq!(Some(Header(false)), Header::from_config(&c)); + } +} \ No newline at end of file