Skip to content

Commit

Permalink
Extract grid and line-number color from theme
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sharkdp committed Aug 19, 2018
1 parent 297afad commit 2508323
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn print_files(assets: &HighlightingAssets, config: &Config) -> Result<bool>

let mut output_type = OutputType::from_mode(config.paging_mode);
let handle = output_type.handle()?;
let mut printer = Printer::new(handle, &config);
let mut printer = Printer::new(handle, &config, &theme);
let mut no_errors: bool = true;

for file in &config.files {
Expand Down
27 changes: 16 additions & 11 deletions src/printer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ansi_term::Colour::{Fixed, Green, Red, White, Yellow};
use ansi_term::Colour::{Fixed, Green, Red, Yellow};
use ansi_term::Style;
use app::Config;
use console::AnsiCodeIterator;
Expand All @@ -9,8 +9,8 @@ use std::boxed::Box;
use std::io::Write;
use std::vec::Vec;
use style::OutputWrap;
use syntect::highlighting;
use terminal::as_terminal_escaped;
use syntect::highlighting::{self, Theme};
use terminal::{as_terminal_escaped, to_ansi_color};

pub struct Printer<'a> {
handle: &'a mut Write,
Expand All @@ -24,9 +24,9 @@ pub struct Printer<'a> {
}

impl<'a> Printer<'a> {
pub fn new(handle: &'a mut Write, config: &'a Config) -> Self {
pub fn new(handle: &'a mut Write, config: &'a Config, theme: &Theme) -> Self {
let colors = if config.colored_output {
Colors::colored()
Colors::colored(theme, config.true_color)
} else {
Colors::plain()
};
Expand Down Expand Up @@ -274,8 +274,7 @@ impl<'a> Printer<'a> {
}
}

const GRID_COLOR: u8 = 238;
const LINE_NUMBER_COLOR: u8 = 244;
const DEFAULT_GUTTER_COLOR: u8 = 238;

#[derive(Default)]
pub struct Colors {
Expand All @@ -292,14 +291,20 @@ impl Colors {
Colors::default()
}

fn colored() -> Self {
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));

Colors {
grid: Fixed(GRID_COLOR).normal(),
filename: White.bold(),
grid: gutter_color.normal(),
filename: Style::new().bold(),
git_added: Green.normal(),
git_removed: Red.normal(),
git_modified: Yellow.normal(),
line_number: Fixed(LINE_NUMBER_COLOR).normal(),
line_number: gutter_color.normal(),
}
}
}
18 changes: 11 additions & 7 deletions src/terminal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ansi_term::Colour::{Fixed, RGB};
use ansi_term::Style;
use ansi_term::{self, Style};
use syntect::highlighting::{self, FontStyle};

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

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)
}
}

pub fn as_terminal_escaped(
style: highlighting::Style,
text: &str,
Expand All @@ -29,12 +38,7 @@ pub fn as_terminal_escaped(
let style = if !colored {
Style::default()
} else {
let color = 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)
};
let color = to_ansi_color(style.foreground, true_color);

if style.font_style.contains(FontStyle::BOLD) {
color.bold()
Expand Down

0 comments on commit 2508323

Please sign in to comment.