Skip to content

Commit 2508323

Browse files
committed
Extract grid and line-number color from theme
This changes the output color of the grid and the line numbers to use the "gutter" foreground color defined in the Sublime `.tmTheme` files. Sublime Text does the same. Note: we could go one step further and also extract the "GitGutter" colors from the themes. These could be used instead of red/green/yellow to signify Git modifications. The problem is that they are quite a bit harder to extract from the syntect `Theme` object. closes #178
1 parent 297afad commit 2508323

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

Diff for: src/features.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn print_files(assets: &HighlightingAssets, config: &Config) -> Result<bool>
6161

6262
let mut output_type = OutputType::from_mode(config.paging_mode);
6363
let handle = output_type.handle()?;
64-
let mut printer = Printer::new(handle, &config);
64+
let mut printer = Printer::new(handle, &config, &theme);
6565
let mut no_errors: bool = true;
6666

6767
for file in &config.files {

Diff for: src/printer.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ansi_term::Colour::{Fixed, Green, Red, White, Yellow};
1+
use ansi_term::Colour::{Fixed, Green, Red, Yellow};
22
use ansi_term::Style;
33
use app::Config;
44
use console::AnsiCodeIterator;
@@ -9,8 +9,8 @@ use std::boxed::Box;
99
use std::io::Write;
1010
use std::vec::Vec;
1111
use style::OutputWrap;
12-
use syntect::highlighting;
13-
use terminal::as_terminal_escaped;
12+
use syntect::highlighting::{self, Theme};
13+
use terminal::{as_terminal_escaped, to_ansi_color};
1414

1515
pub struct Printer<'a> {
1616
handle: &'a mut Write,
@@ -24,9 +24,9 @@ pub struct Printer<'a> {
2424
}
2525

2626
impl<'a> Printer<'a> {
27-
pub fn new(handle: &'a mut Write, config: &'a Config) -> Self {
27+
pub fn new(handle: &'a mut Write, config: &'a Config, theme: &Theme) -> Self {
2828
let colors = if config.colored_output {
29-
Colors::colored()
29+
Colors::colored(theme, config.true_color)
3030
} else {
3131
Colors::plain()
3232
};
@@ -274,8 +274,7 @@ impl<'a> Printer<'a> {
274274
}
275275
}
276276

277-
const GRID_COLOR: u8 = 238;
278-
const LINE_NUMBER_COLOR: u8 = 244;
277+
const DEFAULT_GUTTER_COLOR: u8 = 238;
279278

280279
#[derive(Default)]
281280
pub struct Colors {
@@ -292,14 +291,20 @@ impl Colors {
292291
Colors::default()
293292
}
294293

295-
fn colored() -> Self {
294+
fn colored(theme: &Theme, true_color: bool) -> Self {
295+
let gutter_color = theme
296+
.settings
297+
.gutter_foreground
298+
.map(|c| to_ansi_color(c, true_color))
299+
.unwrap_or(Fixed(DEFAULT_GUTTER_COLOR));
300+
296301
Colors {
297-
grid: Fixed(GRID_COLOR).normal(),
298-
filename: White.bold(),
302+
grid: gutter_color.normal(),
303+
filename: Style::new().bold(),
299304
git_added: Green.normal(),
300305
git_removed: Red.normal(),
301306
git_modified: Yellow.normal(),
302-
line_number: Fixed(LINE_NUMBER_COLOR).normal(),
307+
line_number: gutter_color.normal(),
303308
}
304309
}
305310
}

Diff for: src/terminal.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ansi_term::Colour::{Fixed, RGB};
2-
use ansi_term::Style;
2+
use ansi_term::{self, Style};
33
use syntect::highlighting::{self, FontStyle};
44

55
/// Approximate a 24 bit color value by a 8 bit ANSI code
@@ -20,6 +20,15 @@ fn rgb2ansi(r: u8, g: u8, b: u8) -> u8 {
2020
}
2121
}
2222

23+
pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> ansi_term::Colour {
24+
if true_color {
25+
RGB(color.r, color.g, color.b)
26+
} else {
27+
let ansi_code = rgb2ansi(color.r, color.g, color.b);
28+
Fixed(ansi_code)
29+
}
30+
}
31+
2332
pub fn as_terminal_escaped(
2433
style: highlighting::Style,
2534
text: &str,
@@ -29,12 +38,7 @@ pub fn as_terminal_escaped(
2938
let style = if !colored {
3039
Style::default()
3140
} else {
32-
let color = if true_color {
33-
RGB(style.foreground.r, style.foreground.g, style.foreground.b)
34-
} else {
35-
let ansi = rgb2ansi(style.foreground.r, style.foreground.g, style.foreground.b);
36-
Fixed(ansi)
37-
};
41+
let color = to_ansi_color(style.foreground, true_color);
3842

3943
if style.font_style.contains(FontStyle::BOLD) {
4044
color.bold()

0 commit comments

Comments
 (0)