summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2021-11-21 22:09:51 -0500
committerGitHub <noreply@github.com>2021-11-21 22:09:51 -0500
commit48173248dac85d6d6871554030e4a07e06c9ecdd (patch)
treeee04a945624c64dacb518af91cee976e488c5a19
parent0a0a3e02addfdb33adc4e72d361cef392f47f189 (diff)
Support named colors (#786)
https://docs.rs/palette/0.6.0/palette/named/index.html
-rw-r--r--Cargo.lock154
-rw-r--r--Cargo.toml1
-rw-r--r--src/cli.rs12
-rw-r--r--src/color.rs1
-rw-r--r--src/utils/syntect.rs9
5 files changed, 173 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 0c081ac2..0997c92e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -45,6 +45,15 @@ dependencies = [
]
[[package]]
+name = "approx"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "072df7202e63b127ab55acfe16ce97013d5b97bf160489336d3f1840fd78e99e"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
name = "arrayvec"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -285,6 +294,15 @@ dependencies = [
]
[[package]]
+name = "find-crate"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "59a98bbaacea1c0eb6a0876280051b892eb73594fd90cf3b20e9c817029c57d2"
+dependencies = [
+ "toml",
+]
+
+[[package]]
name = "flate2"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -354,6 +372,7 @@ dependencies = [
"grep-cli",
"itertools",
"lazy_static",
+ "palette",
"pathdiff",
"regex",
"shell-words",
@@ -649,6 +668,30 @@ dependencies = [
]
[[package]]
+name = "palette"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f9735f7e1e51a3f740bacd5dc2724b61a7806f23597a8736e679f38ee3435d18"
+dependencies = [
+ "approx",
+ "num-traits",
+ "palette_derive",
+ "phf",
+]
+
+[[package]]
+name = "palette_derive"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7799c3053ea8a6d8a1193c7ba42f534e7863cf52e378a7f90406f4a645d33bad"
+dependencies = [
+ "find-crate",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
name = "pathdiff"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -661,6 +704,50 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]]
+name = "phf"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b2ac8b67553a7ca9457ce0e526948cad581819238f4a9d1ea74545851fa24f37"
+dependencies = [
+ "phf_macros",
+ "phf_shared",
+ "proc-macro-hack",
+]
+
+[[package]]
+name = "phf_generator"
+version = "0.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d43f3220d96e0080cc9ea234978ccd80d904eafb17be31bb0f76daaea6493082"
+dependencies = [
+ "phf_shared",
+ "rand",
+]
+
+[[package]]
+name = "phf_macros"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b706f5936eb50ed880ae3009395b43ed19db5bff2ebd459c95e7bf013a89ab86"
+dependencies = [
+ "phf_generator",
+ "phf_shared",
+ "proc-macro-hack",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "phf_shared"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a68318426de33640f02be62b4ae8eb1261be2efbc337b60c54d845bf4484e0d9"
+dependencies = [
+ "siphasher",
+]
+
+[[package]]
name = "pkg-config"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -681,6 +768,12 @@ dependencies = [
]
[[package]]
+name = "ppv-lite86"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba"
+
+[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -705,6 +798,12 @@ dependencies = [
]
[[package]]
+name = "proc-macro-hack"
+version = "0.5.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
+
+[[package]]
name = "proc-macro2"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -723,6 +822,46 @@ dependencies = [
]
[[package]]
+name = "rand"
+version = "0.8.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
+dependencies = [
+ "libc",
+ "rand_chacha",
+ "rand_core",
+ "rand_hc",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
+dependencies = [
+ "getrandom 0.2.3",
+]
+
+[[package]]
+name = "rand_hc"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
+dependencies = [
+ "rand_core",
+]
+
+[[package]]
name = "redox_syscall"
version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -836,6 +975,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6fa3938c99da4914afedd13bf3d79bcb6c277d1b2c398d23257a304d9e1b074"
[[package]]
+name = "siphasher"
+version = "0.3.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "533494a8f9b724d33625ab53c6c4800f7cc445895924a8ef649222dcb76e938b"
+
+[[package]]
name = "smol_str"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -974,6 +1119,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
[[package]]
+name = "toml"
+version = "0.5.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
+dependencies = [
+ "serde",
+]
+
+[[package]]
name = "unicode-bidi"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index e7181ddc..9ea4d211 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -29,6 +29,7 @@ dirs-next = "2.0.0"
grep-cli = "0.1.6"
itertools = "0.10.1"
lazy_static = "1.4"
+palette = "0.6.0"
pathdiff = "0.2.1"
regex = "1.4.6"
shell-words = "1.0.0"
diff --git a/src/cli.rs b/src/cli.rs
index 5b0fd6a6..558d2141 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -114,15 +114,19 @@ A complete description of the style string syntax follows:
COLORS
------
-There are three ways to specify a color (this section applies to foreground and background colors
+There are four ways to specify a color (this section applies to foreground and background colors
within a style string):
-1. RGB hex code
+1. CSS color name
+
+ Any of the 140 color names used in CSS: https://www.w3schools.com/colors/colors_groups.asp
+
+2. RGB hex code
An example of using an RGB hex code is:
--file-style=\"#0e7c0e\"
-2. ANSI color name
+3. ANSI color name
There are 8 ANSI color names:
black, red, green, yellow, blue, magenta, cyan, white.
@@ -141,7 +145,7 @@ within a style string):
\"purple\" is accepted as a synonym for \"magenta\". Color names and codes are case-insensitive.
-3. ANSI color number
+4. ANSI color number
An example of using an ANSI color number is:
--file-style=28
diff --git a/src/color.rs b/src/color.rs
index c816e158..6f617baf 100644
--- a/src/color.rs
+++ b/src/color.rs
@@ -22,6 +22,7 @@ pub fn parse_color(s: &str, true_color: bool) -> Option<Color> {
.ok()
.and_then(utils::syntect::syntect_color_from_ansi_number)
.or_else(|| utils::syntect::syntect_color_from_ansi_name(s))
+ .or_else(|| utils::syntect::syntect_color_from_name(s))
.unwrap_or_else(die)
};
utils::bat::terminal::to_ansi_color(syntect_color, true_color)
diff --git a/src/utils/syntect.rs b/src/utils/syntect.rs
index 6f7e4af5..99b358da 100644
--- a/src/utils/syntect.rs
+++ b/src/utils/syntect.rs
@@ -9,6 +9,15 @@ pub fn syntect_color_from_ansi_name(name: &str) -> Option<Color> {
color::ansi_16_color_name_to_number(name).and_then(syntect_color_from_ansi_number)
}
+pub fn syntect_color_from_name(name: &str) -> Option<Color> {
+ palette::named::from_str(name).map(|color| Color {
+ r: color.red,
+ g: color.green,
+ b: color.blue,
+ a: 0xFF,
+ })
+}
+
/// 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 syntect_color_from_ansi_number(n: u8) -> Option<Color> {