1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-01-18 20:11:03 +00:00

Rename field name, new constructors

This commit is contained in:
sharkdp 2020-04-11 19:17:36 +02:00 committed by David Peter
parent 04fa84aea7
commit 90e7d2fe33
6 changed files with 27 additions and 17 deletions

View File

@ -26,7 +26,7 @@ fn main() {
]), ]),
files: files files: files
.iter() .iter()
.map(|file| InputFile::Ordinary(OrdinaryFile::new(file, None))) .map(|file| InputFile::Ordinary(OrdinaryFile::from_path(file)))
.collect(), .collect(),
..Default::default() ..Default::default()
}; };

View File

@ -9,9 +9,8 @@ fn main() {
let path_to_this_file = OsStr::new(file!()); let path_to_this_file = OsStr::new(file!());
let config = Config { let config = Config {
files: vec![InputFile::Ordinary(OrdinaryFile::new( files: vec![InputFile::Ordinary(OrdinaryFile::from_path(
path_to_this_file, path_to_this_file,
None,
))], ))],
colored_output: true, colored_output: true,
true_color: true, true_color: true,

View File

@ -267,7 +267,7 @@ mod tests {
writeln!(temp_file, "{}", first_line).unwrap(); writeln!(temp_file, "{}", first_line).unwrap();
} }
let input_file = InputFile::Ordinary(OrdinaryFile::new(OsStr::new(&file_path), None)); let input_file = InputFile::Ordinary(OrdinaryFile::from_path(OsStr::new(&file_path)));
let syntax = self.assets.get_syntax( let syntax = self.assets.get_syntax(
None, None,
input_file, input_file,

View File

@ -265,7 +265,9 @@ impl App {
if input.to_str().unwrap() == "-" { if input.to_str().unwrap() == "-" {
file_input.push(InputFile::StdIn(name)); file_input.push(InputFile::StdIn(name));
} else { } else {
file_input.push(InputFile::Ordinary(OrdinaryFile::new(input, name))) file_input.push(InputFile::Ordinary(OrdinaryFile::from_path_with_name(
input, name,
)))
} }
} }
None => {} None => {}

View File

@ -167,10 +167,9 @@ fn run() -> Result<bool> {
Ok(true) Ok(true)
} else { } else {
let mut config = app.config()?; let mut config = app.config()?;
config.files = vec![InputFile::Ordinary(OrdinaryFile::new( config.files = vec![InputFile::Ordinary(OrdinaryFile::from_path(OsStr::new(
OsStr::new("cache"), "cache",
None, )))];
))];
run_controller(&config) run_controller(&config)
} }

View File

@ -54,20 +54,30 @@ impl<'a> InputFileReader<'a> {
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct OrdinaryFile<'a> { pub struct OrdinaryFile<'a> {
filename: &'a OsStr, path: &'a OsStr,
user_provided_name: Option<&'a OsStr>, user_provided_name: Option<&'a OsStr>,
} }
impl<'a> OrdinaryFile<'a> { impl<'a> OrdinaryFile<'a> {
pub fn new(filename: &'a OsStr, user_provided_name: Option<&'a OsStr>) -> OrdinaryFile<'a> { pub fn from_path(path: &'a OsStr) -> OrdinaryFile<'a> {
OrdinaryFile { OrdinaryFile {
filename: filename, path,
user_provided_name: user_provided_name, user_provided_name: None,
}
}
pub fn from_path_with_name(
path: &'a OsStr,
user_provided_name: Option<&'a OsStr>,
) -> OrdinaryFile<'a> {
OrdinaryFile {
path,
user_provided_name,
} }
} }
pub fn filename(&self) -> &'a OsStr { pub fn filename(&self) -> &'a OsStr {
self.user_provided_name.unwrap_or_else(|| self.filename) self.user_provided_name.unwrap_or_else(|| self.path)
} }
} }
@ -83,12 +93,12 @@ impl<'a> InputFile<'a> {
match self { match self {
InputFile::StdIn(_) => Ok(InputFileReader::new(stdin.lock())), InputFile::StdIn(_) => Ok(InputFileReader::new(stdin.lock())),
InputFile::Ordinary(ofile) => { InputFile::Ordinary(ofile) => {
let file = File::open(ofile.filename) let file = File::open(ofile.path)
.map_err(|e| format!("'{}': {}", ofile.filename.to_string_lossy(), e))?; .map_err(|e| format!("'{}': {}", ofile.path.to_string_lossy(), e))?;
if file.metadata()?.is_dir() { if file.metadata()?.is_dir() {
return Err( return Err(
format!("'{}' is a directory.", ofile.filename.to_string_lossy()).into(), format!("'{}' is a directory.", ofile.path.to_string_lossy()).into(),
); );
} }