on master help sign in
Fix `--show-all` for UTF-16 encoding
commite81f9b23e698d17dbe6afd6cb718b70987d5bf70 authorsharkdp <davidpeter@web.de> dateThu 1 Nov 2018, 20:29 (8 years ago) parent50dc4a7
3 files changed+27−38
src/controller.rs+1−19 src/preprocessor.rs+19−17 src/printer.rs+7−2
src/controller.rs+1−19
@@ -1,5 +1,4 @@
11use std::io::{self, Write};
2−use std::mem::swap;
32
43use app::Config;
54use assets::HighlightingAssets;
@@ -7,7 +6,6 @@ use errors::*;
76use inputfile::{InputFile, InputFileReader};
87use line_range::{LineRanges, RangeCheckResult};
98use output::OutputType;
10−use preprocessor::replace_nonprintable;
119use printer::{InteractivePrinter, Printer, SimplePrinter};
1210
1311pub struct Controller<'a> {
@@ -66,14 +64,7 @@ impl<'b> Controller<'b> {
6664 input_file: InputFile<'a>,
6765 ) -> Result<()> {
6866 printer.print_header(writer, input_file)?;
69− self.print_file_ranges(
70− printer,
71− writer,
72− reader,
73− &self.config.line_ranges,
74− self.config.show_nonprintable,
75− self.config.tab_width,
76− )?;
67+ self.print_file_ranges(printer, writer, reader, &self.config.line_ranges)?;
7768 printer.print_footer(writer)?;
7869
7970 Ok(())
@@ -85,20 +76,11 @@ impl<'b> Controller<'b> {
8576 writer: &mut Write,
8677 mut reader: InputFileReader,
8778 line_ranges: &LineRanges,
88− show_nonprintable: bool,
89− tab_width: usize,
9079 ) -> Result<()> {
9180 let mut line_buffer = Vec::new();
92− let mut line_buffer_processed = Vec::new();
93−
9481 let mut line_number: usize = 1;
9582
9683 while reader.read_line(&mut line_buffer)? {
97− if show_nonprintable {
98− replace_nonprintable(&mut line_buffer, &mut line_buffer_processed, tab_width);
99− swap(&mut line_buffer, &mut line_buffer_processed);
100− }
101−
10284 match line_ranges.check(line_number) {
10385 RangeCheckResult::OutsideRange => {
10486 // Call the printer in case we need to call the syntax highlighter
src/preprocessor.rs+19−17
@@ -33,39 +33,41 @@ pub fn expand_tabs(line: &str, width: usize, cursor: &mut usize) -> String {
3333 buffer
3434}
3535
36−pub fn replace_nonprintable(input: &mut Vec<u8>, output: &mut Vec<u8>, tab_width: usize) {
37− output.clear();
36+pub fn replace_nonprintable(input: &str, tab_width: usize) -> String {
37+ let mut output = String::new();
3838
3939 let tab_width = if tab_width == 0 { 4 } else { tab_width };
4040
41− for chr in input {
42− match *chr {
41+ for chr in input.chars() {
42+ match chr {
4343 // space
44− b' ' => output.extend_from_slice("•".as_bytes()),
44+ ' ' => output.push('•'),
4545 // tab
46− b'\t' => {
46+ '\t' => {
4747 if tab_width == 1 {
48− output.extend_from_slice("↹".as_bytes());
48+ output.push('↹');
4949 } else {
50− output.extend_from_slice("├".as_bytes());
51− output.extend_from_slice("─".repeat(tab_width - 2).as_bytes());
52− output.extend_from_slice("┤".as_bytes());
50+ output.push('├');
51+ output.push_str(&"─".repeat(tab_width - 2));
52+ output.push('┤');
5353 }
5454 }
5555 // line feed
56− 0x0A => output.extend_from_slice("␊".as_bytes()),
56+ '\x0A' => output.push('␊'),
5757 // carriage return
58− 0x0D => output.extend_from_slice("␍".as_bytes()),
58+ '\x0D' => output.push('␍'),
5959 // null
60− 0x00 => output.extend_from_slice("␀".as_bytes()),
60+ '\x00' => output.push('␀'),
6161 // bell
62− 0x07 => output.extend_from_slice("␇".as_bytes()),
62+ '\x07' => output.push('␇'),
6363 // backspace
64− 0x08 => output.extend_from_slice("␈".as_bytes()),
64+ '\x08' => output.push('␈'),
6565 // escape
66− 0x1B => output.extend_from_slice("␛".as_bytes()),
66+ '\x1B' => output.push('␛'),
6767 // anything else
68− _ => output.push(*chr),
68+ _ => output.push(chr),
6969 }
7070 }
71+
72+ output
7173}
src/printer.rs+7−2
@@ -22,7 +22,7 @@ use diff::get_git_diff;
2222use diff::LineChanges;
2323use errors::*;
2424use inputfile::{InputFile, InputFileReader};
25−use preprocessor::expand_tabs;
25+use preprocessor::{expand_tabs, replace_nonprintable};
2626use style::OutputWrap;
2727use terminal::{as_terminal_escaped, to_ansi_color};
2828
@@ -251,7 +251,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
251251 line_number: usize,
252252 line_buffer: &[u8],
253253 ) -> Result<()> {
254− let line = match self.content_type {
254+ let mut line = match self.content_type {
255255 ContentType::BINARY => {
256256 return Ok(());
257257 }
@@ -263,6 +263,11 @@ impl<'a> Printer for InteractivePrinter<'a> {
263263 .unwrap_or("Invalid UTF-16BE".into()),
264264 _ => String::from_utf8_lossy(&line_buffer).to_string(),
265265 };
266+
267+ if self.config.show_nonprintable {
268+ line = replace_nonprintable(&mut line, self.config.tab_width);
269+ }
270+
266271 let regions = {
267272 let highlighter = match self.highlighter {
268273 Some(ref mut highlighter) => highlighter,