mirror of
https://github.com/sharkdp/hexyl.git
synced 2026-10-30 09:05:54 +00:00
Implement builder pattern
This commit is contained in:
1 parent
9bc81820e7
commit
80163764c3
3 files changed
+92
-22
No files matched your search
+10
-11
@@ -1,6 +1,6 @@
|
|||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use hexyl::{BorderStyle, Printer};
|
use hexyl::{BorderStyle, PrinterBuilder};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = vec![
|
let input = vec![
|
||||||
@@ -16,16 +16,15 @@ fn main() {
|
|||||||
let show_position_panel = true;
|
let show_position_panel = true;
|
||||||
let use_squeezing = false;
|
let use_squeezing = false;
|
||||||
let border_style = BorderStyle::Unicode;
|
let border_style = BorderStyle::Unicode;
|
||||||
let columns = 2;
|
let panels = 2;
|
||||||
|
|
||||||
let mut printer = Printer::new(
|
let mut printer = PrinterBuilder::new(&mut handle)
|
||||||
&mut handle,
|
.show_color(show_color)
|
||||||
show_color,
|
.show_char_panel(show_char_panel)
|
||||||
show_char_panel,
|
.show_position_panel(show_position_panel)
|
||||||
show_position_panel,
|
.with_border_style(border_style)
|
||||||
border_style,
|
.with_squeeze(use_squeezing)
|
||||||
use_squeezing,
|
.with_panels(panels)
|
||||||
columns,
|
.build();
|
||||||
);
|
|
||||||
printer.print_all(&input[..]).unwrap();
|
printer.print_all(&input[..]).unwrap();
|
||||||
}
|
}
|
||||||
+9
-10
@@ -18,7 +18,7 @@ use thiserror::Error as ThisError;
|
|||||||
|
|
||||||
use terminal_size::terminal_size;
|
use terminal_size::terminal_size;
|
||||||
|
|
||||||
use hexyl::{BorderStyle, Input, Printer};
|
use hexyl::{BorderStyle, Input, PrinterBuilder};
|
||||||
|
|
||||||
const DEFAULT_BLOCK_SIZE: i64 = 512;
|
const DEFAULT_BLOCK_SIZE: i64 = 512;
|
||||||
|
|
||||||
@@ -335,15 +335,14 @@ fn run() -> Result<(), AnyhowError> {
|
|||||||
let stdout = io::stdout();
|
let stdout = io::stdout();
|
||||||
let mut stdout_lock = stdout.lock();
|
let mut stdout_lock = stdout.lock();
|
||||||
|
|
||||||
let mut printer = Printer::new(
|
let mut printer = PrinterBuilder::new(&mut stdout_lock)
|
||||||
&mut stdout_lock,
|
.show_color(show_color)
|
||||||
show_color,
|
.show_char_panel(show_char_panel)
|
||||||
show_char_panel,
|
.show_position_panel(show_position_panel)
|
||||||
show_position_panel,
|
.with_border_style(border_style)
|
||||||
border_style,
|
.with_squeeze(squeeze)
|
||||||
squeeze,
|
.with_panels(panels)
|
||||||
panels,
|
.build();
|
||||||
);
|
|
||||||
printer.display_offset(skip_offset + display_offset);
|
printer.display_offset(skip_offset + display_offset);
|
||||||
printer.print_all(&mut reader).map_err(|e| anyhow!(e))?;
|
printer.print_all(&mut reader).map_err(|e| anyhow!(e))?;
|
||||||
|
|
||||||
|
|||||||
+73
-1
@@ -78,6 +78,7 @@ struct BorderElements {
|
|||||||
right_corner: char,
|
right_corner: char,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
pub enum BorderStyle {
|
pub enum BorderStyle {
|
||||||
Unicode,
|
Unicode,
|
||||||
Ascii,
|
Ascii,
|
||||||
@@ -138,6 +139,77 @@ impl BorderStyle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct PrinterBuilder<'a, Writer: Write> {
|
||||||
|
writer: &'a mut Writer,
|
||||||
|
show_color: bool,
|
||||||
|
show_char_panel: bool,
|
||||||
|
show_position_panel: bool,
|
||||||
|
border_style: BorderStyle,
|
||||||
|
use_squeeze: bool,
|
||||||
|
panels: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Writer: Write> PrinterBuilder<'a, Writer> {
|
||||||
|
pub fn new(writer: &'a mut Writer) -> Self {
|
||||||
|
PrinterBuilder {
|
||||||
|
writer,
|
||||||
|
show_color: true,
|
||||||
|
show_char_panel: true,
|
||||||
|
show_position_panel: true,
|
||||||
|
border_style: BorderStyle::Unicode,
|
||||||
|
use_squeeze: true,
|
||||||
|
panels: 2,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_writer(mut self, writer: &'a mut Writer) -> PrinterBuilder<'a, Writer> {
|
||||||
|
self.writer = writer;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn show_color(mut self, show_color: bool) -> Self {
|
||||||
|
self.show_color = show_color;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn show_char_panel(mut self, show_char_panel: bool) -> Self {
|
||||||
|
self.show_char_panel = show_char_panel;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn show_position_panel(mut self, show_position_panel: bool) -> Self {
|
||||||
|
self.show_position_panel = show_position_panel;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_border_style(mut self, border_style: BorderStyle) -> Self {
|
||||||
|
self.border_style = border_style;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_squeeze(mut self, use_squeeze: bool) -> Self {
|
||||||
|
self.use_squeeze = use_squeeze;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_panels(mut self, panels: u16) -> Self {
|
||||||
|
self.panels = panels;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build(self) -> Printer<'a, Writer> {
|
||||||
|
Printer::new(
|
||||||
|
self.writer,
|
||||||
|
self.show_color,
|
||||||
|
self.show_char_panel,
|
||||||
|
self.show_position_panel,
|
||||||
|
self.border_style,
|
||||||
|
self.use_squeeze,
|
||||||
|
self.panels,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Printer<'a, Writer: Write> {
|
pub struct Printer<'a, Writer: Write> {
|
||||||
idx: u64,
|
idx: u64,
|
||||||
/// The raw bytes used as input for the current line.
|
/// The raw bytes used as input for the current line.
|
||||||
@@ -159,7 +231,7 @@ pub struct Printer<'a, Writer: Write> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Writer: Write> Printer<'a, Writer> {
|
impl<'a, Writer: Write> Printer<'a, Writer> {
|
||||||
pub fn new(
|
fn new(
|
||||||
writer: &'a mut Writer,
|
writer: &'a mut Writer,
|
||||||
show_color: bool,
|
show_color: bool,
|
||||||
show_char_panel: bool,
|
show_char_panel: bool,
|
||||||
|
|||||||
Reference in new issue
Block a user