From 6099f2c14611319cd2d222c6fb779c1f317ce8fd Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 1 Sep 2022 20:44:22 -0500 Subject: [PATCH] refactor: Move off of value_of --- src/bin/bat/app.rs | 102 +++++++++++++++++++++++++------------------- src/bin/bat/main.rs | 10 +++-- 2 files changed, 65 insertions(+), 47 deletions(-) diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index f9f0044f..293edbdf 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -79,7 +79,7 @@ impl App { pub fn config(&self, inputs: &[Input]) -> Result { let style_components = self.style_components()?; - let paging_mode = match self.matches.value_of("paging") { + let paging_mode = match self.matches.get_one::("paging").map(|s| s.as_str()) { Some("always") => PagingMode::Always, Some("never") => PagingMode::Never, Some("auto") | None => { @@ -107,13 +107,13 @@ impl App { let mut syntax_mapping = SyntaxMapping::builtin(); - if let Some(values) = self.matches.values_of("ignored-suffix") { + if let Some(values) = self.matches.get_many::("ignored-suffix") { for suffix in values { syntax_mapping.insert_ignored_suffix(suffix); } } - if let Some(values) = self.matches.values_of("map-syntax") { + if let Some(values) = self.matches.get_many::("map-syntax") { for from_to in values { let parts: Vec<_> = from_to.split(':').collect(); @@ -125,36 +125,43 @@ impl App { } } - let maybe_term_width = self.matches.value_of("terminal-width").and_then(|w| { - if w.starts_with('+') || w.starts_with('-') { - // Treat argument as a delta to the current terminal width - w.parse().ok().map(|delta: i16| { - let old_width: u16 = Term::stdout().size().1; - let new_width: i32 = i32::from(old_width) + i32::from(delta); + let maybe_term_width = self + .matches + .get_one::("terminal-width") + .and_then(|w| { + if w.starts_with('+') || w.starts_with('-') { + // Treat argument as a delta to the current terminal width + w.parse().ok().map(|delta: i16| { + let old_width: u16 = Term::stdout().size().1; + let new_width: i32 = i32::from(old_width) + i32::from(delta); - if new_width <= 0 { - old_width as usize - } else { - new_width as usize - } - }) - } else { - w.parse().ok() - } - }); + if new_width <= 0 { + old_width as usize + } else { + new_width as usize + } + }) + } else { + w.parse().ok() + } + }); Ok(Config { true_color: is_truecolor_terminal(), - language: self.matches.value_of("language").or_else(|| { - if self.matches.is_present("show-all") { - Some("show-nonprintable") - } else { - None - } - }), + language: self + .matches + .get_one::("language") + .map(|s| s.as_str()) + .or_else(|| { + if self.matches.is_present("show-all") { + Some("show-nonprintable") + } else { + None + } + }), show_nonprintable: self.matches.is_present("show-all"), wrapping_mode: if self.interactive_output || maybe_term_width.is_some() { - match self.matches.value_of("wrap") { + match self.matches.get_one::("wrap").map(|s| s.as_str()) { Some("character") => WrappingMode::Character, Some("never") => WrappingMode::NoWrapping(true), Some("auto") | None => { @@ -172,7 +179,7 @@ impl App { WrappingMode::NoWrapping(false) }, colored_output: self.matches.is_present("force-colorization") - || match self.matches.value_of("color") { + || match self.matches.get_one::("color").map(|s| s.as_str()) { Some("always") => true, Some("never") => false, Some("auto") => env::var_os("NO_COLOR").is_none() && self.interactive_output, @@ -181,12 +188,16 @@ impl App { paging_mode, term_width: maybe_term_width.unwrap_or(Term::stdout().size().1 as usize), loop_through: !(self.interactive_output - || self.matches.value_of("color") == Some("always") - || self.matches.value_of("decorations") == Some("always") + || self.matches.get_one::("color").map(|s| s.as_str()) == Some("always") + || self + .matches + .get_one::("decorations") + .map(|s| s.as_str()) + == Some("always") || self.matches.is_present("force-colorization")), tab_width: self .matches - .value_of("tabs") + .get_one::("tabs") .map(String::from) .or_else(|| env::var("BAT_TABS").ok()) .and_then(|t| t.parse().ok()) @@ -199,7 +210,7 @@ impl App { ), theme: self .matches - .value_of("theme") + .get_one::("theme") .map(String::from) .or_else(|| env::var("BAT_THEME").ok()) .map(|s| { @@ -214,15 +225,15 @@ impl App { #[cfg(feature = "git")] true => VisibleLines::DiffContext( self.matches - .value_of("diff-context") + .get_one::("diff-context") .and_then(|t| t.parse().ok()) .unwrap_or(2), ), _ => VisibleLines::Ranges( self.matches - .values_of("line-range") - .map(|vs| vs.map(LineRange::from).collect()) + .get_many::("line-range") + .map(|vs| vs.map(|s| LineRange::from(s.as_str())).collect()) .transpose()? .map(LineRanges::from) .unwrap_or_default(), @@ -230,12 +241,16 @@ impl App { }, style_components, syntax_mapping, - pager: self.matches.value_of("pager"), - use_italic_text: self.matches.value_of("italic-text") == Some("always"), + pager: self.matches.get_one::("pager").map(|s| s.as_str()), + use_italic_text: self + .matches + .get_one::("italic-text") + .map(|s| s.as_str()) + == Some("always"), highlighted_lines: self .matches - .values_of("highlight-line") - .map(|ws| ws.map(LineRange::from).collect()) + .get_many::("highlight-line") + .map(|ws| ws.map(|s| LineRange::from(s.as_str())).collect()) .transpose()? .map(LineRanges::from) .map(HighlightedLineRanges) @@ -292,8 +307,8 @@ impl App { fn style_components(&self) -> Result { let matches = &self.matches; - let mut styled_components = - StyleComponents(if matches.value_of("decorations") == Some("never") { + let mut styled_components = StyleComponents( + if matches.get_one::("decorations").map(|s| s.as_str()) == Some("never") { HashSet::new() } else if matches.is_present("number") { [StyleComponent::LineNumbers].iter().cloned().collect() @@ -311,7 +326,7 @@ impl App { .transpose()?; matches - .value_of("style") + .get_one::("style") .map(|styles| { styles .split(',') @@ -327,7 +342,8 @@ impl App { acc.extend(components.iter().cloned()); acc }) - }); + }, + ); // If `grid` is set, remove `rule` as it is a subset of `grid`, and print a warning. if styled_components.grid() && styled_components.0.remove(&StyleComponent::Rule) { diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs index 39023fca..f48b8b0a 100644 --- a/src/bin/bat/main.rs +++ b/src/bin/bat/main.rs @@ -39,11 +39,11 @@ const THEME_PREVIEW_DATA: &[u8] = include_bytes!("../../../assets/theme_preview. #[cfg(feature = "build-assets")] fn build_assets(matches: &clap::ArgMatches) -> Result<()> { let source_dir = matches - .value_of("source") + .get_one::("source") .map(Path::new) .unwrap_or_else(|| PROJECT_DIRS.config_dir()); let target_dir = matches - .value_of("target") + .get_one::("target") .map(Path::new) .unwrap_or_else(|| PROJECT_DIRS.cache_dir()); @@ -227,8 +227,10 @@ fn run_controller(inputs: Vec, config: &Config) -> Result { #[cfg(feature = "bugreport")] fn invoke_bugreport(app: &App) { use bugreport::{bugreport, collector::*, format::Markdown}; - let pager = bat::config::get_pager_executable(app.matches.value_of("pager")) - .unwrap_or_else(|| "less".to_owned()); // FIXME: Avoid non-canonical path to "less". + let pager = bat::config::get_pager_executable( + app.matches.get_one::("pager").map(|s| s.as_str()), + ) + .unwrap_or_else(|| "less".to_owned()); // FIXME: Avoid non-canonical path to "less". let mut custom_assets_metadata = PROJECT_DIRS.cache_dir().to_path_buf(); custom_assets_metadata.push("metadata.yaml");