mirror of
https://github.com/sharkdp/bat.git
synced 2025-01-19 12:24:17 +00:00
refactor: Move off of value_of
This commit is contained in:
parent
50bb924ee3
commit
6099f2c146
@ -79,7 +79,7 @@ impl App {
|
|||||||
pub fn config(&self, inputs: &[Input]) -> Result<Config> {
|
pub fn config(&self, inputs: &[Input]) -> Result<Config> {
|
||||||
let style_components = self.style_components()?;
|
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("always") => PagingMode::Always,
|
||||||
Some("never") => PagingMode::Never,
|
Some("never") => PagingMode::Never,
|
||||||
Some("auto") | None => {
|
Some("auto") | None => {
|
||||||
@ -107,13 +107,13 @@ impl App {
|
|||||||
|
|
||||||
let mut syntax_mapping = SyntaxMapping::builtin();
|
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 {
|
for suffix in values {
|
||||||
syntax_mapping.insert_ignored_suffix(suffix);
|
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 {
|
for from_to in values {
|
||||||
let parts: Vec<_> = from_to.split(':').collect();
|
let parts: Vec<_> = from_to.split(':').collect();
|
||||||
|
|
||||||
@ -125,7 +125,10 @@ impl App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let maybe_term_width = self.matches.value_of("terminal-width").and_then(|w| {
|
let maybe_term_width = self
|
||||||
|
.matches
|
||||||
|
.get_one::<String>("terminal-width")
|
||||||
|
.and_then(|w| {
|
||||||
if w.starts_with('+') || w.starts_with('-') {
|
if w.starts_with('+') || w.starts_with('-') {
|
||||||
// Treat argument as a delta to the current terminal width
|
// Treat argument as a delta to the current terminal width
|
||||||
w.parse().ok().map(|delta: i16| {
|
w.parse().ok().map(|delta: i16| {
|
||||||
@ -145,7 +148,11 @@ impl App {
|
|||||||
|
|
||||||
Ok(Config {
|
Ok(Config {
|
||||||
true_color: is_truecolor_terminal(),
|
true_color: is_truecolor_terminal(),
|
||||||
language: self.matches.value_of("language").or_else(|| {
|
language: self
|
||||||
|
.matches
|
||||||
|
.get_one::<String>("language")
|
||||||
|
.map(|s| s.as_str())
|
||||||
|
.or_else(|| {
|
||||||
if self.matches.is_present("show-all") {
|
if self.matches.is_present("show-all") {
|
||||||
Some("show-nonprintable")
|
Some("show-nonprintable")
|
||||||
} else {
|
} else {
|
||||||
@ -154,7 +161,7 @@ impl App {
|
|||||||
}),
|
}),
|
||||||
show_nonprintable: self.matches.is_present("show-all"),
|
show_nonprintable: self.matches.is_present("show-all"),
|
||||||
wrapping_mode: if self.interactive_output || maybe_term_width.is_some() {
|
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("character") => WrappingMode::Character,
|
||||||
Some("never") => WrappingMode::NoWrapping(true),
|
Some("never") => WrappingMode::NoWrapping(true),
|
||||||
Some("auto") | None => {
|
Some("auto") | None => {
|
||||||
@ -172,7 +179,7 @@ impl App {
|
|||||||
WrappingMode::NoWrapping(false)
|
WrappingMode::NoWrapping(false)
|
||||||
},
|
},
|
||||||
colored_output: self.matches.is_present("force-colorization")
|
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("always") => true,
|
||||||
Some("never") => false,
|
Some("never") => false,
|
||||||
Some("auto") => env::var_os("NO_COLOR").is_none() && self.interactive_output,
|
Some("auto") => env::var_os("NO_COLOR").is_none() && self.interactive_output,
|
||||||
@ -181,12 +188,16 @@ impl App {
|
|||||||
paging_mode,
|
paging_mode,
|
||||||
term_width: maybe_term_width.unwrap_or(Term::stdout().size().1 as usize),
|
term_width: maybe_term_width.unwrap_or(Term::stdout().size().1 as usize),
|
||||||
loop_through: !(self.interactive_output
|
loop_through: !(self.interactive_output
|
||||||
|| self.matches.value_of("color") == Some("always")
|
|| self.matches.get_one::<String>("color").map(|s| s.as_str()) == Some("always")
|
||||||
|| self.matches.value_of("decorations") == Some("always")
|
|| self
|
||||||
|
.matches
|
||||||
|
.get_one::<String>("decorations")
|
||||||
|
.map(|s| s.as_str())
|
||||||
|
== Some("always")
|
||||||
|| self.matches.is_present("force-colorization")),
|
|| self.matches.is_present("force-colorization")),
|
||||||
tab_width: self
|
tab_width: self
|
||||||
.matches
|
.matches
|
||||||
.value_of("tabs")
|
.get_one::<String>("tabs")
|
||||||
.map(String::from)
|
.map(String::from)
|
||||||
.or_else(|| env::var("BAT_TABS").ok())
|
.or_else(|| env::var("BAT_TABS").ok())
|
||||||
.and_then(|t| t.parse().ok())
|
.and_then(|t| t.parse().ok())
|
||||||
@ -199,7 +210,7 @@ impl App {
|
|||||||
),
|
),
|
||||||
theme: self
|
theme: self
|
||||||
.matches
|
.matches
|
||||||
.value_of("theme")
|
.get_one::<String>("theme")
|
||||||
.map(String::from)
|
.map(String::from)
|
||||||
.or_else(|| env::var("BAT_THEME").ok())
|
.or_else(|| env::var("BAT_THEME").ok())
|
||||||
.map(|s| {
|
.map(|s| {
|
||||||
@ -214,15 +225,15 @@ impl App {
|
|||||||
#[cfg(feature = "git")]
|
#[cfg(feature = "git")]
|
||||||
true => VisibleLines::DiffContext(
|
true => VisibleLines::DiffContext(
|
||||||
self.matches
|
self.matches
|
||||||
.value_of("diff-context")
|
.get_one::<String>("diff-context")
|
||||||
.and_then(|t| t.parse().ok())
|
.and_then(|t| t.parse().ok())
|
||||||
.unwrap_or(2),
|
.unwrap_or(2),
|
||||||
),
|
),
|
||||||
|
|
||||||
_ => VisibleLines::Ranges(
|
_ => VisibleLines::Ranges(
|
||||||
self.matches
|
self.matches
|
||||||
.values_of("line-range")
|
.get_many::<String>("line-range")
|
||||||
.map(|vs| vs.map(LineRange::from).collect())
|
.map(|vs| vs.map(|s| LineRange::from(s.as_str())).collect())
|
||||||
.transpose()?
|
.transpose()?
|
||||||
.map(LineRanges::from)
|
.map(LineRanges::from)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
@ -230,12 +241,16 @@ impl App {
|
|||||||
},
|
},
|
||||||
style_components,
|
style_components,
|
||||||
syntax_mapping,
|
syntax_mapping,
|
||||||
pager: self.matches.value_of("pager"),
|
pager: self.matches.get_one::<String>("pager").map(|s| s.as_str()),
|
||||||
use_italic_text: self.matches.value_of("italic-text") == Some("always"),
|
use_italic_text: self
|
||||||
|
.matches
|
||||||
|
.get_one::<String>("italic-text")
|
||||||
|
.map(|s| s.as_str())
|
||||||
|
== Some("always"),
|
||||||
highlighted_lines: self
|
highlighted_lines: self
|
||||||
.matches
|
.matches
|
||||||
.values_of("highlight-line")
|
.get_many::<String>("highlight-line")
|
||||||
.map(|ws| ws.map(LineRange::from).collect())
|
.map(|ws| ws.map(|s| LineRange::from(s.as_str())).collect())
|
||||||
.transpose()?
|
.transpose()?
|
||||||
.map(LineRanges::from)
|
.map(LineRanges::from)
|
||||||
.map(HighlightedLineRanges)
|
.map(HighlightedLineRanges)
|
||||||
@ -292,8 +307,8 @@ impl App {
|
|||||||
|
|
||||||
fn style_components(&self) -> Result<StyleComponents> {
|
fn style_components(&self) -> Result<StyleComponents> {
|
||||||
let matches = &self.matches;
|
let matches = &self.matches;
|
||||||
let mut styled_components =
|
let mut styled_components = StyleComponents(
|
||||||
StyleComponents(if matches.value_of("decorations") == Some("never") {
|
if matches.get_one::<String>("decorations").map(|s| s.as_str()) == Some("never") {
|
||||||
HashSet::new()
|
HashSet::new()
|
||||||
} else if matches.is_present("number") {
|
} else if matches.is_present("number") {
|
||||||
[StyleComponent::LineNumbers].iter().cloned().collect()
|
[StyleComponent::LineNumbers].iter().cloned().collect()
|
||||||
@ -311,7 +326,7 @@ impl App {
|
|||||||
.transpose()?;
|
.transpose()?;
|
||||||
|
|
||||||
matches
|
matches
|
||||||
.value_of("style")
|
.get_one::<String>("style")
|
||||||
.map(|styles| {
|
.map(|styles| {
|
||||||
styles
|
styles
|
||||||
.split(',')
|
.split(',')
|
||||||
@ -327,7 +342,8 @@ impl App {
|
|||||||
acc.extend(components.iter().cloned());
|
acc.extend(components.iter().cloned());
|
||||||
acc
|
acc
|
||||||
})
|
})
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// If `grid` is set, remove `rule` as it is a subset of `grid`, and print a warning.
|
// 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) {
|
if styled_components.grid() && styled_components.0.remove(&StyleComponent::Rule) {
|
||||||
|
@ -39,11 +39,11 @@ const THEME_PREVIEW_DATA: &[u8] = include_bytes!("../../../assets/theme_preview.
|
|||||||
#[cfg(feature = "build-assets")]
|
#[cfg(feature = "build-assets")]
|
||||||
fn build_assets(matches: &clap::ArgMatches) -> Result<()> {
|
fn build_assets(matches: &clap::ArgMatches) -> Result<()> {
|
||||||
let source_dir = matches
|
let source_dir = matches
|
||||||
.value_of("source")
|
.get_one::<String>("source")
|
||||||
.map(Path::new)
|
.map(Path::new)
|
||||||
.unwrap_or_else(|| PROJECT_DIRS.config_dir());
|
.unwrap_or_else(|| PROJECT_DIRS.config_dir());
|
||||||
let target_dir = matches
|
let target_dir = matches
|
||||||
.value_of("target")
|
.get_one::<String>("target")
|
||||||
.map(Path::new)
|
.map(Path::new)
|
||||||
.unwrap_or_else(|| PROJECT_DIRS.cache_dir());
|
.unwrap_or_else(|| PROJECT_DIRS.cache_dir());
|
||||||
|
|
||||||
@ -227,7 +227,9 @@ fn run_controller(inputs: Vec<Input>, config: &Config) -> Result<bool> {
|
|||||||
#[cfg(feature = "bugreport")]
|
#[cfg(feature = "bugreport")]
|
||||||
fn invoke_bugreport(app: &App) {
|
fn invoke_bugreport(app: &App) {
|
||||||
use bugreport::{bugreport, collector::*, format::Markdown};
|
use bugreport::{bugreport, collector::*, format::Markdown};
|
||||||
let pager = bat::config::get_pager_executable(app.matches.value_of("pager"))
|
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".
|
.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();
|
let mut custom_assets_metadata = PROJECT_DIRS.cache_dir().to_path_buf();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user