mirror of
https://github.com/sharkdp/bat.git
synced 2024-12-22 22:40:47 +01:00
Enable 8 bit color support, closes #11
This commit is contained in:
parent
9435e16a1b
commit
38762c34d9
20
src/main.rs
20
src/main.rs
@ -7,6 +7,8 @@ extern crate syntect;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
|
|
||||||
|
mod terminal;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::io::{self, BufRead, ErrorKind, Result, StdoutLock, Write};
|
use std::io::{self, BufRead, ErrorKind, Result, StdoutLock, Write};
|
||||||
@ -23,7 +25,12 @@ use git2::{DiffOptions, IntoCString, Repository};
|
|||||||
use syntect::easy::HighlightFile;
|
use syntect::easy::HighlightFile;
|
||||||
use syntect::highlighting::{Theme, ThemeSet};
|
use syntect::highlighting::{Theme, ThemeSet};
|
||||||
use syntect::parsing::SyntaxSet;
|
use syntect::parsing::SyntaxSet;
|
||||||
use syntect::util::as_24_bit_terminal_escaped;
|
|
||||||
|
use terminal::as_terminal_escaped;
|
||||||
|
|
||||||
|
struct Options {
|
||||||
|
true_color: bool,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
enum LineChange {
|
enum LineChange {
|
||||||
@ -52,6 +59,7 @@ fn print_horizontal_line(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn print_file<P: AsRef<Path>>(
|
fn print_file<P: AsRef<Path>>(
|
||||||
|
options: &Options,
|
||||||
theme: &Theme,
|
theme: &Theme,
|
||||||
syntax_set: &SyntaxSet,
|
syntax_set: &SyntaxSet,
|
||||||
filename: P,
|
filename: P,
|
||||||
@ -101,7 +109,7 @@ fn print_file<P: AsRef<Path>>(
|
|||||||
Fixed(244).paint(format!("{:4}", line_nr)),
|
Fixed(244).paint(format!("{:4}", line_nr)),
|
||||||
line_change,
|
line_change,
|
||||||
Fixed(GRID_COLOR).paint("│"),
|
Fixed(GRID_COLOR).paint("│"),
|
||||||
as_24_bit_terminal_escaped(®ions, false)
|
as_terminal_escaped(®ions, options.true_color)
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,6 +177,12 @@ fn run(matches: &ArgMatches) -> Result<()> {
|
|||||||
"Could not get home directory",
|
"Could not get home directory",
|
||||||
))?;
|
))?;
|
||||||
|
|
||||||
|
let colorterm = env::var("COLORTERM").unwrap_or("".into());
|
||||||
|
|
||||||
|
let options = Options {
|
||||||
|
true_color: colorterm == "truecolor" || colorterm == "24bit",
|
||||||
|
};
|
||||||
|
|
||||||
let theme_dir = home_dir.join(".config").join("bat").join("themes");
|
let theme_dir = home_dir.join(".config").join("bat").join("themes");
|
||||||
let theme_set = ThemeSet::load_from_folder(theme_dir)
|
let theme_set = ThemeSet::load_from_folder(theme_dir)
|
||||||
.map_err(|_| io::Error::new(ErrorKind::Other, "Could not load themes"))?;
|
.map_err(|_| io::Error::new(ErrorKind::Other, "Could not load themes"))?;
|
||||||
@ -179,7 +193,7 @@ fn run(matches: &ArgMatches) -> Result<()> {
|
|||||||
if let Some(files) = matches.values_of("FILE") {
|
if let Some(files) = matches.values_of("FILE") {
|
||||||
for file in files {
|
for file in files {
|
||||||
let line_changes = get_git_diff(file.to_string());
|
let line_changes = get_git_diff(file.to_string());
|
||||||
print_file(theme, &syntax_set, file, line_changes)?;
|
print_file(&options, theme, &syntax_set, file, line_changes)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
66
src/terminal.rs
Normal file
66
src/terminal.rs
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
use std::fmt::Write;
|
||||||
|
|
||||||
|
use ansi_term::Colour::{Fixed, RGB};
|
||||||
|
use syntect::highlighting;
|
||||||
|
|
||||||
|
/// 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 {
|
||||||
|
let fr = r as f32;
|
||||||
|
(((fr - 8.) / 247.) * 24.) as u8 + 232
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let fr = r as f32;
|
||||||
|
let fg = g as f32;
|
||||||
|
let fb = b as f32;
|
||||||
|
16 + (36 * (fr / 255. * 5.) as u8) + (6 * (fg / 255. * 5.) as u8) + (fb / 255. * 5.) as u8
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_terminal_escaped(v: &[(highlighting::Style, &str)], true_color: bool) -> String {
|
||||||
|
let mut s: String = String::new();
|
||||||
|
for &(ref style, text) in v.iter() {
|
||||||
|
let style = if true_color {
|
||||||
|
RGB(style.foreground.r, style.foreground.g, style.foreground.b)
|
||||||
|
} else {
|
||||||
|
let ansi = rgb2ansi(style.foreground.r, style.foreground.g, style.foreground.b);
|
||||||
|
Fixed(ansi)
|
||||||
|
};
|
||||||
|
|
||||||
|
write!(s, "{}", style.paint(text)).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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));
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user