mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-21 20:38:44 +00:00
Fix bug for file with invalid-utf8 filenames
This commit is contained in:
parent
47abb192bc
commit
82e20bfe14
@ -244,24 +244,20 @@ impl App {
|
|||||||
}
|
}
|
||||||
None => Box::new(std::iter::repeat(None)),
|
None => Box::new(std::iter::repeat(None)),
|
||||||
};
|
};
|
||||||
let files: Option<Vec<&str>> = self
|
let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
|
||||||
.matches
|
|
||||||
.values_of_os("FILE")
|
|
||||||
.map(|values| values.map(|fname| fname.to_str()).collect())
|
|
||||||
.unwrap_or(None);
|
|
||||||
|
|
||||||
if files.is_none() {
|
if files.is_none() {
|
||||||
return Ok(vec![InputFile::StdIn(filenames_or_none.nth(0).unwrap())]);
|
return Ok(vec![InputFile::StdIn(filenames_or_none.nth(0).unwrap())]);
|
||||||
}
|
}
|
||||||
let files_or_none: Box<dyn Iterator<Item = _>> = match files {
|
let files_or_none: Box<dyn Iterator<Item = _>> = match files {
|
||||||
Some(ref files) => Box::new(files.into_iter().map(|name| Some(OsStr::new(*name)))),
|
Some(ref files) => Box::new(files.into_iter().map(|name| Some(*name))),
|
||||||
None => Box::new(std::iter::repeat(None)),
|
None => Box::new(std::iter::repeat(None)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut file_input = Vec::new();
|
let mut file_input = Vec::new();
|
||||||
for (input, name) in files_or_none.zip(filenames_or_none) {
|
for (input, name) in files_or_none.zip(filenames_or_none) {
|
||||||
if let Some(input) = input {
|
if let Some(input) = input {
|
||||||
if input.to_str().unwrap() == "-" {
|
if input.to_str().unwrap_or_default() == "-" {
|
||||||
file_input.push(InputFile::StdIn(name));
|
file_input.push(InputFile::StdIn(name));
|
||||||
} else {
|
} else {
|
||||||
let mut ofile = OrdinaryFile::from_path(input);
|
let mut ofile = OrdinaryFile::from_path(input);
|
||||||
|
4
tests/examples/test-invalid-utf8-�(.rs
vendored
Normal file
4
tests/examples/test-invalid-utf8-�(.rs
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
fn print_square(num: f64) {
|
||||||
|
let result = f64::powf(num, 2.0);
|
||||||
|
println!("The square of {:.2} is {:.2}.", num, result);
|
||||||
|
}
|
@ -628,6 +628,18 @@ fn filename_multiple_err() {
|
|||||||
.failure();
|
.failure();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
#[test]
|
||||||
|
fn file_with_invalid_utf8_filename() {
|
||||||
|
use std::ffi::OsStr;
|
||||||
|
use std::os::unix::ffi::OsStrExt;
|
||||||
|
|
||||||
|
bat()
|
||||||
|
.arg(OsStr::from_bytes(b"test-invalid-utf8-\xC3(.rs"))
|
||||||
|
.assert()
|
||||||
|
.success();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn do_not_panic_regression_tests() {
|
fn do_not_panic_regression_tests() {
|
||||||
for filename in &[
|
for filename in &[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user