1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-09-12 08:12:27 +01:00

Add simple loop-through mode

Use a loop-through mode that simply copies input to output if a
non-interactive terminal is detected.

see #150
This commit is contained in:
sharkdp
2018-08-23 23:13:24 +02:00
parent 246cf79dbd
commit 226d9a573a
4 changed files with 97 additions and 31 deletions

View File

@@ -22,7 +22,40 @@ use terminal::{as_terminal_escaped, to_ansi_color};
pub trait Printer {
fn print_header(&mut self, handle: &mut Write, filename: Option<&str>) -> Result<()>;
fn print_footer(&mut self, handle: &mut Write) -> Result<()>;
fn print_line(&mut self, handle: &mut Write, line_number: usize, line: &str) -> Result<()>;
fn print_line(
&mut self,
handle: &mut Write,
line_number: usize,
line_buffer: &[u8],
) -> Result<()>;
}
pub struct SimplePrinter;
impl SimplePrinter {
pub fn new() -> Self {
SimplePrinter {}
}
}
impl Printer for SimplePrinter {
fn print_header(&mut self, _handle: &mut Write, _filename: Option<&str>) -> Result<()> {
Ok(())
}
fn print_footer(&mut self, _handle: &mut Write) -> Result<()> {
Ok(())
}
fn print_line(
&mut self,
handle: &mut Write,
_line_number: usize,
line_buffer: &[u8],
) -> Result<()> {
handle.write(line_buffer)?;
Ok(())
}
}
pub struct InteractivePrinter<'a> {
@@ -153,7 +186,13 @@ impl<'a> Printer for InteractivePrinter<'a> {
}
}
fn print_line(&mut self, handle: &mut Write, line_number: usize, line: &str) -> Result<()> {
fn print_line(
&mut self,
handle: &mut Write,
line_number: usize,
line_buffer: &[u8],
) -> Result<()> {
let line = String::from_utf8_lossy(&line_buffer);
let regions = self.highlighter.highlight(line.as_ref());
let mut cursor: usize = 0;