diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-03-03 10:27:20 -0600 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-03-03 11:04:34 -0600 |
commit | 1837aa8eaad50d716fa6f25b66c103fc97c28d05 (patch) | |
tree | db7c4306972e0ec01ffdd16c2b9c06d23447cef6 | |
parent | 5293dba7560be300530838c117559746d2b4a4e7 (diff) |
Support specifying color by ANSI 256 color palette index
-rw-r--r-- | README.md | 26 | ||||
-rw-r--r-- | src/cli.rs | 24 | ||||
-rw-r--r-- | src/config.rs | 6 | ||||
-rw-r--r-- | src/paint.rs | 6 |
4 files changed, 43 insertions, 19 deletions
@@ -200,7 +200,7 @@ OPTIONS: not set using this option, it will be taken from the BAT_THEME environment variable, if that contains a valid theme name. Use --list-themes and --compare-themes to view available themes. Note that the choice of theme only affects code syntax highlighting. See --commit-color, --file-color, --hunk-color to configure the - colors of other parts of the diff output. [env: BAT_THEME=] + colors of other parts of the diff output. [env: BAT_THEME=base16] --24-bit-color <true_color> Whether to emit 24-bit ("true color") RGB color codes. Options are auto, always, and never. "auto" means that delta will emit 24-bit color codes iff the environment variable COLORTERM has the value "truecolor" or @@ -215,11 +215,11 @@ OPTIONS: Colors ------ -All delta color options work the same way. There are two ways to specify a color: +All delta color options work the same way. There are three ways to specify a color: 1. RGB hex code - An example of passing an RGB hex code is: + An example of using an RGB hex code is: --file-color="#0e7c0e" 2. ANSI color name @@ -230,16 +230,26 @@ All delta color options work the same way. There are two ways to specify a color In addition, all of them have a bright form: bright-black, bright-red, bright-green, bright-yellow, bright-blue, bright-magenta, bright-cyan, bright-white - An example is: + An example of using an ANSI color name is: --file-color="green" Unlike RGB hex codes, ANSI color names are just names: you can choose the exact color that each - name corresponds to in the settings of your terminal application (the application you use to run - command line programs). This means that if you use ANSI color names, and you change the color - theme used by your terminal, then delta's colors will respond automatically, without needing to - change the delta command line. + name corresponds to in the settings of your terminal application (the application you use to + enter commands at a shell prompt). This means that if you use ANSI color names, and you change + the color theme used by your terminal, then delta's colors will respond automatically, without + needing to change the delta command line. "purple" is accepted as a synonym for "magenta". Color names and codes are case-insensitive. + +3. ANSI color number + + An example of using an ANSI color number is: + --file-color=28 + + There are 256 ANSI color numbers: 0-255. The first 16 are the same as the colors described in + the "ANSI color name" section above. See https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit. + Specifying colors like this is useful if your terminal only supports 256 colors (i.e. doesn't + support 24-bit color). ``` <br> @@ -19,11 +19,11 @@ use crate::style; Colors ------ -All delta color options work the same way. There are two ways to specify a color: +All delta color options work the same way. There are three ways to specify a color: 1. RGB hex code - An example of passing an RGB hex code is: + An example of using an RGB hex code is: --file-color=\"#0e7c0e\" 2. ANSI color name @@ -34,16 +34,26 @@ All delta color options work the same way. There are two ways to specify a color In addition, all of them have a bright form: bright-black, bright-red, bright-green, bright-yellow, bright-blue, bright-magenta, bright-cyan, bright-white - An example is: + An example of using an ANSI color name is: --file-color=\"green\" Unlike RGB hex codes, ANSI color names are just names: you can choose the exact color that each - name corresponds to in the settings of your terminal application (the application you use to run - command line programs). This means that if you use ANSI color names, and you change the color - theme used by your terminal, then delta's colors will respond automatically, without needing to - change the delta command line. + name corresponds to in the settings of your terminal application (the application you use to + enter commands at a shell prompt). This means that if you use ANSI color names, and you change + the color theme used by your terminal, then delta's colors will respond automatically, without + needing to change the delta command line. \"purple\" is accepted as a synonym for \"magenta\". Color names and codes are case-insensitive. + +3. ANSI color number + + An example of using an ANSI color number is: + --file-color=28 + + There are 256 ANSI color numbers: 0-255. The first 16 are the same as the colors described in + the \"ANSI color name\" section above. See https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit. + Specifying colors like this is useful if your terminal only supports 256 colors (i.e. doesn\'t + support 24-bit color). " )] pub struct Opt { diff --git a/src/config.rs b/src/config.rs index 5d5b114f..cba60646 100644 --- a/src/config.rs +++ b/src/config.rs @@ -183,7 +183,11 @@ fn color_from_rgb_or_ansi_code(s: &str) -> Color { if s.starts_with("#") { Color::from_str(s).unwrap_or_else(|_| die()) } else { - paint::color_from_ansi_name(s).unwrap_or_else(die) + s.parse::<u8>() + .ok() + .and_then(paint::color_from_ansi_number) + .or_else(|| paint::color_from_ansi_name(s)) + .unwrap_or_else(die) } } diff --git a/src/paint.rs b/src/paint.rs index 6102505d..b83c3862 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -259,13 +259,13 @@ pub fn ansi_color_name_to_number(name: &str) -> Option<u8> { } pub fn color_from_ansi_name(name: &str) -> Option<Color> { - ansi_color_name_to_number(name).and_then(|n| Some(color_from_ansi_number(n))) + ansi_color_name_to_number(name).and_then(color_from_ansi_number) } /// Convert 8-bit ANSI code to #RGBA string with ANSI code in red channel and 0 in alpha channel. // See https://github.com/sharkdp/bat/pull/543 -pub fn color_from_ansi_number(n: u8) -> Color { - Color::from_str(&format!("#{:02x}000000", n)).unwrap() +pub fn color_from_ansi_number(n: u8) -> Option<Color> { + Color::from_str(&format!("#{:02x}000000", n)).ok() } mod superimpose_style_sections { |