on master help sign in
Handle line with invalid UTF-8
commit12cb438aa4bcfcfea3292e31ca2bdf447839cc95 authorEzinwa Okpoechi <brainmaestro@outlook.com> dateFri 18 May 2018, 11:56 (8 years ago) parentd0782ef
2 files changed+14−30
src/main.rs+8−23 src/printer.rs+6−7
src/main.rs+8−23
@@ -162,30 +162,15 @@ fn print_file(
162162
163163 printer.print_header(filename)?;
164164
165− let mut line_nr = 1;
166− let mut line_buffer = String::new();
167− loop {
168− line_buffer.clear();
169− let num_bytes = reader.read_line(&mut line_buffer);
170−
171− let line = match num_bytes {
172− Ok(0) => {
173− break;
174− }
175− Ok(_) => {
176− if !line_buffer.ends_with('\n') {
177− line_buffer.push('\n');
178− }
179− &line_buffer
180− }
181− Err(_) => "<bat: INVALID UTF-8>\n",
182− };
165+ let mut buffer = Vec::new();
166+ while reader.read_until(b'\n', &mut buffer)? > 0 {
167+ {
168+ let line = String::from_utf8_lossy(&buffer);
169+ let regions = highlighter.highlight(line.as_ref());
183170
184− let regions = highlighter.highlight(line);
185−
186− printer.print_line(line_nr, &regions)?;
187−
188− line_nr += 1;
171+ printer.print_line(&regions)?;
172+ }
173+ buffer.clear();
189174 }
190175
191176 printer.print_footer()?;
src/printer.rs+6−7
@@ -16,6 +16,7 @@ pub struct Printer<'a> {
1616 config: &'a Config<'a>,
1717 decorations: Vec<Box<Decoration>>,
1818 panel_width: usize,
19+ line_number: usize,
1920 pub line_changes: Option<LineChanges>,
2021}
2122
@@ -64,6 +65,7 @@ impl<'a> Printer<'a> {
6465 colors,
6566 config,
6667 decorations,
68+ line_number: 0,
6769 line_changes: None,
6870 }
6971 }
@@ -108,11 +110,8 @@ impl<'a> Printer<'a> {
108110 }
109111 }
110112
111− pub fn print_line(
112− &mut self,
113− line_number: usize,
114− regions: &[(highlighting::Style, &str)],
115− ) -> Result<()> {
113+ pub fn print_line(&mut self, regions: &[(highlighting::Style, &str)]) -> Result<()> {
114+ self.line_number += 1;
116115 let mut cursor: usize = 0;
117116 let mut cursor_max: usize = self.config.term_width;
118117 let mut panel_wrap: Option<String> = None;
@@ -122,7 +121,7 @@ impl<'a> Printer<'a> {
122121 let decorations = self
123122 .decorations
124123 .iter()
125− .map(|ref d| d.generate(line_number, false, self))
124+ .map(|ref d| d.generate(self.line_number, false, self))
126125 .collect::<Vec<_>>();
127126
128127 for deco in decorations {
@@ -184,7 +183,7 @@ impl<'a> Printer<'a> {
184183 "{} ",
185184 self.decorations
186185 .iter()
187− .map(|ref d| d.generate(line_number, true, self).text)
186+ .map(|ref d| d.generate(self.line_number, true, self).text)
188187 .collect::<Vec<String>>()
189188 .join(" ")
190189 ))