summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichal Nazarewicz <mina86@mina86.com>2018-09-21 15:56:09 +0100
committerDavid Peter <sharkdp@users.noreply.github.com>2018-09-23 10:19:19 +0200
commit79b960e17e75339d2093a78bc7f8af89a05a2018 (patch)
tree6a1d2ce4fc98cd41a4758ff0b8deadabf3b198a5
parent6aa626f1c4c2819c37ad1103f038aa8ae7c7a047 (diff)
Use ansi_colours package for better true-colour approximation
-rw-r--r--Cargo.lock10
-rw-r--r--Cargo.toml1
-rw-r--r--src/terminal.rs47
3 files changed, 14 insertions, 44 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 989f1c94..481fead0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -7,6 +7,14 @@ dependencies = [
]
[[package]]
+name = "ansi_colours"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -37,6 +45,7 @@ dependencies = [
name = "bat"
version = "0.7.0"
dependencies = [
+ "ansi_colours 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -794,6 +803,7 @@ dependencies = [
[metadata]
"checksum aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "68f56c7353e5a9547cbd76ed90f7bb5ffc3ba09d4ea9bd1d8c06c8b1142eeb5a"
+"checksum ansi_colours 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1d0f302a81afc6a7f4350c04f0ba7cfab529cc009bca3324b3fb5764e6add8b6"
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652"
"checksum base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7c4a342b450b268e1be8036311e2c613d7f8a7ed31214dff1cc3b60852a3168d"
diff --git a/Cargo.toml b/Cargo.toml
index be7ea253..84c53c87 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,6 +16,7 @@ exclude = [
[dependencies]
atty = "0.2.2"
ansi_term = "0.11"
+ansi_colours = "^1.0"
console = "0.6"
directories = "1.0"
lazy_static = "1.0"
diff --git a/src/terminal.rs b/src/terminal.rs
index db472779..7a6850ca 100644
--- a/src/terminal.rs
+++ b/src/terminal.rs
@@ -1,32 +1,15 @@
+extern crate ansi_colours;
+
use ansi_term::Colour::{Fixed, RGB};
use ansi_term::{self, Style};
use syntect::highlighting::{self, FontStyle};
-/// Approximate a 24 bit color value by a 8 bit ANSI code
-fn rgb2ansi(r: u8, g: u8, b: u8) -> u8 {
- const BLACK: u8 = 16;
- const WHITE: u8 = 231;
-
- if r == g && g == b {
- if r < 8 {
- BLACK
- } else if r > 248 {
- WHITE
- } else {
- ((r - 8) as u16 * 24 / 247) as u8 + 232
- }
- } else {
- 36 * (r / 51) + 6 * (g / 51) + (b / 51) + 16
- }
-}
-
pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> ansi_term::Colour {
if true_color {
RGB(color.r, color.g, color.b)
} else {
- let ansi_code = rgb2ansi(color.r, color.g, color.b);
- Fixed(ansi_code)
+ Fixed(ansi_colours::ansi256_from_rgb((color.r, color.g, color.b)))
}
}
@@ -54,27 +37,3 @@ pub fn as_terminal_escaped(
style.paint(text).to_string()
}
-
-#[test]
-fn test_rgb2ansi_black_white() {
- assert_eq!(16, rgb2ansi(0x00, 0x00, 0x00));
- assert_eq!(231, rgb2ansi(0xff, 0xff, 0xff));
-}
-
-#[test]
-fn test_rgb2ansi_gray() {
- assert_eq!(241, rgb2ansi(0x6c, 0x6c, 0x6c));
- assert_eq!(233, rgb2ansi(0x1c, 0x1c, 0x1c));
-}
-
-#[test]
-fn test_rgb2ansi_color() {
- assert_eq!(96, rgb2ansi(0x87, 0x5f, 0x87));
- assert_eq!(141, rgb2ansi(0xaf, 0x87, 0xff));
- assert_eq!(193, rgb2ansi(0xd7, 0xff, 0xaf));
-}
-
-#[test]
-fn test_rgb2ansi_approx() {
- assert_eq!(231, rgb2ansi(0xfe, 0xfe, 0xfe));
-}