diff --git a/src/app.rs b/src/app.rs index 803cc0cd..7f7b6b70 100644 --- a/src/app.rs +++ b/src/app.rs @@ -143,7 +143,7 @@ impl App { true_color: is_truecolor_terminal(), output_components: self.output_components()?, language: self.matches.value_of("language"), - output_wrap: if ! self.interactive_output { + output_wrap: if !self.interactive_output { // We don't have the tty width when piping to another program. // There's no point in wrapping when this is the case. OutputWrap::None diff --git a/src/printer.rs b/src/printer.rs index 91aaa36a..3bc4a388 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -41,7 +41,7 @@ impl<'a> Printer<'a> { }; // Generate the panel (gutter) width. - let decorations = instance.gen_decorations(0); + let decorations = instance.line_decorations(0); instance.panel_width = decorations.len() + decorations.iter().fold(0, |a, x| a + x.size); // Return the instance. @@ -97,7 +97,7 @@ impl<'a> Printer<'a> { let mut cursor_max: usize = self.config.term_width; // Line decoration. - let decorations = self.gen_decorations(line_number); + let decorations = self.line_decorations(line_number); let gutter_width = decorations.len() + decorations.iter().fold(0, |a, x| a + x.size); if gutter_width > 0 { @@ -115,7 +115,7 @@ impl<'a> Printer<'a> { // Grid border. let border = if gutter_width > 0 && self.config.output_components.grid() { - self.gen_border() + self.line_border() } else { PrintSegment { size: 0, @@ -131,11 +131,19 @@ impl<'a> Printer<'a> { let true_color = self.config.true_color; let colored_output = self.config.colored_output; - write!(self.handle, "{}", - regions.iter() - .map(|&(style, text)| as_terminal_escaped(style, text, true_color, colored_output)) - .collect::>() - .join("") + write!( + self.handle, + "{}", + regions + .iter() + .map(|&(style, text)| as_terminal_escaped( + style, + text, + true_color, + colored_output + )) + .collect::>() + .join("") )?; } else { for &(style, text) in regions.iter() { @@ -164,26 +172,24 @@ impl<'a> Printer<'a> { } // It wraps. - if self.config.output_wrap == OutputWrap::Character { - let text = chars.by_ref().take(available).collect::(); - cursor = 0; - remaining -= available; - - write!( - self.handle, - "{}\n{}{}", - as_terminal_escaped( - style, - &*text, - self.config.true_color, - self.config.colored_output, - ), - " ".repeat(gutter_width), - border.text.to_owned() - )?; - - continue; - } + let text = chars.by_ref().take(available).collect::(); + cursor = 0; + remaining -= available; + + write!( + self.handle, + "{}\n{}{}", + as_terminal_escaped( + style, + &*text, + self.config.true_color, + self.config.colored_output, + ), + " ".repeat(gutter_width), + border.text.to_owned() + )?; + + continue; } } @@ -194,25 +200,23 @@ impl<'a> Printer<'a> { Ok(()) } - /// Generates all the line decorations. - fn gen_decorations(&self, line_number: usize) -> Vec { + fn line_decorations(&self, line_number: usize) -> Vec { let mut decorations = Vec::new(); if self.config.output_components.numbers() { - decorations.push(self.gen_deco_line_number(line_number)); + decorations.push(self.line_number(line_number)); } if self.config.output_components.changes() { - decorations.push(self.gen_deco_line_changes(line_number)); + decorations.push(self.line_changes(line_number)); } return decorations; } - /// Generates the decoration for displaying the line number. - fn gen_deco_line_number(&self, line_number: usize) -> PrintSegment { + fn line_number(&self, line_number: usize) -> PrintSegment { let plain: String = format!("{:width$}", line_number, width = LINE_NUMBER_WIDTH); - let color = self.colors.line_number.paint(plain.to_owned()); + let color = self.colors.line_number.paint(plain.clone()); return PrintSegment { text: color.to_string(), @@ -220,8 +224,7 @@ impl<'a> Printer<'a> { }; } - /// Generates the decoration for displaying the git changes. - fn gen_deco_line_changes(&self, line_number: usize) -> PrintSegment { + fn line_changes(&self, line_number: usize) -> PrintSegment { let color = if let Some(ref changes) = self.line_changes { match changes.get(&(line_number as u32)) { Some(&LineChange::Added) => self.colors.git_added.paint("+"), @@ -240,8 +243,7 @@ impl<'a> Printer<'a> { }; } - /// Generates the vertical grid border. - fn gen_border(&self) -> PrintSegment { + fn line_border(&self) -> PrintSegment { return PrintSegment { text: self.colors.grid.paint("│ ").to_string(), size: 2, diff --git a/src/style.rs b/src/style.rs index c788f661..82ba2ea1 100644 --- a/src/style.rs +++ b/src/style.rs @@ -16,7 +16,7 @@ pub enum OutputComponent { #[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)] pub enum OutputWrap { Character, - None + None, } impl OutputComponent { diff --git a/src/terminal.rs b/src/terminal.rs index af12c404..4ebf9478 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -1,5 +1,3 @@ -use std::fmt::Write; - use ansi_term::Colour::{Fixed, RGB}; use ansi_term::Style; use syntect::highlighting::{self, FontStyle}; @@ -32,7 +30,6 @@ pub fn as_terminal_escaped( true_color: bool, colored: bool, ) -> String { - let style = if !colored { Style::default() } else { @@ -54,9 +51,7 @@ pub fn as_terminal_escaped( } }; - let mut s: String = String::new(); - write!(s, "{}", style.paint(text)).unwrap(); - return s; + style.paint(text).to_string() } #[test]