Skip to content

Commit

Permalink
Add git into flags config
Browse files Browse the repository at this point in the history
  • Loading branch information
hpwxf committed Feb 26, 2023
1 parent d31df56 commit c5df43a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct Config {
pub symlink_arrow: Option<String>,
pub hyperlink: Option<HyperlinkOption>,
pub header: Option<bool>,
pub git: Option<bool>,
}

#[derive(Eq, PartialEq, Debug, Deserialize)]
Expand Down Expand Up @@ -97,6 +98,7 @@ impl Config {
symlink_arrow: None,
hyperlink: None,
header: None,
git: None,
}
}

Expand Down Expand Up @@ -388,7 +390,8 @@ mod tests {
total_size: Some(false),
symlink_arrow: Some("⇒".into()),
hyperlink: Some(HyperlinkOption::Never),
header: None
header: None,
git: None,
},
c
);
Expand Down
4 changes: 4 additions & 0 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod color;
pub mod date;
pub mod dereference;
pub mod display;
pub mod git;
pub mod header;
pub mod hyperlink;
pub mod icons;
Expand Down Expand Up @@ -47,6 +48,7 @@ use crate::config_file::Config;

use clap::{ArgMatches, Error};

use crate::flags::git::Git;
#[cfg(doc)]
use yaml_rust::Yaml;

Expand All @@ -71,6 +73,7 @@ pub struct Flags {
pub symlink_arrow: SymlinkArrow,
pub hyperlink: HyperlinkOption,
pub header: Header,
pub git: Git,
pub should_quote: bool,
}

Expand Down Expand Up @@ -101,6 +104,7 @@ impl Flags {
symlink_arrow: SymlinkArrow::configure_from(matches, config),
hyperlink: HyperlinkOption::configure_from(matches, config),
header: Header::configure_from(matches, config),
git: Git::configure_from(matches, config),
should_quote: true,
})
}
Expand Down
78 changes: 78 additions & 0 deletions src/flags/git.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//! This module defines the [Git] 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 gits.
#[derive(Clone, Debug, Copy, PartialEq, Eq, Default)]
pub struct Git(pub bool);

impl Configurable<Self> for Git {
/// Get a potential `Git` value from [ArgMatches].
///
/// If the "git" argument is passed, this returns a `Git` with value `true` in a
/// [Some]. Otherwise this returns [None].
fn from_arg_matches(matches: &ArgMatches) -> Option<Self> {
if !cfg!(feature = "no-git") && matches.get_one("git") == Some(&true) {
Some(Self(true))
} else {
None
}
}

/// Get a potential `Git` value from a [Config].
///
/// If the `Config::git` has value,
/// this returns it as the value of the `Git`, in a [Some].
/// Otherwise this returns [None].
fn from_config(config: &Config) -> Option<Self> {
config.git.map(Self)
}
}

#[cfg(not(feature = "no-git"))]
#[cfg(test)]
mod test {
use super::Git;

use crate::app;
use crate::config_file::Config;
use crate::flags::Configurable;

#[test]
fn test_from_arg_matches_none() {
let argv = ["lsd"];
let matches = app::build().try_get_matches_from(argv).unwrap();
assert_eq!(None, Git::from_arg_matches(&matches));
}

#[test]
fn test_from_arg_matches_true() {
let argv = ["lsd", "--git"];
let matches = app::build().try_get_matches_from(argv).unwrap();
assert_eq!(Some(Git(true)), Git::from_arg_matches(&matches));
}

#[test]
fn test_from_config_none() {
assert_eq!(None, Git::from_config(&Config::with_none()));
}

#[test]
fn test_from_config_true() {
let mut c = Config::with_none();
c.git = Some(true);
assert_eq!(Some(Git(true)), Git::from_config(&c));
}

#[test]
fn test_from_config_false() {
let mut c = Config::with_none();
c.git = Some(false);
assert_eq!(Some(Git(false)), Git::from_config(&c));
}
}

0 comments on commit c5df43a

Please sign in to comment.