summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2021-11-23 13:54:48 -0500
committerDan Davison <dandavison7@gmail.com>2021-11-23 19:30:36 -0500
commit114ae670223520657208501a3245a3b4261c1093 (patch)
tree53a1b969707adcac45956502f88be4426fcdefb1 /src
parent5dc0d6ef7e37a565b06d794b50fcc763079f9ed7 (diff)
Add --parse-ansi command
Diffstat (limited to 'src')
-rw-r--r--src/cli.rs6
-rw-r--r--src/main.rs2
-rw-r--r--src/options/set.rs1
-rw-r--r--src/subcommands/mod.rs1
-rw-r--r--src/subcommands/parse_ansi.rs20
5 files changed, 30 insertions, 0 deletions
diff --git a/src/cli.rs b/src/cli.rs
index ec9b44cf..ee0f8272 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -299,6 +299,12 @@ pub struct Opt {
#[structopt(long = "show-colors")]
pub show_colors: bool,
+ /// Parse ANSI color escape sequences in input and display them as git style
+ /// strings. Example usage: git show --color=always | delta --parse-ansi
+ /// This can be used to help identify input style strings to use with map-styles.
+ #[structopt(long = "parse-ansi")]
+ pub parse_ansi: bool,
+
#[structopt(long = "no-gitconfig")]
/// Do not take any settings from git config. See GIT CONFIG section.
pub no_gitconfig: bool,
diff --git a/src/main.rs b/src/main.rs
index 19d0d26e..5e6eab5e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -96,6 +96,8 @@ fn run_app() -> std::io::Result<i32> {
))
} else if opt.show_colors {
Some(subcommands::show_colors::show_colors())
+ } else if opt.parse_ansi {
+ Some(subcommands::parse_ansi::parse_ansi())
} else {
None
};
diff --git a/src/options/set.rs b/src/options/set.rs
index 4cc2395e..0c640fd5 100644
--- a/src/options/set.rs
+++ b/src/options/set.rs
@@ -180,6 +180,7 @@ pub fn set_options(
line_numbers_zero_style,
pager,
paging_mode,
+ parse_ansi,
// Hack: plus-style must come before plus-*emph-style because the latter default
// dynamically to the value of the former.
plus_style,
diff --git a/src/subcommands/mod.rs b/src/subcommands/mod.rs
index 7669375e..26f04233 100644
--- a/src/subcommands/mod.rs
+++ b/src/subcommands/mod.rs
@@ -1,5 +1,6 @@
pub mod diff;
pub mod list_syntax_themes;
+pub mod parse_ansi;
mod sample_diff;
pub mod show_colors;
pub mod show_config;
diff --git a/src/subcommands/parse_ansi.rs b/src/subcommands/parse_ansi.rs
new file mode 100644
index 00000000..51bebe56
--- /dev/null
+++ b/src/subcommands/parse_ansi.rs
@@ -0,0 +1,20 @@
+use std::io::{self, BufRead};
+
+#[cfg(not(tarpaulin_include))]
+pub fn parse_ansi() -> std::io::Result<()> {
+ use crate::{ansi, style::Style};
+
+ 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!();
+ }
+ Ok(())
+}