Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only print the header for empty files #566

Merged
merged 1 commit into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/inputfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ const THEME_PREVIEW_FILE: &[u8] = include_bytes!("../assets/theme_preview.rs");
pub struct InputFileReader<'a> {
inner: Box<dyn BufRead + 'a>,
pub first_line: Vec<u8>,
pub content_type: ContentType,
pub content_type: Option<ContentType>,
}

impl<'a> InputFileReader<'a> {
fn new<R: BufRead + 'a>(mut reader: R) -> InputFileReader<'a> {
let mut first_line = vec![];
reader.read_until(b'\n', &mut first_line).ok();

let content_type = content_inspector::inspect(&first_line[..]);
let content_type = if first_line.is_empty() {
None
} else {
Some(content_inspector::inspect(&first_line[..]))
};

if content_type == ContentType::UTF_16LE {
if content_type == Some(ContentType::UTF_16LE) {
reader.read_until(0x00, &mut first_line).ok();
}

Expand All @@ -35,7 +39,7 @@ impl<'a> InputFileReader<'a> {
if self.first_line.is_empty() {
let res = self.inner.read_until(b'\n', buf).map(|size| size > 0)?;

if self.content_type == ContentType::UTF_16LE {
if self.content_type == Some(ContentType::UTF_16LE) {
self.inner.read_until(0x00, buf).ok();
}

Expand Down
24 changes: 13 additions & 11 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub struct InteractivePrinter<'a> {
decorations: Vec<Box<dyn Decoration>>,
panel_width: usize,
ansi_prefix_sgr: String,
content_type: ContentType,
content_type: Option<ContentType>,
pub line_changes: Option<LineChanges>,
highlighter: Option<HighlightLines<'a>>,
syntax_set: &'a SyntaxSet,
Expand Down Expand Up @@ -134,7 +134,7 @@ impl<'a> InteractivePrinter<'a> {

let mut line_changes = None;

let highlighter = if reader.content_type.is_binary() {
let highlighter = if reader.content_type.map_or(false, |c| c.is_binary()) {
None
} else {
// Get the Git modifications
Expand Down Expand Up @@ -194,7 +194,7 @@ impl<'a> InteractivePrinter<'a> {
impl<'a> Printer for InteractivePrinter<'a> {
fn print_header(&mut self, handle: &mut Write, file: InputFile) -> Result<()> {
if !self.config.output_components.header() {
if ContentType::BINARY == self.content_type {
if Some(ContentType::BINARY) == self.content_type {
let input = match file {
InputFile::Ordinary(filename) => format!("file '{}'", filename),
_ => "STDIN".into(),
Expand Down Expand Up @@ -232,9 +232,10 @@ impl<'a> Printer for InteractivePrinter<'a> {
};

let mode = match self.content_type {
ContentType::BINARY => " <BINARY>",
ContentType::UTF_16LE => " <UTF-16LE>",
ContentType::UTF_16BE => " <UTF-16BE>",
Some(ContentType::BINARY) => " <BINARY>",
Some(ContentType::UTF_16LE) => " <UTF-16LE>",
Some(ContentType::UTF_16BE) => " <UTF-16BE>",
None => " <EMPTY>",
_ => "",
};

Expand All @@ -247,7 +248,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
)?;

if self.config.output_components.grid() {
if self.content_type.is_text() {
if self.content_type.map_or(false, |c| c.is_text()) {
self.print_horizontal_line(handle, '┼')?;
} else {
self.print_horizontal_line(handle, '┴')?;
Expand All @@ -258,7 +259,8 @@ impl<'a> Printer for InteractivePrinter<'a> {
}

fn print_footer(&mut self, handle: &mut Write) -> Result<()> {
if self.config.output_components.grid() && self.content_type.is_text() {
if self.config.output_components.grid() && self.content_type.map_or(false, |c| c.is_text())
{
self.print_horizontal_line(handle, '┴')
} else {
Ok(())
Expand All @@ -273,13 +275,13 @@ impl<'a> Printer for InteractivePrinter<'a> {
line_buffer: &[u8],
) -> Result<()> {
let mut line = match self.content_type {
ContentType::BINARY => {
Some(ContentType::BINARY) | None => {
return Ok(());
}
ContentType::UTF_16LE => UTF_16LE
Some(ContentType::UTF_16LE) => UTF_16LE
.decode(&line_buffer, DecoderTrap::Replace)
.map_err(|_| "Invalid UTF-16LE")?,
ContentType::UTF_16BE => UTF_16BE
Some(ContentType::UTF_16BE) => UTF_16BE
.decode(&line_buffer, DecoderTrap::Replace)
.map_err(|_| "Invalid UTF-16BE")?,
_ => String::from_utf8_lossy(&line_buffer).to_string(),
Expand Down