Add --header flag

This commit is contained in:
MichaelAug 2022-04-30 07:55:13 +01:00 committed by Wei Zhang
parent f6965bebe9
commit d5520d2791
4 changed files with 89 additions and 0 deletions

View file

@ -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> {

View file

@ -44,6 +44,7 @@ pub struct Config {
pub total_size: Option<bool>,
pub symlink_arrow: Option<String>,
pub hyperlink: Option<HyperlinkOption>,
pub header: Option<bool>,
}
#[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
);

View file

@ -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),
})
}
}

77
src/flags/header.rs Normal file
View file

@ -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<Self> 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<Self> {
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<Self> {
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));
}
}