mirror of
https://github.com/sharkdp/hexyl.git
synced 2026-10-30 09:05:54 +00:00
printer: accept any Write as output
This commit is contained in:
1 parent
ca8970b5cc
commit
4c48eedb18
2 files changed
+15
-13
No files matched your search
+3
-1
@@ -118,7 +118,9 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.expect("Error setting Ctrl-C handler");
|
.expect("Error setting Ctrl-C handler");
|
||||||
|
|
||||||
let stdout = io::stdout();
|
let stdout = io::stdout();
|
||||||
let mut printer = Printer::new(stdout.lock(), show_color, border_style, squeeze);
|
let mut stdout_lock = stdout.lock();
|
||||||
|
|
||||||
|
let mut printer = Printer::new(&mut stdout_lock, show_color, border_style, squeeze);
|
||||||
|
|
||||||
let mut buffer = [0; BUFFER_SIZE];
|
let mut buffer = [0; BUFFER_SIZE];
|
||||||
'mainloop: loop {
|
'mainloop: loop {
|
||||||
|
|||||||
+12
-12
@@ -1,6 +1,6 @@
|
|||||||
pub mod squeezer;
|
pub mod squeezer;
|
||||||
|
|
||||||
use std::io::{self, StdoutLock, Write};
|
use std::io::{self, Write};
|
||||||
|
|
||||||
use ansi_term::Color;
|
use ansi_term::Color;
|
||||||
use ansi_term::Color::Fixed;
|
use ansi_term::Color::Fixed;
|
||||||
@@ -136,13 +136,13 @@ impl BorderStyle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Printer<'a> {
|
pub struct Printer<'a, Writer: Write> {
|
||||||
idx: usize,
|
idx: usize,
|
||||||
/// The raw bytes used as input for the current line.
|
/// The raw bytes used as input for the current line.
|
||||||
raw_line: Vec<u8>,
|
raw_line: Vec<u8>,
|
||||||
/// The buffered line built with each byte, ready to print to stdout.
|
/// The buffered line built with each byte, ready to print to writer.
|
||||||
buffer_line: Vec<u8>,
|
buffer_line: Vec<u8>,
|
||||||
stdout: StdoutLock<'a>,
|
writer: &'a mut Writer,
|
||||||
show_color: bool,
|
show_color: bool,
|
||||||
border_style: BorderStyle,
|
border_style: BorderStyle,
|
||||||
header_was_printed: bool,
|
header_was_printed: bool,
|
||||||
@@ -151,18 +151,18 @@ pub struct Printer<'a> {
|
|||||||
squeezer: Squeezer,
|
squeezer: Squeezer,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Printer<'a> {
|
impl<'a, Writer: Write> Printer<'a, Writer> {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
stdout: StdoutLock<'_>,
|
writer: &'a mut Writer,
|
||||||
show_color: bool,
|
show_color: bool,
|
||||||
border_style: BorderStyle,
|
border_style: BorderStyle,
|
||||||
use_squeeze: bool,
|
use_squeeze: bool,
|
||||||
) -> Printer<'_> {
|
) -> Printer<'a, Writer> {
|
||||||
Printer {
|
Printer {
|
||||||
idx: 1,
|
idx: 1,
|
||||||
raw_line: vec![],
|
raw_line: vec![],
|
||||||
buffer_line: vec![],
|
buffer_line: vec![],
|
||||||
stdout,
|
writer,
|
||||||
show_color,
|
show_color,
|
||||||
border_style,
|
border_style,
|
||||||
header_was_printed: false,
|
header_was_printed: false,
|
||||||
@@ -197,7 +197,7 @@ impl<'a> Printer<'a> {
|
|||||||
let h25 = h.to_string().repeat(25);
|
let h25 = h.to_string().repeat(25);
|
||||||
|
|
||||||
writeln!(
|
writeln!(
|
||||||
self.stdout,
|
self.writer,
|
||||||
"{l}{h8}{c}{h25}{c}{h25}{c}{h8}{c}{h8}{r}",
|
"{l}{h8}{c}{h25}{c}{h25}{c}{h8}{c}{h8}{r}",
|
||||||
l = border_elements.left_corner,
|
l = border_elements.left_corner,
|
||||||
c = border_elements.column_separator,
|
c = border_elements.column_separator,
|
||||||
@@ -216,7 +216,7 @@ impl<'a> Printer<'a> {
|
|||||||
let h25 = h.to_string().repeat(25);
|
let h25 = h.to_string().repeat(25);
|
||||||
|
|
||||||
writeln!(
|
writeln!(
|
||||||
self.stdout,
|
self.writer,
|
||||||
"{l}{h8}{c}{h25}{c}{h25}{c}{h8}{c}{h8}{r}",
|
"{l}{h8}{c}{h25}{c}{h25}{c}{h8}{c}{h8}{r}",
|
||||||
l = border_elements.left_corner,
|
l = border_elements.left_corner,
|
||||||
c = border_elements.column_separator,
|
c = border_elements.column_separator,
|
||||||
@@ -291,7 +291,7 @@ impl<'a> Printer<'a> {
|
|||||||
self.border_style.inner_sep(),
|
self.border_style.inner_sep(),
|
||||||
self.border_style.outer_sep(),
|
self.border_style.outer_sep(),
|
||||||
);
|
);
|
||||||
self.stdout.write_all(&self.buffer_line)?;
|
self.writer.write_all(&self.buffer_line)?;
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@@ -376,7 +376,7 @@ impl<'a> Printer<'a> {
|
|||||||
SqueezeAction::Ignore => (),
|
SqueezeAction::Ignore => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
self.stdout.write_all(&self.buffer_line)?;
|
self.writer.write_all(&self.buffer_line)?;
|
||||||
|
|
||||||
self.raw_line.clear();
|
self.raw_line.clear();
|
||||||
self.buffer_line.clear();
|
self.buffer_line.clear();
|
||||||
|
|||||||
Reference in new issue
Block a user