From 80163764c3736b1a9389f7af0ad2ae06117e1f00 Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Sat, 17 Sep 2022 09:46:15 -0400 Subject: [PATCH] Implement builder pattern --- examples/simple.rs | 21 +++++++------ src/bin/hexyl.rs | 19 ++++++------ src/lib.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 92 insertions(+), 22 deletions(-) diff --git a/examples/simple.rs b/examples/simple.rs index d4ab8c4..575df7e 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,6 +1,6 @@ use std::io; -use hexyl::{BorderStyle, Printer}; +use hexyl::{BorderStyle, PrinterBuilder}; fn main() { let input = vec![ @@ -16,16 +16,15 @@ fn main() { let show_position_panel = true; let use_squeezing = false; let border_style = BorderStyle::Unicode; - let columns = 2; + let panels = 2; - let mut printer = Printer::new( - &mut handle, - show_color, - show_char_panel, - show_position_panel, - border_style, - use_squeezing, - columns, - ); + let mut printer = PrinterBuilder::new(&mut handle) + .show_color(show_color) + .show_char_panel(show_char_panel) + .show_position_panel(show_position_panel) + .with_border_style(border_style) + .with_squeeze(use_squeezing) + .with_panels(panels) + .build(); printer.print_all(&input[..]).unwrap(); } diff --git a/src/bin/hexyl.rs b/src/bin/hexyl.rs index 98b01fe..75f2c96 100644 --- a/src/bin/hexyl.rs +++ b/src/bin/hexyl.rs @@ -18,7 +18,7 @@ use thiserror::Error as ThisError; use terminal_size::terminal_size; -use hexyl::{BorderStyle, Input, Printer}; +use hexyl::{BorderStyle, Input, PrinterBuilder}; const DEFAULT_BLOCK_SIZE: i64 = 512; @@ -335,15 +335,14 @@ fn run() -> Result<(), AnyhowError> { let stdout = io::stdout(); let mut stdout_lock = stdout.lock(); - let mut printer = Printer::new( - &mut stdout_lock, - show_color, - show_char_panel, - show_position_panel, - border_style, - squeeze, - panels, - ); + let mut printer = PrinterBuilder::new(&mut stdout_lock) + .show_color(show_color) + .show_char_panel(show_char_panel) + .show_position_panel(show_position_panel) + .with_border_style(border_style) + .with_squeeze(squeeze) + .with_panels(panels) + .build(); printer.display_offset(skip_offset + display_offset); printer.print_all(&mut reader).map_err(|e| anyhow!(e))?; diff --git a/src/lib.rs b/src/lib.rs index 044f22d..5128c04 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,6 +78,7 @@ struct BorderElements { right_corner: char, } +#[derive(Clone, Copy)] pub enum BorderStyle { Unicode, 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> { idx: u64, /// 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> { - pub fn new( + fn new( writer: &'a mut Writer, show_color: bool, show_char_panel: bool,