Fix `--show-all` for UTF-16 encoding

This commit is contained in:
sharkdp 2018-11-01 20:29:48 +01:00 committed by David Peter
parent 50dc4a79b0
commit e81f9b23e6
3 changed files with 27 additions and 38 deletions

View File

@ -1,5 +1,4 @@
use std::io::{self, Write};
use std::mem::swap;
use app::Config;
use assets::HighlightingAssets;
@ -7,7 +6,6 @@ use errors::*;
use inputfile::{InputFile, InputFileReader};
use line_range::{LineRanges, RangeCheckResult};
use output::OutputType;
use preprocessor::replace_nonprintable;
use printer::{InteractivePrinter, Printer, SimplePrinter};
pub struct Controller<'a> {
@ -66,14 +64,7 @@ impl<'b> Controller<'b> {
input_file: InputFile<'a>,
) -> Result<()> {
printer.print_header(writer, input_file)?;
self.print_file_ranges(
printer,
writer,
reader,
&self.config.line_ranges,
self.config.show_nonprintable,
self.config.tab_width,
)?;
self.print_file_ranges(printer, writer, reader, &self.config.line_ranges)?;
printer.print_footer(writer)?;
Ok(())
@ -85,20 +76,11 @@ impl<'b> Controller<'b> {
writer: &mut Write,
mut reader: InputFileReader,
line_ranges: &LineRanges,
show_nonprintable: bool,
tab_width: usize,
) -> Result<()> {
let mut line_buffer = Vec::new();
let mut line_buffer_processed = Vec::new();
let mut line_number: usize = 1;
while reader.read_line(&mut line_buffer)? {
if show_nonprintable {
replace_nonprintable(&mut line_buffer, &mut line_buffer_processed, tab_width);
swap(&mut line_buffer, &mut line_buffer_processed);
}
match line_ranges.check(line_number) {
RangeCheckResult::OutsideRange => {
// Call the printer in case we need to call the syntax highlighter

View File

@ -33,39 +33,41 @@ pub fn expand_tabs(line: &str, width: usize, cursor: &mut usize) -> String {
buffer
}
pub fn replace_nonprintable(input: &mut Vec<u8>, output: &mut Vec<u8>, tab_width: usize) {
output.clear();
pub fn replace_nonprintable(input: &str, tab_width: usize) -> String {
let mut output = String::new();
let tab_width = if tab_width == 0 { 4 } else { tab_width };
for chr in input {
match *chr {
for chr in input.chars() {
match chr {
// space
b' ' => output.extend_from_slice("".as_bytes()),
' ' => output.push('•'),
// tab
b'\t' => {
'\t' => {
if tab_width == 1 {
output.extend_from_slice("".as_bytes());
output.push('↹');
} else {
output.extend_from_slice("".as_bytes());
output.extend_from_slice("".repeat(tab_width - 2).as_bytes());
output.extend_from_slice("".as_bytes());
output.push('├');
output.push_str(&"".repeat(tab_width - 2));
output.push('┤');
}
}
// line feed
0x0A => output.extend_from_slice("".as_bytes()),
'\x0A' => output.push('␊'),
// carriage return
0x0D => output.extend_from_slice("".as_bytes()),
'\x0D' => output.push('␍'),
// null
0x00 => output.extend_from_slice("".as_bytes()),
'\x00' => output.push('␀'),
// bell
0x07 => output.extend_from_slice("".as_bytes()),
'\x07' => output.push('␇'),
// backspace
0x08 => output.extend_from_slice("".as_bytes()),
'\x08' => output.push('␈'),
// escape
0x1B => output.extend_from_slice("".as_bytes()),
'\x1B' => output.push('␛'),
// anything else
_ => output.push(*chr),
_ => output.push(chr),
}
}
output
}

View File

@ -22,7 +22,7 @@ use diff::get_git_diff;
use diff::LineChanges;
use errors::*;
use inputfile::{InputFile, InputFileReader};
use preprocessor::expand_tabs;
use preprocessor::{expand_tabs, replace_nonprintable};
use style::OutputWrap;
use terminal::{as_terminal_escaped, to_ansi_color};
@ -251,7 +251,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
line_number: usize,
line_buffer: &[u8],
) -> Result<()> {
let line = match self.content_type {
let mut line = match self.content_type {
ContentType::BINARY => {
return Ok(());
}
@ -263,6 +263,11 @@ impl<'a> Printer for InteractivePrinter<'a> {
.unwrap_or("Invalid UTF-16BE".into()),
_ => String::from_utf8_lossy(&line_buffer).to_string(),
};
if self.config.show_nonprintable {
line = replace_nonprintable(&mut line, self.config.tab_width);
}
let regions = {
let highlighter = match self.highlighter {
Some(ref mut highlighter) => highlighter,