mirror of
https://github.com/sharkdp/bat.git
synced 2025-09-01 19:02:22 +01:00
Add ansi theme to replace ansi-light and ansi-dark
This combines ansi-light and ansi-dark into a single theme that works with both light and dark backgrounds. Instead of specifying white/black, the ansi theme uses the terminal's default foreground/background color by setting alpha=01, i.e. #00000001. This is in addition to the alpha=00 encoding where red contains an ANSI color palette number. Now, `--theme ansi-light` and `--theme ansi-dark` will print a deprecation notice and use ansi instead (unless the user has a custom theme named ansi-light or ansi-dark, which would take precedence).
This commit is contained in:
committed by
David Peter
parent
19e7763f35
commit
3099f51ba7
@@ -189,6 +189,15 @@ impl HighlightingAssets {
|
||||
match self.theme_set.themes.get(theme) {
|
||||
Some(theme) => theme,
|
||||
None => {
|
||||
if theme == "ansi-light" || theme == "ansi-dark" {
|
||||
use ansi_term::Colour::Yellow;
|
||||
eprintln!(
|
||||
"{}: Theme '{}' is deprecated, using 'ansi' instead.",
|
||||
Yellow.paint("[bat warning]"),
|
||||
theme
|
||||
);
|
||||
return self.get_theme("ansi");
|
||||
}
|
||||
if theme != "" {
|
||||
use ansi_term::Colour::Yellow;
|
||||
eprintln!(
|
||||
|
@@ -448,7 +448,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
if text.len() != text_trimmed.len() {
|
||||
if let Some(background_color) = background_color {
|
||||
let mut ansi_style = Style::default();
|
||||
ansi_style.background = Some(to_ansi_color(background_color, true_color));
|
||||
ansi_style.background = to_ansi_color(background_color, true_color);
|
||||
let width = if cursor_total <= cursor_max {
|
||||
cursor_max - cursor_total + 1
|
||||
} else {
|
||||
@@ -589,8 +589,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
|
||||
if let Some(background_color) = background_color {
|
||||
let mut ansi_style = Style::default();
|
||||
ansi_style.background =
|
||||
Some(to_ansi_color(background_color, self.config.true_color));
|
||||
ansi_style.background = to_ansi_color(background_color, self.config.true_color);
|
||||
|
||||
write!(
|
||||
handle,
|
||||
@@ -624,20 +623,27 @@ impl Colors {
|
||||
}
|
||||
|
||||
fn colored(theme: &Theme, true_color: bool) -> Self {
|
||||
let gutter_color = theme
|
||||
.settings
|
||||
.gutter_foreground
|
||||
.map(|c| to_ansi_color(c, true_color))
|
||||
.unwrap_or(Fixed(DEFAULT_GUTTER_COLOR));
|
||||
let gutter_style = Style {
|
||||
foreground: match theme.settings.gutter_foreground {
|
||||
// If the theme provides a gutter foreground color, use it.
|
||||
// Note: It might be the special value #00000001, in which case
|
||||
// to_ansi_color returns None and we use an empty Style
|
||||
// (resulting in the terminal's default foreground color).
|
||||
Some(c) => to_ansi_color(c, true_color),
|
||||
// Otherwise, use a specific fallback color.
|
||||
None => Some(Fixed(DEFAULT_GUTTER_COLOR)),
|
||||
},
|
||||
..Style::default()
|
||||
};
|
||||
|
||||
Colors {
|
||||
grid: gutter_color.normal(),
|
||||
rule: gutter_color.normal(),
|
||||
grid: gutter_style,
|
||||
rule: gutter_style,
|
||||
filename: Style::new().bold(),
|
||||
git_added: Green.normal(),
|
||||
git_removed: Red.normal(),
|
||||
git_modified: Yellow.normal(),
|
||||
line_number: gutter_color.normal(),
|
||||
line_number: gutter_style,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -3,13 +3,13 @@ use ansi_term::{self, Style};
|
||||
|
||||
use syntect::highlighting::{self, FontStyle};
|
||||
|
||||
pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> ansi_term::Color {
|
||||
pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> Option<ansi_term::Color> {
|
||||
if color.a == 0 {
|
||||
// Themes can specify one of the user-configurable terminal colors by
|
||||
// encoding them as #RRGGBBAA with AA set to 00 (transparent) and RR set
|
||||
// to the 8-bit color palette number. The built-in themes ansi-light,
|
||||
// ansi-dark, base16, and base16-256 use this.
|
||||
match color.r {
|
||||
// to the 8-bit color palette number. The built-in themes ansi, base16,
|
||||
// and base16-256 use this.
|
||||
Some(match color.r {
|
||||
// For the first 8 colors, use the Color enum to produce ANSI escape
|
||||
// sequences using codes 30-37 (foreground) and 40-47 (background).
|
||||
// For example, red foreground is \x1b[31m. This works on terminals
|
||||
@@ -31,11 +31,18 @@ pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> ansi_term:
|
||||
// 90-97 (foreground) and 100-107 (background), we should use those
|
||||
// for values 0x08 to 0x0f and only use Fixed for 0x10 to 0xff.
|
||||
n => Fixed(n),
|
||||
}
|
||||
})
|
||||
} else if color.a == 1 {
|
||||
// Themes can specify the terminal's default foreground/background color
|
||||
// (i.e. no escape sequence) using the encoding #RRGGBBAA with AA set to
|
||||
// 01. The built-in theme ansi uses this.
|
||||
None
|
||||
} else if true_color {
|
||||
RGB(color.r, color.g, color.b)
|
||||
Some(RGB(color.r, color.g, color.b))
|
||||
} else {
|
||||
Fixed(ansi_colours::ansi256_from_rgb((color.r, color.g, color.b)))
|
||||
Some(Fixed(ansi_colours::ansi256_from_rgb((
|
||||
color.r, color.g, color.b,
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +61,10 @@ pub fn as_terminal_escaped(
|
||||
let mut style = if !colored {
|
||||
Style::default()
|
||||
} else {
|
||||
let mut color = Style::from(to_ansi_color(style.foreground, true_color));
|
||||
let mut color = Style {
|
||||
foreground: to_ansi_color(style.foreground, true_color),
|
||||
..Style::default()
|
||||
};
|
||||
if style.font_style.contains(FontStyle::BOLD) {
|
||||
color = color.bold();
|
||||
}
|
||||
@@ -67,6 +77,6 @@ pub fn as_terminal_escaped(
|
||||
color
|
||||
};
|
||||
|
||||
style.background = background_color.map(|c| to_ansi_color(c, true_color));
|
||||
style.background = background_color.and_then(|c| to_ansi_color(c, true_color));
|
||||
style.paint(text).to_string()
|
||||
}
|
||||
|
Reference in New Issue
Block a user