mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-21 20:38:44 +00:00
Handle line with invalid UTF-8
This commit is contained in:
parent
d0782ef954
commit
12cb438aa4
31
src/main.rs
31
src/main.rs
@ -162,30 +162,15 @@ fn print_file(
|
|||||||
|
|
||||||
printer.print_header(filename)?;
|
printer.print_header(filename)?;
|
||||||
|
|
||||||
let mut line_nr = 1;
|
let mut buffer = Vec::new();
|
||||||
let mut line_buffer = String::new();
|
while reader.read_until(b'\n', &mut buffer)? > 0 {
|
||||||
loop {
|
{
|
||||||
line_buffer.clear();
|
let line = String::from_utf8_lossy(&buffer);
|
||||||
let num_bytes = reader.read_line(&mut line_buffer);
|
let regions = highlighter.highlight(line.as_ref());
|
||||||
|
|
||||||
let line = match num_bytes {
|
printer.print_line(®ions)?;
|
||||||
Ok(0) => {
|
}
|
||||||
break;
|
buffer.clear();
|
||||||
}
|
|
||||||
Ok(_) => {
|
|
||||||
if !line_buffer.ends_with('\n') {
|
|
||||||
line_buffer.push('\n');
|
|
||||||
}
|
|
||||||
&line_buffer
|
|
||||||
}
|
|
||||||
Err(_) => "<bat: INVALID UTF-8>\n",
|
|
||||||
};
|
|
||||||
|
|
||||||
let regions = highlighter.highlight(line);
|
|
||||||
|
|
||||||
printer.print_line(line_nr, ®ions)?;
|
|
||||||
|
|
||||||
line_nr += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printer.print_footer()?;
|
printer.print_footer()?;
|
||||||
|
@ -16,6 +16,7 @@ pub struct Printer<'a> {
|
|||||||
config: &'a Config<'a>,
|
config: &'a Config<'a>,
|
||||||
decorations: Vec<Box<Decoration>>,
|
decorations: Vec<Box<Decoration>>,
|
||||||
panel_width: usize,
|
panel_width: usize,
|
||||||
|
line_number: usize,
|
||||||
pub line_changes: Option<LineChanges>,
|
pub line_changes: Option<LineChanges>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,6 +65,7 @@ impl<'a> Printer<'a> {
|
|||||||
colors,
|
colors,
|
||||||
config,
|
config,
|
||||||
decorations,
|
decorations,
|
||||||
|
line_number: 0,
|
||||||
line_changes: None,
|
line_changes: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,11 +110,8 @@ impl<'a> Printer<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_line(
|
pub fn print_line(&mut self, regions: &[(highlighting::Style, &str)]) -> Result<()> {
|
||||||
&mut self,
|
self.line_number += 1;
|
||||||
line_number: usize,
|
|
||||||
regions: &[(highlighting::Style, &str)],
|
|
||||||
) -> Result<()> {
|
|
||||||
let mut cursor: usize = 0;
|
let mut cursor: usize = 0;
|
||||||
let mut cursor_max: usize = self.config.term_width;
|
let mut cursor_max: usize = self.config.term_width;
|
||||||
let mut panel_wrap: Option<String> = None;
|
let mut panel_wrap: Option<String> = None;
|
||||||
@ -122,7 +121,7 @@ impl<'a> Printer<'a> {
|
|||||||
let decorations = self
|
let decorations = self
|
||||||
.decorations
|
.decorations
|
||||||
.iter()
|
.iter()
|
||||||
.map(|ref d| d.generate(line_number, false, self))
|
.map(|ref d| d.generate(self.line_number, false, self))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
for deco in decorations {
|
for deco in decorations {
|
||||||
@ -184,7 +183,7 @@ impl<'a> Printer<'a> {
|
|||||||
"{} ",
|
"{} ",
|
||||||
self.decorations
|
self.decorations
|
||||||
.iter()
|
.iter()
|
||||||
.map(|ref d| d.generate(line_number, true, self).text)
|
.map(|ref d| d.generate(self.line_number, true, self).text)
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
.join(" ")
|
.join(" ")
|
||||||
))
|
))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user