refactor: Move off of value_of

This commit is contained in:
Ed Page 2022-09-01 20:44:22 -05:00 committed by David Peter
parent 50bb924ee3
commit 6099f2c146
2 changed files with 65 additions and 47 deletions

View file

@ -79,7 +79,7 @@ impl App {
pub fn config(&self, inputs: &[Input]) -> Result<Config> {
let style_components = self.style_components()?;
let paging_mode = match self.matches.value_of("paging") {
let paging_mode = match self.matches.get_one::<String>("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::<String>("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::<String>("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::<String>("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::<String>("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::<String>("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::<String>("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::<String>("color").map(|s| s.as_str()) == Some("always")
|| self
.matches
.get_one::<String>("decorations")
.map(|s| s.as_str())
== Some("always")
|| self.matches.is_present("force-colorization")),
tab_width: self
.matches
.value_of("tabs")
.get_one::<String>("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::<String>("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::<String>("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::<String>("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::<String>("pager").map(|s| s.as_str()),
use_italic_text: self
.matches
.get_one::<String>("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::<String>("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<StyleComponents> {
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::<String>("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::<String>("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) {

View file

@ -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::<String>("source")
.map(Path::new)
.unwrap_or_else(|| PROJECT_DIRS.config_dir());
let target_dir = matches
.value_of("target")
.get_one::<String>("target")
.map(Path::new)
.unwrap_or_else(|| PROJECT_DIRS.cache_dir());
@ -227,8 +227,10 @@ fn run_controller(inputs: Vec<Input>, config: &Config) -> Result<bool> {
#[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::<String>("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");