summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main.rs31
-rw-r--r--src/printer.rs13
2 files changed, 14 insertions, 30 deletions
diff --git a/src/main.rs b/src/main.rs
index 9e569514..0c0cda19 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -162,30 +162,15 @@ fn print_file(
printer.print_header(filename)?;
- let mut line_nr = 1;
- let mut line_buffer = String::new();
- loop {
- line_buffer.clear();
- let num_bytes = reader.read_line(&mut line_buffer);
-
- let line = match num_bytes {
- Ok(0) => {
- break;
- }
- Ok(_) => {
- if !line_buffer.ends_with('\n') {
- line_buffer.push('\n');
- }
- &line_buffer
- }
- Err(_) => "<bat: INVALID UTF-8>\n",
- };
+ let mut buffer = Vec::new();
+ while reader.read_until(b'\n', &mut buffer)? > 0 {
+ {
+ let line = String::from_utf8_lossy(&buffer);
+ let regions = highlighter.highlight(line.as_ref());
- let regions = highlighter.highlight(line);
-
- printer.print_line(line_nr, &regions)?;
-
- line_nr += 1;
+ printer.print_line(&regions)?;
+ }
+ buffer.clear();
}
printer.print_footer()?;
diff --git a/src/printer.rs b/src/printer.rs
index a51cc4dc..57d87685 100644
--- a/src/printer.rs
+++ b/src/printer.rs
@@ -16,6 +16,7 @@ pub struct Printer<'a> {
config: &'a Config<'a>,
decorations: Vec<Box<Decoration>>,
panel_width: usize,
+ line_number: usize,
pub line_changes: Option<LineChanges>,
}
@@ -64,6 +65,7 @@ impl<'a> Printer<'a> {
colors,
config,
decorations,
+ line_number: 0,
line_changes: None,
}
}
@@ -108,11 +110,8 @@ impl<'a> Printer<'a> {
}
}
- pub fn print_line(
- &mut self,
- line_number: usize,
- regions: &[(highlighting::Style, &str)],
- ) -> Result<()> {
+ pub fn print_line(&mut self, regions: &[(highlighting::Style, &str)]) -> Result<()> {
+ self.line_number += 1;
let mut cursor: usize = 0;
let mut cursor_max: usize = self.config.term_width;
let mut panel_wrap: Option<String> = None;
@@ -122,7 +121,7 @@ impl<'a> Printer<'a> {
let decorations = self
.decorations
.iter()
- .map(|ref d| d.generate(line_number, false, self))
+ .map(|ref d| d.generate(self.line_number, false, self))
.collect::<Vec<_>>();
for deco in decorations {
@@ -184,7 +183,7 @@ impl<'a> Printer<'a> {
"{} ",
self.decorations
.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>>()
.join(" ")
))