This commit is contained in:
eth-p 2018-05-16 14:19:36 -07:00 committed by David Peter
parent 247dfbee83
commit 486e6a19cd

View File

@ -1,4 +1,6 @@
use Colors;
use app::Config; use app::Config;
use console::AnsiCodeIterator;
use decorations::{Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration}; use decorations::{Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration};
use diff::LineChanges; use diff::LineChanges;
use errors::*; use errors::*;
@ -8,7 +10,6 @@ use std::vec::Vec;
use style::OutputWrap; use style::OutputWrap;
use syntect::highlighting; use syntect::highlighting;
use terminal::as_terminal_escaped; use terminal::as_terminal_escaped;
use Colors;
pub struct Printer<'a> { pub struct Printer<'a> {
handle: &'a mut Write, handle: &'a mut Write,
@ -118,8 +119,7 @@ impl<'a> Printer<'a> {
// Line decorations. // Line decorations.
if self.panel_width > 0 { if self.panel_width > 0 {
let decorations = self let decorations = self.decorations
.decorations
.iter() .iter()
.map(|ref d| d.generate(self.line_number, false, self)) .map(|ref d| d.generate(self.line_number, false, self))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -150,64 +150,79 @@ impl<'a> Printer<'a> {
.join("") .join("")
)?; )?;
} else { } else {
for &(style, text) in regions.iter() { for &(style, region) in regions.iter() {
let text = text.trim_right_matches(|c| c == '\r' || c == '\n'); let mut ansi_iterator = AnsiCodeIterator::new(region);
let mut chars = text.chars(); let mut ansi_prefix = "";
let mut remaining = text.chars().count(); while let Some(chunk) = ansi_iterator.next() {
match chunk {
// ANSI escape passthrough.
(text, true) => {
ansi_prefix = text;
}
while remaining > 0 { // Regular text.
let available = cursor_max - cursor; (text, false) => {
let text = text.trim_right_matches(|c| c == '\r' || c == '\n');
let mut chars = text.chars();
let mut remaining = text.chars().count();
// It fits. while remaining > 0 {
if remaining <= available { let available = cursor_max - cursor;
let text = chars.by_ref().take(remaining).collect::<String>();
cursor += remaining;
write!( // It fits.
self.handle, if remaining <= available {
"{}", let text = chars.by_ref().take(remaining).collect::<String>();
as_terminal_escaped( cursor += remaining;
style,
&*text,
self.config.true_color,
self.config.colored_output,
)
)?;
break;
}
// Generate wrap padding if not already generated. write!(
if panel_wrap.is_none() { self.handle,
panel_wrap = if self.panel_width > 0 { "{}",
Some(format!( as_terminal_escaped(
"{} ", style,
self.decorations &*format!("{}{}", ansi_prefix, text),
.iter() self.config.true_color,
.map(|ref d| d.generate(self.line_number, true, self).text) self.config.colored_output,
.collect::<Vec<String>>() )
.join(" ") )?;
)) break;
} else { }
Some("".to_string())
// Generate wrap padding if not already generated.
if panel_wrap.is_none() {
panel_wrap = if self.panel_width > 0 {
Some(format!(
"{} ",
self.decorations
.iter()
.map(|ref d| d.generate(self.line_number, true, self)
.text)
.collect::<Vec<String>>()
.join(" ")
))
} else {
Some("".to_string())
}
}
// It wraps.
let text = chars.by_ref().take(available).collect::<String>();
cursor = 0;
remaining -= available;
write!(
self.handle,
"{}\n{}",
as_terminal_escaped(
style,
&*format!("{}{}", ansi_prefix, text),
self.config.true_color,
self.config.colored_output,
),
panel_wrap.clone().unwrap()
)?;
}
} }
} }
// It wraps.
let text = chars.by_ref().take(available).collect::<String>();
cursor = 0;
remaining -= available;
write!(
self.handle,
"{}\n{}",
as_terminal_escaped(
style,
&*text,
self.config.true_color,
self.config.colored_output,
),
panel_wrap.clone().unwrap()
)?;
} }
} }