Skip to content

Commit 6099f2c

Browse files
epagesharkdp
authored andcommitted
refactor: Move off of value_of
1 parent 50bb924 commit 6099f2c

File tree

2 files changed

+66
-48
lines changed

2 files changed

+66
-48
lines changed

src/bin/bat/app.rs

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl App {
7979
pub fn config(&self, inputs: &[Input]) -> Result<Config> {
8080
let style_components = self.style_components()?;
8181

82-
let paging_mode = match self.matches.value_of("paging") {
82+
let paging_mode = match self.matches.get_one::<String>("paging").map(|s| s.as_str()) {
8383
Some("always") => PagingMode::Always,
8484
Some("never") => PagingMode::Never,
8585
Some("auto") | None => {
@@ -107,13 +107,13 @@ impl App {
107107

108108
let mut syntax_mapping = SyntaxMapping::builtin();
109109

110-
if let Some(values) = self.matches.values_of("ignored-suffix") {
110+
if let Some(values) = self.matches.get_many::<String>("ignored-suffix") {
111111
for suffix in values {
112112
syntax_mapping.insert_ignored_suffix(suffix);
113113
}
114114
}
115115

116-
if let Some(values) = self.matches.values_of("map-syntax") {
116+
if let Some(values) = self.matches.get_many::<String>("map-syntax") {
117117
for from_to in values {
118118
let parts: Vec<_> = from_to.split(':').collect();
119119

@@ -125,36 +125,43 @@ impl App {
125125
}
126126
}
127127

128-
let maybe_term_width = self.matches.value_of("terminal-width").and_then(|w| {
129-
if w.starts_with('+') || w.starts_with('-') {
130-
// Treat argument as a delta to the current terminal width
131-
w.parse().ok().map(|delta: i16| {
132-
let old_width: u16 = Term::stdout().size().1;
133-
let new_width: i32 = i32::from(old_width) + i32::from(delta);
134-
135-
if new_width <= 0 {
136-
old_width as usize
137-
} else {
138-
new_width as usize
139-
}
140-
})
141-
} else {
142-
w.parse().ok()
143-
}
144-
});
128+
let maybe_term_width = self
129+
.matches
130+
.get_one::<String>("terminal-width")
131+
.and_then(|w| {
132+
if w.starts_with('+') || w.starts_with('-') {
133+
// Treat argument as a delta to the current terminal width
134+
w.parse().ok().map(|delta: i16| {
135+
let old_width: u16 = Term::stdout().size().1;
136+
let new_width: i32 = i32::from(old_width) + i32::from(delta);
137+
138+
if new_width <= 0 {
139+
old_width as usize
140+
} else {
141+
new_width as usize
142+
}
143+
})
144+
} else {
145+
w.parse().ok()
146+
}
147+
});
145148

146149
Ok(Config {
147150
true_color: is_truecolor_terminal(),
148-
language: self.matches.value_of("language").or_else(|| {
149-
if self.matches.is_present("show-all") {
150-
Some("show-nonprintable")
151-
} else {
152-
None
153-
}
154-
}),
151+
language: self
152+
.matches
153+
.get_one::<String>("language")
154+
.map(|s| s.as_str())
155+
.or_else(|| {
156+
if self.matches.is_present("show-all") {
157+
Some("show-nonprintable")
158+
} else {
159+
None
160+
}
161+
}),
155162
show_nonprintable: self.matches.is_present("show-all"),
156163
wrapping_mode: if self.interactive_output || maybe_term_width.is_some() {
157-
match self.matches.value_of("wrap") {
164+
match self.matches.get_one::<String>("wrap").map(|s| s.as_str()) {
158165
Some("character") => WrappingMode::Character,
159166
Some("never") => WrappingMode::NoWrapping(true),
160167
Some("auto") | None => {
@@ -172,7 +179,7 @@ impl App {
172179
WrappingMode::NoWrapping(false)
173180
},
174181
colored_output: self.matches.is_present("force-colorization")
175-
|| match self.matches.value_of("color") {
182+
|| match self.matches.get_one::<String>("color").map(|s| s.as_str()) {
176183
Some("always") => true,
177184
Some("never") => false,
178185
Some("auto") => env::var_os("NO_COLOR").is_none() && self.interactive_output,
@@ -181,12 +188,16 @@ impl App {
181188
paging_mode,
182189
term_width: maybe_term_width.unwrap_or(Term::stdout().size().1 as usize),
183190
loop_through: !(self.interactive_output
184-
|| self.matches.value_of("color") == Some("always")
185-
|| self.matches.value_of("decorations") == Some("always")
191+
|| self.matches.get_one::<String>("color").map(|s| s.as_str()) == Some("always")
192+
|| self
193+
.matches
194+
.get_one::<String>("decorations")
195+
.map(|s| s.as_str())
196+
== Some("always")
186197
|| self.matches.is_present("force-colorization")),
187198
tab_width: self
188199
.matches
189-
.value_of("tabs")
200+
.get_one::<String>("tabs")
190201
.map(String::from)
191202
.or_else(|| env::var("BAT_TABS").ok())
192203
.and_then(|t| t.parse().ok())
@@ -199,7 +210,7 @@ impl App {
199210
),
200211
theme: self
201212
.matches
202-
.value_of("theme")
213+
.get_one::<String>("theme")
203214
.map(String::from)
204215
.or_else(|| env::var("BAT_THEME").ok())
205216
.map(|s| {
@@ -214,28 +225,32 @@ impl App {
214225
#[cfg(feature = "git")]
215226
true => VisibleLines::DiffContext(
216227
self.matches
217-
.value_of("diff-context")
228+
.get_one::<String>("diff-context")
218229
.and_then(|t| t.parse().ok())
219230
.unwrap_or(2),
220231
),
221232

222233
_ => VisibleLines::Ranges(
223234
self.matches
224-
.values_of("line-range")
225-
.map(|vs| vs.map(LineRange::from).collect())
235+
.get_many::<String>("line-range")
236+
.map(|vs| vs.map(|s| LineRange::from(s.as_str())).collect())
226237
.transpose()?
227238
.map(LineRanges::from)
228239
.unwrap_or_default(),
229240
),
230241
},
231242
style_components,
232243
syntax_mapping,
233-
pager: self.matches.value_of("pager"),
234-
use_italic_text: self.matches.value_of("italic-text") == Some("always"),
244+
pager: self.matches.get_one::<String>("pager").map(|s| s.as_str()),
245+
use_italic_text: self
246+
.matches
247+
.get_one::<String>("italic-text")
248+
.map(|s| s.as_str())
249+
== Some("always"),
235250
highlighted_lines: self
236251
.matches
237-
.values_of("highlight-line")
238-
.map(|ws| ws.map(LineRange::from).collect())
252+
.get_many::<String>("highlight-line")
253+
.map(|ws| ws.map(|s| LineRange::from(s.as_str())).collect())
239254
.transpose()?
240255
.map(LineRanges::from)
241256
.map(HighlightedLineRanges)
@@ -292,8 +307,8 @@ impl App {
292307

293308
fn style_components(&self) -> Result<StyleComponents> {
294309
let matches = &self.matches;
295-
let mut styled_components =
296-
StyleComponents(if matches.value_of("decorations") == Some("never") {
310+
let mut styled_components = StyleComponents(
311+
if matches.get_one::<String>("decorations").map(|s| s.as_str()) == Some("never") {
297312
HashSet::new()
298313
} else if matches.is_present("number") {
299314
[StyleComponent::LineNumbers].iter().cloned().collect()
@@ -311,7 +326,7 @@ impl App {
311326
.transpose()?;
312327

313328
matches
314-
.value_of("style")
329+
.get_one::<String>("style")
315330
.map(|styles| {
316331
styles
317332
.split(',')
@@ -327,7 +342,8 @@ impl App {
327342
acc.extend(components.iter().cloned());
328343
acc
329344
})
330-
});
345+
},
346+
);
331347

332348
// If `grid` is set, remove `rule` as it is a subset of `grid`, and print a warning.
333349
if styled_components.grid() && styled_components.0.remove(&StyleComponent::Rule) {

src/bin/bat/main.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ const THEME_PREVIEW_DATA: &[u8] = include_bytes!("../../../assets/theme_preview.
3939
#[cfg(feature = "build-assets")]
4040
fn build_assets(matches: &clap::ArgMatches) -> Result<()> {
4141
let source_dir = matches
42-
.value_of("source")
42+
.get_one::<String>("source")
4343
.map(Path::new)
4444
.unwrap_or_else(|| PROJECT_DIRS.config_dir());
4545
let target_dir = matches
46-
.value_of("target")
46+
.get_one::<String>("target")
4747
.map(Path::new)
4848
.unwrap_or_else(|| PROJECT_DIRS.cache_dir());
4949

@@ -227,8 +227,10 @@ fn run_controller(inputs: Vec<Input>, config: &Config) -> Result<bool> {
227227
#[cfg(feature = "bugreport")]
228228
fn invoke_bugreport(app: &App) {
229229
use bugreport::{bugreport, collector::*, format::Markdown};
230-
let pager = bat::config::get_pager_executable(app.matches.value_of("pager"))
231-
.unwrap_or_else(|| "less".to_owned()); // FIXME: Avoid non-canonical path to "less".
230+
let pager = bat::config::get_pager_executable(
231+
app.matches.get_one::<String>("pager").map(|s| s.as_str()),
232+
)
233+
.unwrap_or_else(|| "less".to_owned()); // FIXME: Avoid non-canonical path to "less".
232234

233235
let mut custom_assets_metadata = PROJECT_DIRS.cache_dir().to_path_buf();
234236
custom_assets_metadata.push("metadata.yaml");

0 commit comments

Comments
 (0)