diff options
author | sharkdp <davidpeter@web.de> | 2020-02-12 23:23:49 +0100 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2020-02-13 08:25:04 +0100 |
commit | 7779d9f6221b3e98c43a43ceb5596ba285fdf4f8 (patch) | |
tree | 96de677cb4b523de268914d226280f0e7ca5b86f /src | |
parent | e98f34b1e8a420aad4eba313bd1604d640183413 (diff) |
Allow for non-unicode filenames, closes #225
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/bat/app.rs | 2 | ||||
-rw-r--r-- | src/bin/bat/main.rs | 3 | ||||
-rw-r--r-- | src/diff.rs | 6 | ||||
-rw-r--r-- | src/inputfile.rs | 7 | ||||
-rw-r--r-- | src/printer.rs | 7 |
5 files changed, 15 insertions, 10 deletions
diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index 23ac7a05..75e66854 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -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| { diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs index 1176c7d4..33bd3b3c 100644 --- a/src/bin/bat/main.rs +++ b/src/bin/bat/main.rs @@ -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) } diff --git a/src/diff.rs b/src/diff.rs index 083e96d7..c6113166 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -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()?; diff --git a/src/inputfile.rs b/src/inputfile.rs index 6fda8aea..2db2bd1a 100644 --- a/src/inputfile.rs +++ b/src/inputfile.rs @@ -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))) diff --git a/src/printer.rs b/src/printer.rs index ff4e59a8..49e6b3b7 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -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 { |