summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Otto <th1000s@posteo.net>2021-12-07 22:42:34 +0100
committerDan Davison <dandavison7@gmail.com>2021-12-07 19:18:23 -0500
commit47b073d224757217de468063904d1c8342e290d1 (patch)
treecfa604633e69f06ca672b8f5404a49bfc397ab94
parent7eb56b0997bc0250494b872c134b1947a7886c7f (diff)
Move explain_ansi() part out of parse_ansi()
-rw-r--r--src/ansi/mod.rs19
-rw-r--r--src/subcommands/parse_ansi.rs19
2 files changed, 27 insertions, 11 deletions
diff --git a/src/ansi/mod.rs b/src/ansi/mod.rs
index f45c13e7..fe27bbe6 100644
--- a/src/ansi/mod.rs
+++ b/src/ansi/mod.rs
@@ -155,6 +155,25 @@ fn strip_ansi_codes_from_strings_iterator<'a>(
.join("")
}
+pub fn explain_ansi(line: &str, colorful: bool) -> String {
+ use crate::style::Style;
+
+ parse_style_sections(line)
+ .into_iter()
+ .map(|(ansi_term_style, s)| {
+ let style = Style {
+ ansi_term_style,
+ ..Style::default()
+ };
+ if colorful {
+ format!("({}){}", style.to_painted_string(), style.paint(s))
+ } else {
+ format!("({}){}", style.to_string(), s)
+ }
+ })
+ .collect()
+}
+
#[cfg(test)]
mod tests {
use crate::ansi::ansi_preserving_index;
diff --git a/src/subcommands/parse_ansi.rs b/src/subcommands/parse_ansi.rs
index 51bebe56..eb62383b 100644
--- a/src/subcommands/parse_ansi.rs
+++ b/src/subcommands/parse_ansi.rs
@@ -2,19 +2,16 @@ use std::io::{self, BufRead};
#[cfg(not(tarpaulin_include))]
pub fn parse_ansi() -> std::io::Result<()> {
- use crate::{ansi, style::Style};
+ use crate::ansi;
for line in io::stdin().lock().lines() {
- for (ansi_term_style, s) in ansi::parse_style_sections(
- &line.unwrap_or_else(|line| panic!("Invalid utf-8: {:?}", line)),
- ) {
- let style = Style {
- ansi_term_style,
- ..Style::default()
- };
- print!("({}){}", style.to_painted_string(), style.paint(s));
- }
- println!();
+ println!(
+ "{}",
+ ansi::explain_ansi(
+ &line.unwrap_or_else(|line| panic!("Invalid utf-8: {:?}", line)),
+ true
+ )
+ );
}
Ok(())
}