@@ -18,6 +18,7 @@ pub struct Printer<'a> {
1818 pub config: &'a Config<'a>,
1919 decorations: Vec<Box<Decoration>>,
2020 panel_width: usize,
21+ pub ansi_prefix_sgr: String,
2122 pub line_number: usize,
2223 pub line_changes: Option<LineChanges>,
2324}
@@ -67,6 +68,7 @@ impl<'a> Printer<'a> {
6768 colors,
6869 config,
6970 decorations,
71+ ansi_prefix_sgr: String::new(),
7072 line_number: 0,
7173 line_changes: None,
7274 }
@@ -124,8 +126,7 @@ impl<'a> Printer<'a> {
124126
125127 // Line decorations.
126128 if self.panel_width > 0 {
127− let decorations = self
128− .decorations
129+ let decorations = self.decorations
129130 .iter()
130131 .map(|ref d| d.generate(self.line_number, false, self))
131132 .collect::<Vec<_>>();
@@ -158,12 +159,17 @@ impl<'a> Printer<'a> {
158159 } else {
159160 for &(style, region) in regions.iter() {
160161 let mut ansi_iterator = AnsiCodeIterator::new(region);
161− let mut ansi_prefix = "";
162+ let mut ansi_prefix: String = String::new();
162163 for chunk in ansi_iterator {
163164 match chunk {
164165 // ANSI escape passthrough.
165166 (text, true) => {
166− ansi_prefix = text;
167+ if text.chars().last().unwrap() == 'm' {
168+ self.ansi_prefix_sgr.push_str(text);
169+ ansi_prefix.push_str(text);
170+ } else {
171+ ansi_prefix.push_str(text);
172+ }
167173 }
168174
169175 // Regular text.
@@ -185,7 +191,10 @@ impl<'a> Printer<'a> {
185191 "{}",
186192 as_terminal_escaped(
187193 style,
188− &*format!("{}{}", ansi_prefix, text),
194+ &*format!(
195+ "{}{}{}",
196+ self.ansi_prefix_sgr, ansi_prefix, text
197+ ),
189198 self.config.true_color,
190199 self.config.colored_output,
191200 )
@@ -200,9 +209,11 @@ impl<'a> Printer<'a> {
200209 "{} ",
201210 self.decorations
202211 .iter()
203− .map(|ref d| d
204− .generate(self.line_number, true, self)
205− .text)
212+ .map(|ref d| d.generate(
213+ self.line_number,
214+ true,
215+ self
216+ ).text)
206217 .collect::<Vec<String>>()
207218 .join(" ")
208219 ))
@@ -221,13 +232,19 @@ impl<'a> Printer<'a> {
221232 "{}\n{}",
222233 as_terminal_escaped(
223234 style,
224− &*format!("{}{}", ansi_prefix, text),
235+ &*format!(
236+ "{}{}{}",
237+ self.ansi_prefix_sgr, ansi_prefix, text
238+ ),
225239 self.config.true_color,
226240 self.config.colored_output,
227241 ),
228242 panel_wrap.clone().unwrap()
229243 )?;
230244 }
245+
246+ // Clear the ANSI prefix buffer.
247+ ansi_prefix.clear();
231248 }
232249 }
233250 }