mirror of
https://github.com/sharkdp/bat.git
synced 2024-12-21 22:10:45 +01:00
Fix --show-all
for UTF-16 encoding
This commit is contained in:
parent
50dc4a79b0
commit
e81f9b23e6
@ -1,5 +1,4 @@
|
|||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use std::mem::swap;
|
|
||||||
|
|
||||||
use app::Config;
|
use app::Config;
|
||||||
use assets::HighlightingAssets;
|
use assets::HighlightingAssets;
|
||||||
@ -7,7 +6,6 @@ use errors::*;
|
|||||||
use inputfile::{InputFile, InputFileReader};
|
use inputfile::{InputFile, InputFileReader};
|
||||||
use line_range::{LineRanges, RangeCheckResult};
|
use line_range::{LineRanges, RangeCheckResult};
|
||||||
use output::OutputType;
|
use output::OutputType;
|
||||||
use preprocessor::replace_nonprintable;
|
|
||||||
use printer::{InteractivePrinter, Printer, SimplePrinter};
|
use printer::{InteractivePrinter, Printer, SimplePrinter};
|
||||||
|
|
||||||
pub struct Controller<'a> {
|
pub struct Controller<'a> {
|
||||||
@ -66,14 +64,7 @@ impl<'b> Controller<'b> {
|
|||||||
input_file: InputFile<'a>,
|
input_file: InputFile<'a>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
printer.print_header(writer, input_file)?;
|
printer.print_header(writer, input_file)?;
|
||||||
self.print_file_ranges(
|
self.print_file_ranges(printer, writer, reader, &self.config.line_ranges)?;
|
||||||
printer,
|
|
||||||
writer,
|
|
||||||
reader,
|
|
||||||
&self.config.line_ranges,
|
|
||||||
self.config.show_nonprintable,
|
|
||||||
self.config.tab_width,
|
|
||||||
)?;
|
|
||||||
printer.print_footer(writer)?;
|
printer.print_footer(writer)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -85,20 +76,11 @@ impl<'b> Controller<'b> {
|
|||||||
writer: &mut Write,
|
writer: &mut Write,
|
||||||
mut reader: InputFileReader,
|
mut reader: InputFileReader,
|
||||||
line_ranges: &LineRanges,
|
line_ranges: &LineRanges,
|
||||||
show_nonprintable: bool,
|
|
||||||
tab_width: usize,
|
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut line_buffer = Vec::new();
|
let mut line_buffer = Vec::new();
|
||||||
let mut line_buffer_processed = Vec::new();
|
|
||||||
|
|
||||||
let mut line_number: usize = 1;
|
let mut line_number: usize = 1;
|
||||||
|
|
||||||
while reader.read_line(&mut line_buffer)? {
|
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) {
|
match line_ranges.check(line_number) {
|
||||||
RangeCheckResult::OutsideRange => {
|
RangeCheckResult::OutsideRange => {
|
||||||
// Call the printer in case we need to call the syntax highlighter
|
// Call the printer in case we need to call the syntax highlighter
|
||||||
|
@ -33,39 +33,41 @@ pub fn expand_tabs(line: &str, width: usize, cursor: &mut usize) -> String {
|
|||||||
buffer
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn replace_nonprintable(input: &mut Vec<u8>, output: &mut Vec<u8>, tab_width: usize) {
|
pub fn replace_nonprintable(input: &str, tab_width: usize) -> String {
|
||||||
output.clear();
|
let mut output = String::new();
|
||||||
|
|
||||||
let tab_width = if tab_width == 0 { 4 } else { tab_width };
|
let tab_width = if tab_width == 0 { 4 } else { tab_width };
|
||||||
|
|
||||||
for chr in input {
|
for chr in input.chars() {
|
||||||
match *chr {
|
match chr {
|
||||||
// space
|
// space
|
||||||
b' ' => output.extend_from_slice("•".as_bytes()),
|
' ' => output.push('•'),
|
||||||
// tab
|
// tab
|
||||||
b'\t' => {
|
'\t' => {
|
||||||
if tab_width == 1 {
|
if tab_width == 1 {
|
||||||
output.extend_from_slice("↹".as_bytes());
|
output.push('↹');
|
||||||
} else {
|
} else {
|
||||||
output.extend_from_slice("├".as_bytes());
|
output.push('├');
|
||||||
output.extend_from_slice("─".repeat(tab_width - 2).as_bytes());
|
output.push_str(&"─".repeat(tab_width - 2));
|
||||||
output.extend_from_slice("┤".as_bytes());
|
output.push('┤');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// line feed
|
// line feed
|
||||||
0x0A => output.extend_from_slice("␊".as_bytes()),
|
'\x0A' => output.push('␊'),
|
||||||
// carriage return
|
// carriage return
|
||||||
0x0D => output.extend_from_slice("␍".as_bytes()),
|
'\x0D' => output.push('␍'),
|
||||||
// null
|
// null
|
||||||
0x00 => output.extend_from_slice("␀".as_bytes()),
|
'\x00' => output.push('␀'),
|
||||||
// bell
|
// bell
|
||||||
0x07 => output.extend_from_slice("␇".as_bytes()),
|
'\x07' => output.push('␇'),
|
||||||
// backspace
|
// backspace
|
||||||
0x08 => output.extend_from_slice("␈".as_bytes()),
|
'\x08' => output.push('␈'),
|
||||||
// escape
|
// escape
|
||||||
0x1B => output.extend_from_slice("␛".as_bytes()),
|
'\x1B' => output.push('␛'),
|
||||||
// anything else
|
// anything else
|
||||||
_ => output.push(*chr),
|
_ => output.push(chr),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ use diff::get_git_diff;
|
|||||||
use diff::LineChanges;
|
use diff::LineChanges;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use inputfile::{InputFile, InputFileReader};
|
use inputfile::{InputFile, InputFileReader};
|
||||||
use preprocessor::expand_tabs;
|
use preprocessor::{expand_tabs, replace_nonprintable};
|
||||||
use style::OutputWrap;
|
use style::OutputWrap;
|
||||||
use terminal::{as_terminal_escaped, to_ansi_color};
|
use terminal::{as_terminal_escaped, to_ansi_color};
|
||||||
|
|
||||||
@ -251,7 +251,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
line_number: usize,
|
line_number: usize,
|
||||||
line_buffer: &[u8],
|
line_buffer: &[u8],
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let line = match self.content_type {
|
let mut line = match self.content_type {
|
||||||
ContentType::BINARY => {
|
ContentType::BINARY => {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@ -263,6 +263,11 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
.unwrap_or("Invalid UTF-16BE".into()),
|
.unwrap_or("Invalid UTF-16BE".into()),
|
||||||
_ => String::from_utf8_lossy(&line_buffer).to_string(),
|
_ => 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 regions = {
|
||||||
let highlighter = match self.highlighter {
|
let highlighter = match self.highlighter {
|
||||||
Some(ref mut highlighter) => highlighter,
|
Some(ref mut highlighter) => highlighter,
|
||||||
|
Loading…
Reference in New Issue
Block a user