mirror of
https://github.com/sharkdp/bat
synced 2024-11-23 12:23:19 +00:00
refactor: Switch from is_present to actions
Because "diff" is conditionally present, we need to check for it to avoid some of clap's stricter checks meant to prevent bugs.
This commit is contained in:
parent
6099f2c146
commit
3d398b35c3
3 changed files with 46 additions and 23 deletions
|
@ -85,7 +85,7 @@ impl App {
|
|||
Some("auto") | None => {
|
||||
// If we have -pp as an option when in auto mode, the pager should be disabled.
|
||||
let extra_plain = self.matches.get_count("plain") > 1;
|
||||
if extra_plain || self.matches.is_present("no-paging") {
|
||||
if extra_plain || self.matches.get_flag("no-paging") {
|
||||
PagingMode::Never
|
||||
} else if inputs.iter().any(Input::is_stdin) {
|
||||
// If we are reading from stdin, only enable paging if we write to an
|
||||
|
@ -153,13 +153,13 @@ impl App {
|
|||
.get_one::<String>("language")
|
||||
.map(|s| s.as_str())
|
||||
.or_else(|| {
|
||||
if self.matches.is_present("show-all") {
|
||||
if self.matches.get_flag("show-all") {
|
||||
Some("show-nonprintable")
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}),
|
||||
show_nonprintable: self.matches.is_present("show-all"),
|
||||
show_nonprintable: self.matches.get_flag("show-all"),
|
||||
wrapping_mode: if self.interactive_output || maybe_term_width.is_some() {
|
||||
match self.matches.get_one::<String>("wrap").map(|s| s.as_str()) {
|
||||
Some("character") => WrappingMode::Character,
|
||||
|
@ -178,7 +178,7 @@ impl App {
|
|||
// There's no point in wrapping when this is the case.
|
||||
WrappingMode::NoWrapping(false)
|
||||
},
|
||||
colored_output: self.matches.is_present("force-colorization")
|
||||
colored_output: self.matches.get_flag("force-colorization")
|
||||
|| match self.matches.get_one::<String>("color").map(|s| s.as_str()) {
|
||||
Some("always") => true,
|
||||
Some("never") => false,
|
||||
|
@ -194,7 +194,7 @@ impl App {
|
|||
.get_one::<String>("decorations")
|
||||
.map(|s| s.as_str())
|
||||
== Some("always")
|
||||
|| self.matches.is_present("force-colorization")),
|
||||
|| self.matches.get_flag("force-colorization")),
|
||||
tab_width: self
|
||||
.matches
|
||||
.get_one::<String>("tabs")
|
||||
|
@ -221,7 +221,7 @@ impl App {
|
|||
}
|
||||
})
|
||||
.unwrap_or_else(|| String::from(HighlightingAssets::default_theme())),
|
||||
visible_lines: match self.matches.is_present("diff") {
|
||||
visible_lines: match self.matches.contains_id("diff") && self.matches.get_flag("diff") {
|
||||
#[cfg(feature = "git")]
|
||||
true => VisibleLines::DiffContext(
|
||||
self.matches
|
||||
|
@ -255,7 +255,7 @@ impl App {
|
|||
.map(LineRanges::from)
|
||||
.map(HighlightedLineRanges)
|
||||
.unwrap_or_default(),
|
||||
use_custom_assets: !self.matches.is_present("no-custom-assets"),
|
||||
use_custom_assets: !self.matches.get_flag("no-custom-assets"),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -310,7 +310,7 @@ impl App {
|
|||
let mut styled_components = StyleComponents(
|
||||
if matches.get_one::<String>("decorations").map(|s| s.as_str()) == Some("never") {
|
||||
HashSet::new()
|
||||
} else if matches.is_present("number") {
|
||||
} else if matches.get_flag("number") {
|
||||
[StyleComponent::LineNumbers].iter().cloned().collect()
|
||||
} else if 0 < matches.get_count("plain") {
|
||||
[StyleComponent::Plain].iter().cloned().collect()
|
||||
|
|
|
@ -60,6 +60,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
|
|||
.long("show-all")
|
||||
.alias("show-nonprintable")
|
||||
.short('A')
|
||||
.action(ArgAction::SetTrue)
|
||||
.conflicts_with("language")
|
||||
.help("Show non-printable characters (space, tab, newline, ..).")
|
||||
.long_help(
|
||||
|
@ -137,6 +138,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
|
|||
Arg::new("diff")
|
||||
.long("diff")
|
||||
.short('d')
|
||||
.action(ArgAction::SetTrue)
|
||||
.conflicts_with("line-range")
|
||||
.help("Only show lines that have been added/removed/modified.")
|
||||
.long_help(
|
||||
|
@ -229,6 +231,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
|
|||
.long("number")
|
||||
.overrides_with("number")
|
||||
.short('n')
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Show line numbers (alias for '--style=numbers').")
|
||||
.long_help(
|
||||
"Only show line numbers, no other decorations. This is an alias for \
|
||||
|
@ -283,6 +286,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
|
|||
Arg::new("force-colorization")
|
||||
.long("force-colorization")
|
||||
.short('f')
|
||||
.action(ArgAction::SetTrue)
|
||||
.conflicts_with("color")
|
||||
.conflicts_with("decorations")
|
||||
.overrides_with("force-colorization")
|
||||
|
@ -314,6 +318,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
|
|||
.short('P')
|
||||
.long("no-paging")
|
||||
.alias("no-pager")
|
||||
.action(ArgAction::SetTrue)
|
||||
.overrides_with("no-paging")
|
||||
.hide(true)
|
||||
.hide_short_help(true)
|
||||
|
@ -379,6 +384,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
|
|||
.arg(
|
||||
Arg::new("list-themes")
|
||||
.long("list-themes")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Display all supported highlighting themes.")
|
||||
.long_help("Display a list of supported themes for syntax highlighting."),
|
||||
)
|
||||
|
@ -469,6 +475,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
|
|||
Arg::new("list-languages")
|
||||
.long("list-languages")
|
||||
.short('L')
|
||||
.action(ArgAction::SetTrue)
|
||||
.conflicts_with("list-themes")
|
||||
.help("Display all supported languages.")
|
||||
.long_help("Display a list of supported languages for syntax highlighting."),
|
||||
|
@ -493,12 +500,14 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
|
|||
.arg(
|
||||
Arg::new("no-custom-assets")
|
||||
.long("no-custom-assets")
|
||||
.action(ArgAction::SetTrue)
|
||||
.hide(true)
|
||||
.help("Do not load custom assets"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("config-file")
|
||||
.long("config-file")
|
||||
.action(ArgAction::SetTrue)
|
||||
.conflicts_with("list-languages")
|
||||
.conflicts_with("list-themes")
|
||||
.hide(true)
|
||||
|
@ -507,6 +516,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
|
|||
.arg(
|
||||
Arg::new("generate-config-file")
|
||||
.long("generate-config-file")
|
||||
.action(ArgAction::SetTrue)
|
||||
.conflicts_with("list-languages")
|
||||
.conflicts_with("list-themes")
|
||||
.hide(true)
|
||||
|
@ -515,12 +525,14 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
|
|||
.arg(
|
||||
Arg::new("config-dir")
|
||||
.long("config-dir")
|
||||
.action(ArgAction::SetTrue)
|
||||
.hide(true)
|
||||
.help("Show bat's configuration directory."),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("cache-dir")
|
||||
.long("cache-dir")
|
||||
.action(ArgAction::SetTrue)
|
||||
.hide(true)
|
||||
.help("Show bat's cache directory."),
|
||||
)
|
||||
|
@ -528,12 +540,14 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
|
|||
Arg::new("diagnostic")
|
||||
.long("diagnostic")
|
||||
.alias("diagnostics")
|
||||
.action(ArgAction::SetTrue)
|
||||
.hide_short_help(true)
|
||||
.help("Show diagnostic information for bug reports.")
|
||||
)
|
||||
.arg(
|
||||
Arg::new("acknowledgements")
|
||||
.long("acknowledgements")
|
||||
.action(ArgAction::SetTrue)
|
||||
.hide_short_help(true)
|
||||
.help("Show acknowledgements."),
|
||||
)
|
||||
|
@ -551,6 +565,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
|
|||
Arg::new("build")
|
||||
.long("build")
|
||||
.short('b')
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Initialize (or update) the syntax/theme cache.")
|
||||
.long_help(
|
||||
"Initialize (or update) the syntax/theme cache by loading from \
|
||||
|
@ -561,6 +576,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
|
|||
Arg::new("clear")
|
||||
.long("clear")
|
||||
.short('c')
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Remove the cached syntax definitions and themes."),
|
||||
)
|
||||
.group(
|
||||
|
@ -586,13 +602,20 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
|
|||
"Use a different directory to store the cached syntax and theme set.",
|
||||
),
|
||||
)
|
||||
.arg(Arg::new("blank").long("blank").requires("build").help(
|
||||
"Create completely new syntax and theme sets \
|
||||
.arg(
|
||||
Arg::new("blank")
|
||||
.long("blank")
|
||||
.action(ArgAction::SetTrue)
|
||||
.requires("build")
|
||||
.help(
|
||||
"Create completely new syntax and theme sets \
|
||||
(instead of appending to the default sets).",
|
||||
))
|
||||
),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("acknowledgements")
|
||||
.long("acknowledgements")
|
||||
.action(ArgAction::SetTrue)
|
||||
.requires("build")
|
||||
.help("Build acknowledgements.bin."),
|
||||
),
|
||||
|
|
|
@ -49,20 +49,20 @@ fn build_assets(matches: &clap::ArgMatches) -> Result<()> {
|
|||
|
||||
bat::assets::build(
|
||||
source_dir,
|
||||
!matches.is_present("blank"),
|
||||
matches.is_present("acknowledgements"),
|
||||
!matches.get_flag("blank"),
|
||||
matches.get_flag("acknowledgements"),
|
||||
target_dir,
|
||||
clap::crate_version!(),
|
||||
)
|
||||
}
|
||||
|
||||
fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
|
||||
if matches.is_present("build") {
|
||||
if matches.get_flag("build") {
|
||||
#[cfg(feature = "build-assets")]
|
||||
build_assets(matches)?;
|
||||
#[cfg(not(feature = "build-assets"))]
|
||||
println!("bat has been built without the 'build-assets' feature. The 'cache --build' option is not available.");
|
||||
} else if matches.is_present("clear") {
|
||||
} else if matches.get_flag("clear") {
|
||||
clear_assets();
|
||||
}
|
||||
|
||||
|
@ -286,7 +286,7 @@ fn invoke_bugreport(app: &App) {
|
|||
fn run() -> Result<bool> {
|
||||
let app = App::new()?;
|
||||
|
||||
if app.matches.is_present("diagnostic") {
|
||||
if app.matches.get_flag("diagnostic") {
|
||||
#[cfg(feature = "bugreport")]
|
||||
invoke_bugreport(&app);
|
||||
#[cfg(not(feature = "bugreport"))]
|
||||
|
@ -313,7 +313,7 @@ fn run() -> Result<bool> {
|
|||
let inputs = app.inputs()?;
|
||||
let config = app.config(&inputs)?;
|
||||
|
||||
if app.matches.is_present("list-languages") {
|
||||
if app.matches.get_flag("list-languages") {
|
||||
let languages: String = get_languages(&config)?;
|
||||
let inputs: Vec<Input> = vec![Input::from_reader(Box::new(languages.as_bytes()))];
|
||||
let plain_config = Config {
|
||||
|
@ -322,22 +322,22 @@ fn run() -> Result<bool> {
|
|||
..Default::default()
|
||||
};
|
||||
run_controller(inputs, &plain_config)
|
||||
} else if app.matches.is_present("list-themes") {
|
||||
} else if app.matches.get_flag("list-themes") {
|
||||
list_themes(&config)?;
|
||||
Ok(true)
|
||||
} else if app.matches.is_present("config-file") {
|
||||
} else if app.matches.get_flag("config-file") {
|
||||
println!("{}", config_file().to_string_lossy());
|
||||
Ok(true)
|
||||
} else if app.matches.is_present("generate-config-file") {
|
||||
} else if app.matches.get_flag("generate-config-file") {
|
||||
generate_config_file()?;
|
||||
Ok(true)
|
||||
} else if app.matches.is_present("config-dir") {
|
||||
} else if app.matches.get_flag("config-dir") {
|
||||
writeln!(io::stdout(), "{}", config_dir())?;
|
||||
Ok(true)
|
||||
} else if app.matches.is_present("cache-dir") {
|
||||
} else if app.matches.get_flag("cache-dir") {
|
||||
writeln!(io::stdout(), "{}", cache_dir())?;
|
||||
Ok(true)
|
||||
} else if app.matches.is_present("acknowledgements") {
|
||||
} else if app.matches.get_flag("acknowledgements") {
|
||||
writeln!(io::stdout(), "{}", bat::assets::get_acknowledgements())?;
|
||||
Ok(true)
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue