on master help sign in
Improve readability
35347c2Aleksey Kladov
CHANGELOG.md+2
@@ -6,6 +6,8 @@
6
6
7## Other
7## Other
8
8
9+- `Input::ordinary_file` and `Input::with_name` now accept `Path` rather than `OsStr` see #1571 (@matklad)
10+
9## Syntaxes
11## Syntaxes
10
12
11## New themes
13## New themes
src/assets.rs+4−4
@@ -327,7 +327,7 @@ mod tests {
327 writeln!(temp_file, "{}", first_line).unwrap();
327 writeln!(temp_file, "{}", first_line).unwrap();
328 }
328 }
329
329
330− let input = Input::ordinary_file(file_path.as_os_str());
330+ let input = Input::ordinary_file(&file_path);
331 let dummy_stdin: &[u8] = &[];
331 let dummy_stdin: &[u8] = &[];
332 let mut opened_input = input.open(dummy_stdin, None).unwrap();
332 let mut opened_input = input.open(dummy_stdin, None).unwrap();
333
333
@@ -341,7 +341,7 @@ mod tests {
341 fn syntax_for_file_with_content_os(&self, file_name: &OsStr, first_line: &str) -> String {
341 fn syntax_for_file_with_content_os(&self, file_name: &OsStr, first_line: &str) -> String {
342 let file_path = self.temp_dir.path().join(file_name);
342 let file_path = self.temp_dir.path().join(file_name);
343 let input = Input::from_reader(Box::new(BufReader::new(first_line.as_bytes())))
343 let input = Input::from_reader(Box::new(BufReader::new(first_line.as_bytes())))
344− .with_name(Some(file_path.as_os_str()));
344+ .with_name(Some(&file_path));
345 let dummy_stdin: &[u8] = &[];
345 let dummy_stdin: &[u8] = &[];
346 let mut opened_input = input.open(dummy_stdin, None).unwrap();
346 let mut opened_input = input.open(dummy_stdin, None).unwrap();
347
347
@@ -366,7 +366,7 @@ mod tests {
366 }
366 }
367
367
368 fn syntax_for_stdin_with_content(&self, file_name: &str, content: &[u8]) -> String {
368 fn syntax_for_stdin_with_content(&self, file_name: &str, content: &[u8]) -> String {
369− let input = Input::stdin().with_name(Some(OsStr::new(file_name)));
369+ let input = Input::stdin().with_name(Some(file_name));
370 let mut opened_input = input.open(content, None).unwrap();
370 let mut opened_input = input.open(content, None).unwrap();
371
371
372 self.assets
372 self.assets
@@ -521,7 +521,7 @@ mod tests {
521 .expect("creation of directory succeeds");
521 .expect("creation of directory succeeds");
522 symlink(&file_path, &file_path_symlink).expect("creation of symbolic link succeeds");
522 symlink(&file_path, &file_path_symlink).expect("creation of symbolic link succeeds");
523
523
524− let input = Input::ordinary_file(file_path_symlink.as_os_str());
524+ let input = Input::ordinary_file(&file_path_symlink);
525 let dummy_stdin: &[u8] = &[];
525 let dummy_stdin: &[u8] = &[];
526 let mut opened_input = input.open(dummy_stdin, None).unwrap();
526 let mut opened_input = input.open(dummy_stdin, None).unwrap();
527
527
src/bin/bat/app.rs+10−7
@@ -1,6 +1,6 @@
1use std::collections::HashSet;
1use std::collections::HashSet;
2use std::env;
2use std::env;
3−use std::ffi::OsStr;
3+use std::path::Path;
4use std::str::FromStr;
4use std::str::FromStr;
5
5
6use atty::{self, Stream};
6use atty::{self, Stream};
@@ -248,16 +248,19 @@ impl App {
248 }
248 }
249 _ => {}
249 _ => {}
250 }
250 }
251− let filenames: Option<Vec<&str>> = self
251+ let filenames: Option<Vec<&Path>> = self
252 .matches
252 .matches
253− .values_of("file-name")
253+ .values_of_os("file-name")
254− .map(|values| values.collect());
254+ .map(|values| values.map(Path::new).collect());
255
255
256− let mut filenames_or_none: Box<dyn Iterator<Item = _>> = match filenames {
256+ let mut filenames_or_none: Box<dyn Iterator<Item = Option<&Path>>> = match filenames {
257− Some(ref filenames) => Box::new(filenames.iter().map(|name| Some(OsStr::new(*name)))),
257+ Some(filenames) => Box::new(filenames.into_iter().map(Some)),
258 None => Box::new(std::iter::repeat(None)),
258 None => Box::new(std::iter::repeat(None)),
259 };
259 };
260− let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
260+ let files: Option<Vec<&Path>> = self
261+ .matches
262+ .values_of_os("FILE")
263+ .map(|vs| vs.map(Path::new).collect());
261
264
262 if files.is_none() {
265 if files.is_none() {
263 return Ok(vec![new_stdin_input(
266 return Ok(vec![new_stdin_input(
src/bin/bat/input.rs+4−4
@@ -1,15 +1,15 @@
1use bat::input::Input;
1use bat::input::Input;
2−use std::ffi::OsStr;
2+use std::path::Path;
3
3
4−pub fn new_file_input<'a>(file: &'a OsStr, name: Option<&'a OsStr>) -> Input<'a> {
4+pub fn new_file_input<'a>(file: &'a Path, name: Option<&'a Path>) -> Input<'a> {
5 named(Input::ordinary_file(file), name.or(Some(file)))
5 named(Input::ordinary_file(file), name.or(Some(file)))
6}
6}
7
7
8−pub fn new_stdin_input(name: Option<&OsStr>) -> Input {
8+pub fn new_stdin_input(name: Option<&Path>) -> Input {
9 named(Input::stdin(), name)
9 named(Input::stdin(), name)
10}
10}
11
11
12−fn named<'a>(input: Input<'a>, name: Option<&OsStr>) -> Input<'a> {
12+fn named<'a>(input: Input<'a>, name: Option<&Path>) -> Input<'a> {
13 if let Some(provided_name) = name {
13 if let Some(provided_name) = name {
14 let mut input = input.with_name(Some(provided_name));
14 let mut input = input.with_name(Some(provided_name));
15 input.description_mut().set_kind(Some("File".to_owned()));
15 input.description_mut().set_kind(Some("File".to_owned()));
src/bin/bat/main.rs+1−2
@@ -9,7 +9,6 @@ mod directories;
9mod input;
9mod input;
10
10
11use std::collections::{HashMap, HashSet};
11use std::collections::{HashMap, HashSet};
12−use std::ffi::OsStr;
13use std::io;
12use std::io;
14use std::io::{BufReader, Write};
13use std::io::{BufReader, Write};
15use std::path::Path;
14use std::path::Path;
@@ -269,7 +268,7 @@ fn run() -> Result<bool> {
269 run_cache_subcommand(cache_matches)?;
268 run_cache_subcommand(cache_matches)?;
270 Ok(true)
269 Ok(true)
271 } else {
270 } else {
272− let inputs = vec![Input::ordinary_file(OsStr::new("cache"))];
271+ let inputs = vec![Input::ordinary_file("cache")];
273 let config = app.config(&inputs)?;
272 let config = app.config(&inputs)?;
274
273
275 run_controller(inputs, &config)
274 run_controller(inputs, &config)
src/diff.rs+1−2
@@ -1,7 +1,6 @@
1#![cfg(feature = "git")]
1#![cfg(feature = "git")]
2
2
3use std::collections::HashMap;
3use std::collections::HashMap;
4−use std::ffi::OsStr;
5use std::fs;
4use std::fs;
6use std::path::Path;
5use std::path::Path;
7
6
@@ -17,7 +16,7 @@ pub enum LineChange {
17
16
18pub type LineChanges = HashMap<u32, LineChange>;
17pub type LineChanges = HashMap<u32, LineChange>;
19
18
20−pub fn get_git_diff(filename: &OsStr) -> Option<LineChanges> {
19+pub fn get_git_diff(filename: &Path) -> Option<LineChanges> {
21 let repo = Repository::discover(&filename).ok()?;
20 let repo = Repository::discover(&filename).ok()?;
22
21
23 let repo_path_absolute = fs::canonicalize(repo.workdir()?).ok()?;
22 let repo_path_absolute = fs::canonicalize(repo.workdir()?).ok()?;
src/input.rs+13−7
@@ -1,7 +1,7 @@
1use std::convert::TryFrom;
1use std::convert::TryFrom;
2−use std::ffi::{OsStr, OsString};
3use std::fs::File;
2use std::fs::File;
4use std::io::{self, BufRead, BufReader, Read};
3use std::io::{self, BufRead, BufReader, Read};
4+use std::path::{Path, PathBuf};
5
5
6use clircle::{Clircle, Identifier};
6use clircle::{Clircle, Identifier};
7use content_inspector::{self, ContentType};
7use content_inspector::{self, ContentType};
@@ -69,7 +69,7 @@ impl InputDescription {
69}
69}
70
70
71pub(crate) enum InputKind<'a> {
71pub(crate) enum InputKind<'a> {
72− OrdinaryFile(OsString),
72+ OrdinaryFile(PathBuf),
73 StdIn,
73 StdIn,
74 CustomReader(Box<dyn Read + 'a>),
74 CustomReader(Box<dyn Read + 'a>),
75}
75}
@@ -86,7 +86,7 @@ impl<'a> InputKind<'a> {
86
86
87#[derive(Clone, Default)]
87#[derive(Clone, Default)]
88pub(crate) struct InputMetadata {
88pub(crate) struct InputMetadata {
89− pub(crate) user_provided_name: Option<OsString>,
89+ pub(crate) user_provided_name: Option<PathBuf>,
90}
90}
91
91
92pub struct Input<'a> {
92pub struct Input<'a> {
@@ -96,7 +96,7 @@ pub struct Input<'a> {
96}
96}
97
97
98pub(crate) enum OpenedInputKind {
98pub(crate) enum OpenedInputKind {
99− OrdinaryFile(OsString),
99+ OrdinaryFile(PathBuf),
100 StdIn,
100 StdIn,
101 CustomReader,
101 CustomReader,
102}
102}
@@ -109,8 +109,11 @@ pub(crate) struct OpenedInput<'a> {
109}
109}
110
110
111impl<'a> Input<'a> {
111impl<'a> Input<'a> {
112− pub fn ordinary_file(path: &OsStr) -> Self {
112+ pub fn ordinary_file(path: impl AsRef<Path>) -> Self {
113+ Self::_ordinary_file(path.as_ref())
114+ }
115+ fn _ordinary_file(path: &Path) -> Self {
113− let kind = InputKind::OrdinaryFile(path.to_os_string());
116+ let kind = InputKind::OrdinaryFile(path.to_path_buf());
114 Input {
117 Input {
115 description: kind.description(),
118 description: kind.description(),
116 metadata: InputMetadata::default(),
119 metadata: InputMetadata::default(),
@@ -140,7 +143,10 @@ impl<'a> Input<'a> {
140 matches!(self.kind, InputKind::StdIn)
143 matches!(self.kind, InputKind::StdIn)
141 }
144 }
142
145
143− pub fn with_name(mut self, provided_name: Option<&OsStr>) -> Self {
146+ pub fn with_name(mut self, provided_name: Option<impl AsRef<Path>>) -> Self {
147+ self._with_name(provided_name.as_ref().map(|it| it.as_ref()))
148+ }
149+ pub fn _with_name(mut self, provided_name: Option<&Path>) -> Self {
144 if let Some(name) = provided_name {
150 if let Some(name) = provided_name {
145 self.description.name = name.to_string_lossy().to_string()
151 self.description.name = name.to_string_lossy().to_string()
146 }
152 }
src/pretty_printer.rs+7−7
@@ -1,5 +1,5 @@
1−use std::ffi::OsStr;
2use std::io::Read;
1use std::io::Read;
2+use std::path::Path;
3
3
4use console::Term;
4use console::Term;
5use syntect::parsing::SyntaxReference;
5use syntect::parsing::SyntaxReference;
@@ -71,7 +71,7 @@ impl<'a> PrettyPrinter<'a> {
71 }
71 }
72
72
73 /// Add a file which should be pretty-printed
73 /// Add a file which should be pretty-printed
74− pub fn input_file(&mut self, path: impl AsRef<OsStr>) -> &mut Self {
74+ pub fn input_file(&mut self, path: impl AsRef<Path>) -> &mut Self {
75 self.input(Input::from_file(path).kind("File"))
75 self.input(Input::from_file(path).kind("File"))
76 }
76 }
77
77
@@ -79,7 +79,7 @@ impl<'a> PrettyPrinter<'a> {
79 pub fn input_files<I, P>(&mut self, paths: I) -> &mut Self
79 pub fn input_files<I, P>(&mut self, paths: I) -> &mut Self
80 where
80 where
81 I: IntoIterator<Item = P>,
81 I: IntoIterator<Item = P>,
82− P: AsRef<OsStr>,
82+ P: AsRef<Path>,
83 {
83 {
84 self.inputs(paths.into_iter().map(Input::from_file))
84 self.inputs(paths.into_iter().map(Input::from_file))
85 }
85 }
@@ -297,8 +297,8 @@ impl<'a> Input<'a> {
297 }
297 }
298
298
299 /// A new input from a file.
299 /// A new input from a file.
300− pub fn from_file(path: impl AsRef<OsStr>) -> Self {
300+ pub fn from_file(path: impl AsRef<Path>) -> Self {
301− input::Input::ordinary_file(path.as_ref()).into()
301+ input::Input::ordinary_file(path).into()
302 }
302 }
303
303
304 /// A new input from bytes.
304 /// A new input from bytes.
@@ -313,8 +313,8 @@ impl<'a> Input<'a> {
313
313
314 /// The filename of the input.
314 /// The filename of the input.
315 /// This affects syntax detection and changes the default header title.
315 /// This affects syntax detection and changes the default header title.
316− pub fn name(mut self, name: impl AsRef<OsStr>) -> Self {
316+ pub fn name(mut self, name: impl AsRef<Path>) -> Self {
317− self.input = self.input.with_name(Some(name.as_ref()));
317+ self.input = self.input.with_name(Some(name));
318 self
318 self
319 }
319 }
320
320