mirror of
https://github.com/lsd-rs/lsd
synced 2024-12-14 06:02:36 +00:00
Add --header flag
This commit is contained in:
parent
f6965bebe9
commit
d5520d2791
4 changed files with 89 additions and 0 deletions
|
@ -329,6 +329,11 @@ pub fn build() -> App<'static, 'static> {
|
||||||
.number_of_values(1)
|
.number_of_values(1)
|
||||||
.help("Attach hyperlink to filenames"),
|
.help("Attach hyperlink to filenames"),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("header")
|
||||||
|
.long("header")
|
||||||
|
.help("Display block headers"),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_date_argument(arg: String) -> Result<(), String> {
|
fn validate_date_argument(arg: String) -> Result<(), String> {
|
||||||
|
|
|
@ -44,6 +44,7 @@ pub struct Config {
|
||||||
pub total_size: Option<bool>,
|
pub total_size: Option<bool>,
|
||||||
pub symlink_arrow: Option<String>,
|
pub symlink_arrow: Option<String>,
|
||||||
pub hyperlink: Option<HyperlinkOption>,
|
pub hyperlink: Option<HyperlinkOption>,
|
||||||
|
pub header: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Debug, Deserialize)]
|
#[derive(Eq, PartialEq, Debug, Deserialize)]
|
||||||
|
@ -95,6 +96,7 @@ impl Config {
|
||||||
total_size: None,
|
total_size: None,
|
||||||
symlink_arrow: None,
|
symlink_arrow: None,
|
||||||
hyperlink: None,
|
hyperlink: None,
|
||||||
|
header: None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -388,6 +390,7 @@ mod tests {
|
||||||
total_size: Some(false),
|
total_size: Some(false),
|
||||||
symlink_arrow: Some("⇒".into()),
|
symlink_arrow: Some("⇒".into()),
|
||||||
hyperlink: Some(HyperlinkOption::Never),
|
hyperlink: Some(HyperlinkOption::Never),
|
||||||
|
header: None
|
||||||
},
|
},
|
||||||
c
|
c
|
||||||
);
|
);
|
||||||
|
|
|
@ -15,6 +15,7 @@ pub mod sorting;
|
||||||
pub mod symlink_arrow;
|
pub mod symlink_arrow;
|
||||||
pub mod symlinks;
|
pub mod symlinks;
|
||||||
pub mod total_size;
|
pub mod total_size;
|
||||||
|
pub mod header;
|
||||||
|
|
||||||
pub use blocks::Block;
|
pub use blocks::Block;
|
||||||
pub use blocks::Blocks;
|
pub use blocks::Blocks;
|
||||||
|
@ -41,6 +42,7 @@ pub use sorting::Sorting;
|
||||||
pub use symlink_arrow::SymlinkArrow;
|
pub use symlink_arrow::SymlinkArrow;
|
||||||
pub use symlinks::NoSymlink;
|
pub use symlinks::NoSymlink;
|
||||||
pub use total_size::TotalSize;
|
pub use total_size::TotalSize;
|
||||||
|
pub use header::Header;
|
||||||
|
|
||||||
use crate::config_file::Config;
|
use crate::config_file::Config;
|
||||||
|
|
||||||
|
@ -69,6 +71,7 @@ pub struct Flags {
|
||||||
pub total_size: TotalSize,
|
pub total_size: TotalSize,
|
||||||
pub symlink_arrow: SymlinkArrow,
|
pub symlink_arrow: SymlinkArrow,
|
||||||
pub hyperlink: HyperlinkOption,
|
pub hyperlink: HyperlinkOption,
|
||||||
|
pub header: Header,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Flags {
|
impl Flags {
|
||||||
|
@ -97,6 +100,7 @@ impl Flags {
|
||||||
total_size: TotalSize::configure_from(matches, config),
|
total_size: TotalSize::configure_from(matches, config),
|
||||||
symlink_arrow: SymlinkArrow::configure_from(matches, config),
|
symlink_arrow: SymlinkArrow::configure_from(matches, config),
|
||||||
hyperlink: HyperlinkOption::configure_from(matches, config),
|
hyperlink: HyperlinkOption::configure_from(matches, config),
|
||||||
|
header: Header::configure_from(matches, config),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
77
src/flags/header.rs
Normal file
77
src/flags/header.rs
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue