Allow for non-unicode filenames, closes #225

This commit is contained in:
sharkdp 2020-02-12 23:23:49 +01:00 committed by David Peter
parent e98f34b1e8
commit 7779d9f622
6 changed files with 16 additions and 11 deletions

View File

@ -9,7 +9,7 @@ use console::Term;
use std::process;
fn main() {
let files = std::env::args().skip(1).collect::<Vec<_>>();
let files = std::env::args_os().skip(1).collect::<Vec<_>>();
if files.is_empty() {
eprintln!("No input files specified");

View File

@ -227,7 +227,7 @@ impl App {
fn files(&self) -> Vec<InputFile> {
self.matches
.values_of("FILE")
.values_of_os("FILE")
.map(|values| {
values
.map(|filename| {

View File

@ -13,6 +13,7 @@ use std::io;
use std::io::Write;
use std::path::Path;
use std::process;
use std::ffi::OsStr;
use ansi_term::Colour::Green;
use ansi_term::Style;
@ -160,7 +161,7 @@ fn run() -> Result<bool> {
Ok(true)
} else {
let mut config = app.config()?;
config.files = vec![InputFile::Ordinary(&"cache")];
config.files = vec![InputFile::Ordinary(OsStr::new("cache"))];
run_controller(&config)
}

View File

@ -1,7 +1,9 @@
use git2::{DiffOptions, IntoCString, Repository};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::ffi::OsStr;
use git2::{DiffOptions, IntoCString, Repository};
#[derive(Copy, Clone, Debug)]
pub enum LineChange {
@ -13,7 +15,7 @@ pub enum LineChange {
pub type LineChanges = HashMap<u32, LineChange>;
pub fn get_git_diff(filename: &str) -> Option<LineChanges> {
pub fn get_git_diff(filename: &OsStr) -> Option<LineChanges> {
let repo = Repository::discover(&filename).ok()?;
let repo_path_absolute = fs::canonicalize(repo.workdir()?).ok()?;

View File

@ -1,5 +1,6 @@
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::ffi::OsStr;
use content_inspector::{self, ContentType};
@ -54,7 +55,7 @@ impl<'a> InputFileReader<'a> {
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum InputFile<'a> {
StdIn,
Ordinary(&'a str),
Ordinary(&'a OsStr),
ThemePreviewFile,
}
@ -63,10 +64,10 @@ impl<'a> InputFile<'a> {
match self {
InputFile::StdIn => Ok(InputFileReader::new(stdin.lock())),
InputFile::Ordinary(filename) => {
let file = File::open(filename).map_err(|e| format!("'{}': {}", filename, e))?;
let file = File::open(filename).map_err(|e| format!("'{}': {}", filename.to_string_lossy(), e))?;
if file.metadata()?.is_dir() {
return Err(format!("'{}' is a directory.", filename).into());
return Err(format!("'{}' is a directory.", filename.to_string_lossy()).into());
}
Ok(InputFileReader::new(BufReader::new(file)))

View File

@ -1,5 +1,6 @@
use std::io::Write;
use std::vec::Vec;
use std::borrow::Cow;
use ansi_term::Colour::{Fixed, Green, Red, Yellow};
use ansi_term::Style;
@ -225,7 +226,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
if !self.config.output_components.header() {
if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable {
let input = match file {
InputFile::Ordinary(filename) => format!("file '{}'", filename),
InputFile::Ordinary(filename) => format!("file '{}'", filename.to_string_lossy()),
_ => "STDIN".into(),
};
@ -261,8 +262,8 @@ impl<'a> Printer for InteractivePrinter<'a> {
}
let (prefix, name) = match file {
InputFile::Ordinary(filename) => ("File: ", filename),
_ => ("", "STDIN"),
InputFile::Ordinary(filename) => ("File: ", filename.to_string_lossy()),
_ => ("", Cow::from("STDIN")),
};
let mode = match self.content_type {