PR #102 Followed @BrainMaestro's suggestions

Also ran cargo fmt.
pull/102/head
eth-p 6 years ago
parent 9f005d115d
commit 870b3c0daf
No known key found for this signature in database
GPG Key ID: 1F8DF8091CD46FBC

@ -143,7 +143,7 @@ impl App {
true_color: is_truecolor_terminal(), true_color: is_truecolor_terminal(),
output_components: self.output_components()?, output_components: self.output_components()?,
language: self.matches.value_of("language"), 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. // We don't have the tty width when piping to another program.
// There's no point in wrapping when this is the case. // There's no point in wrapping when this is the case.
OutputWrap::None OutputWrap::None

@ -41,7 +41,7 @@ impl<'a> Printer<'a> {
}; };
// Generate the panel (gutter) width. // 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); instance.panel_width = decorations.len() + decorations.iter().fold(0, |a, x| a + x.size);
// Return the instance. // Return the instance.
@ -97,7 +97,7 @@ impl<'a> Printer<'a> {
let mut cursor_max: usize = self.config.term_width; let mut cursor_max: usize = self.config.term_width;
// Line decoration. // 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); let gutter_width = decorations.len() + decorations.iter().fold(0, |a, x| a + x.size);
if gutter_width > 0 { if gutter_width > 0 {
@ -115,7 +115,7 @@ impl<'a> Printer<'a> {
// Grid border. // Grid border.
let border = if gutter_width > 0 && self.config.output_components.grid() { let border = if gutter_width > 0 && self.config.output_components.grid() {
self.gen_border() self.line_border()
} else { } else {
PrintSegment { PrintSegment {
size: 0, size: 0,
@ -131,11 +131,19 @@ impl<'a> Printer<'a> {
let true_color = self.config.true_color; let true_color = self.config.true_color;
let colored_output = self.config.colored_output; let colored_output = self.config.colored_output;
write!(self.handle, "{}", write!(
regions.iter() self.handle,
.map(|&(style, text)| as_terminal_escaped(style, text, true_color, colored_output)) "{}",
.collect::<Vec<_>>() regions
.join("") .iter()
.map(|&(style, text)| as_terminal_escaped(
style,
text,
true_color,
colored_output
))
.collect::<Vec<_>>()
.join("")
)?; )?;
} else { } else {
for &(style, text) in regions.iter() { for &(style, text) in regions.iter() {
@ -164,26 +172,24 @@ impl<'a> Printer<'a> {
} }
// It wraps. // It wraps.
if self.config.output_wrap == OutputWrap::Character { let text = chars.by_ref().take(available).collect::<String>();
let text = chars.by_ref().take(available).collect::<String>(); cursor = 0;
cursor = 0; remaining -= available;
remaining -= available;
write!(
write!( self.handle,
self.handle, "{}\n{}{}",
"{}\n{}{}", as_terminal_escaped(
as_terminal_escaped( style,
style, &*text,
&*text, self.config.true_color,
self.config.true_color, self.config.colored_output,
self.config.colored_output, ),
), " ".repeat(gutter_width),
" ".repeat(gutter_width), border.text.to_owned()
border.text.to_owned() )?;
)?;
continue;
continue;
}
} }
} }
@ -194,25 +200,23 @@ impl<'a> Printer<'a> {
Ok(()) Ok(())
} }
/// Generates all the line decorations. fn line_decorations(&self, line_number: usize) -> Vec<PrintSegment> {
fn gen_decorations(&self, line_number: usize) -> Vec<PrintSegment> {
let mut decorations = Vec::new(); let mut decorations = Vec::new();
if self.config.output_components.numbers() { 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() { if self.config.output_components.changes() {
decorations.push(self.gen_deco_line_changes(line_number)); decorations.push(self.line_changes(line_number));
} }
return decorations; return decorations;
} }
/// Generates the decoration for displaying the line number. fn line_number(&self, line_number: usize) -> PrintSegment {
fn gen_deco_line_number(&self, line_number: usize) -> PrintSegment {
let plain: String = format!("{:width$}", line_number, width = LINE_NUMBER_WIDTH); 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 { return PrintSegment {
text: color.to_string(), text: color.to_string(),
@ -220,8 +224,7 @@ impl<'a> Printer<'a> {
}; };
} }
/// Generates the decoration for displaying the git changes. fn line_changes(&self, line_number: usize) -> PrintSegment {
fn gen_deco_line_changes(&self, line_number: usize) -> PrintSegment {
let color = if let Some(ref changes) = self.line_changes { let color = if let Some(ref changes) = self.line_changes {
match changes.get(&(line_number as u32)) { match changes.get(&(line_number as u32)) {
Some(&LineChange::Added) => self.colors.git_added.paint("+"), Some(&LineChange::Added) => self.colors.git_added.paint("+"),
@ -240,8 +243,7 @@ impl<'a> Printer<'a> {
}; };
} }
/// Generates the vertical grid border. fn line_border(&self) -> PrintSegment {
fn gen_border(&self) -> PrintSegment {
return PrintSegment { return PrintSegment {
text: self.colors.grid.paint("│ ").to_string(), text: self.colors.grid.paint("│ ").to_string(),
size: 2, size: 2,

@ -16,7 +16,7 @@ pub enum OutputComponent {
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)] #[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
pub enum OutputWrap { pub enum OutputWrap {
Character, Character,
None None,
} }
impl OutputComponent { impl OutputComponent {

@ -1,5 +1,3 @@
use std::fmt::Write;
use ansi_term::Colour::{Fixed, RGB}; use ansi_term::Colour::{Fixed, RGB};
use ansi_term::Style; use ansi_term::Style;
use syntect::highlighting::{self, FontStyle}; use syntect::highlighting::{self, FontStyle};
@ -32,7 +30,6 @@ pub fn as_terminal_escaped(
true_color: bool, true_color: bool,
colored: bool, colored: bool,
) -> String { ) -> String {
let style = if !colored { let style = if !colored {
Style::default() Style::default()
} else { } else {
@ -54,9 +51,7 @@ pub fn as_terminal_escaped(
} }
}; };
let mut s: String = String::new(); style.paint(text).to_string()
write!(s, "{}", style.paint(text)).unwrap();
return s;
} }
#[test] #[test]

Loading…
Cancel
Save