@@ -16,18 +16,27 @@ use errors::*;
1616use style::OutputWrap;
1717use terminal::{as_terminal_escaped, to_ansi_color};
1818
19−pub struct Printer<'a> {
19+pub trait Printer {
20+ fn print_header(&mut self, filename: Option<&str>) -> Result<()>;
21+ fn print_footer(&mut self) -> Result<()>;
22+ fn print_line(
23+ &mut self,
24+ line_number: usize,
25+ regions: &[(highlighting::Style, &str)],
26+ ) -> Result<()>;
27+}
28+
29+pub struct InteractivePrinter<'a> {
2030 handle: &'a mut Write,
2131 colors: Colors,
2232 pub config: &'a Config<'a>,
2333 decorations: Vec<Box<Decoration>>,
2434 panel_width: usize,
2535 pub ansi_prefix_sgr: String,
26− pub line_number: usize,
2736 pub line_changes: Option<LineChanges>,
2837}
2938
30−impl<'a> Printer<'a> {
39+impl<'a> InteractivePrinter<'a> {
3140 pub fn new(handle: &'a mut Write, config: &'a Config, theme: &Theme) -> Self {
3241 let colors = if config.colored_output {
3342 Colors::colored(theme, config.true_color)
@@ -66,21 +75,36 @@ impl<'a> Printer<'a> {
6675 }
6776
6877 // Create printer.
69− Printer {
78+ InteractivePrinter {
7079 panel_width,
7180 handle,
7281 colors,
7382 config,
7483 decorations,
7584 ansi_prefix_sgr: String::new(),
76− line_number: 0,
7785 line_changes: None,
7886 }
7987 }
8088
81− pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
82− self.line_number = 0;
89+ fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
90+ if self.panel_width == 0 {
91+ writeln!(
92+ self.handle,
93+ "{}",
94+ self.colors.grid.paint("─".repeat(self.config.term_width))
95+ )?;
96+ } else {
97+ let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
98+ let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
99+ writeln!(self.handle, "{}", self.colors.grid.paint(hline))?;
100+ }
101+
102+ Ok(())
103+ }
104+}
83105
106+impl<'a> Printer for InteractivePrinter<'a> {
107+ fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
84108 if !self.config.output_components.header() {
85109 return Ok(());
86110 }
@@ -114,7 +138,7 @@ impl<'a> Printer<'a> {
114138 Ok(())
115139 }
116140
117− pub fn print_footer(&mut self) -> Result<()> {
141+ fn print_footer(&mut self) -> Result<()> {
118142 if self.config.output_components.grid() {
119143 self.print_horizontal_line('┴')
120144 } else {
@@ -122,8 +146,11 @@ impl<'a> Printer<'a> {
122146 }
123147 }
124148
125− pub fn print_line(&mut self, regions: &[(highlighting::Style, &str)]) -> Result<()> {
126− self.line_number += 1;
149+ fn print_line(
150+ &mut self,
151+ line_number: usize,
152+ regions: &[(highlighting::Style, &str)],
153+ ) -> Result<()> {
127154 let mut cursor: usize = 0;
128155 let mut cursor_max: usize = self.config.term_width;
129156 let mut panel_wrap: Option<String> = None;
@@ -133,7 +160,7 @@ impl<'a> Printer<'a> {
133160 let decorations = self
134161 .decorations
135162 .iter()
136− .map(|ref d| d.generate(self.line_number, false, self))
163+ .map(|ref d| d.generate(line_number, false, self))
137164 .collect::<Vec<_>>();
138165
139166 for deco in decorations {
@@ -218,7 +245,7 @@ impl<'a> Printer<'a> {
218245 self.decorations
219246 .iter()
220247 .map(|ref d| d
221− .generate(self.line_number, true, self)
248+ .generate(line_number, true, self)
222249 .text).collect::<Vec<String>>()
223250 .join(" ")
224251 ))
@@ -260,22 +287,6 @@ impl<'a> Printer<'a> {
260287
261288 Ok(())
262289 }
263−
264− fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
265− if self.panel_width == 0 {
266− writeln!(
267− self.handle,
268− "{}",
269− self.colors.grid.paint("─".repeat(self.config.term_width))
270− )?;
271− } else {
272− let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
273− let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
274− writeln!(self.handle, "{}", self.colors.grid.paint(hline))?;
275− }
276−
277− Ok(())
278− }
279290}
280291
281292const DEFAULT_GUTTER_COLOR: u8 = 238;