Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow --dark to override dark/light detected from syntax theme #1843

Merged
merged 12 commits into from
Sep 8, 2024
3 changes: 2 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use syntect::highlighting::Theme as SyntaxTheme;
use syntect::parsing::SyntaxSet;

use crate::ansi::{ANSI_SGR_BOLD, ANSI_SGR_RESET, ANSI_SGR_UNDERLINE};
use crate::color::ColorMode;
use crate::config::delta_unreachable;
use crate::env::DeltaEnv;
use crate::git_config::GitConfig;
Expand Down Expand Up @@ -1180,7 +1181,7 @@ pub struct ComputedValues {
pub background_color_extends_to_terminal_width: bool,
pub decorations_width: Width,
pub inspect_raw_lines: InspectRawLines,
pub is_light_mode: bool,
pub color_mode: ColorMode,
pub paging_mode: PagingMode,
pub syntax_set: SyntaxSet,
pub syntax_theme: Option<SyntaxTheme>,
Expand Down
60 changes: 36 additions & 24 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use syntect::highlighting::Color as SyntectColor;
use crate::fatal;
use crate::git_config::GitConfig;
use crate::utils;
use ColorMode::*;

pub fn parse_color(s: &str, true_color: bool, git_config: Option<&GitConfig>) -> Option<Color> {
if s == "normal" {
Expand Down Expand Up @@ -105,39 +106,50 @@ fn ansi_16_color_number_to_name(n: u8) -> Option<&'static str> {
None
}

pub fn get_minus_background_color_default(is_light_mode: bool, is_true_color: bool) -> Color {
match (is_light_mode, is_true_color) {
(true, true) => LIGHT_THEME_MINUS_COLOR,
(true, false) => LIGHT_THEME_MINUS_COLOR_256,
(false, true) => DARK_THEME_MINUS_COLOR,
(false, false) => DARK_THEME_MINUS_COLOR_256,
/// The color mode determines some default color choices
/// such as the diff background color or the palette used for blame.
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
pub enum ColorMode {
#[default]
/// Dark background with light text.
Dark,
/// Light background with dark text.
Light,
}

pub fn get_minus_background_color_default(mode: ColorMode, is_true_color: bool) -> Color {
match (mode, is_true_color) {
(Light, true) => LIGHT_THEME_MINUS_COLOR,
(Light, false) => LIGHT_THEME_MINUS_COLOR_256,
(Dark, true) => DARK_THEME_MINUS_COLOR,
(Dark, false) => DARK_THEME_MINUS_COLOR_256,
}
}

pub fn get_minus_emph_background_color_default(is_light_mode: bool, is_true_color: bool) -> Color {
match (is_light_mode, is_true_color) {
(true, true) => LIGHT_THEME_MINUS_EMPH_COLOR,
(true, false) => LIGHT_THEME_MINUS_EMPH_COLOR_256,
(false, true) => DARK_THEME_MINUS_EMPH_COLOR,
(false, false) => DARK_THEME_MINUS_EMPH_COLOR_256,
pub fn get_minus_emph_background_color_default(mode: ColorMode, is_true_color: bool) -> Color {
match (mode, is_true_color) {
(Light, true) => LIGHT_THEME_MINUS_EMPH_COLOR,
(Light, false) => LIGHT_THEME_MINUS_EMPH_COLOR_256,
(Dark, true) => DARK_THEME_MINUS_EMPH_COLOR,
(Dark, false) => DARK_THEME_MINUS_EMPH_COLOR_256,
}
}

pub fn get_plus_background_color_default(is_light_mode: bool, is_true_color: bool) -> Color {
match (is_light_mode, is_true_color) {
(true, true) => LIGHT_THEME_PLUS_COLOR,
(true, false) => LIGHT_THEME_PLUS_COLOR_256,
(false, true) => DARK_THEME_PLUS_COLOR,
(false, false) => DARK_THEME_PLUS_COLOR_256,
pub fn get_plus_background_color_default(mode: ColorMode, is_true_color: bool) -> Color {
match (mode, is_true_color) {
(Light, true) => LIGHT_THEME_PLUS_COLOR,
(Light, false) => LIGHT_THEME_PLUS_COLOR_256,
(Dark, true) => DARK_THEME_PLUS_COLOR,
(Dark, false) => DARK_THEME_PLUS_COLOR_256,
}
}

pub fn get_plus_emph_background_color_default(is_light_mode: bool, is_true_color: bool) -> Color {
match (is_light_mode, is_true_color) {
(true, true) => LIGHT_THEME_PLUS_EMPH_COLOR,
(true, false) => LIGHT_THEME_PLUS_EMPH_COLOR_256,
(false, true) => DARK_THEME_PLUS_EMPH_COLOR,
(false, false) => DARK_THEME_PLUS_EMPH_COLOR_256,
pub fn get_plus_emph_background_color_default(mode: ColorMode, is_true_color: bool) -> Color {
match (mode, is_true_color) {
(Light, true) => LIGHT_THEME_PLUS_EMPH_COLOR,
(Light, false) => LIGHT_THEME_PLUS_EMPH_COLOR_256,
(Dark, true) => DARK_THEME_PLUS_EMPH_COLOR,
(Dark, false) => DARK_THEME_PLUS_EMPH_COLOR_256,
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use syntect::parsing::SyntaxSet;

use crate::ansi;
use crate::cli;
use crate::color;
use crate::color::{self, ColorMode};
use crate::delta::State;
use crate::fatal;
use crate::features::navigate;
Expand Down Expand Up @@ -214,7 +214,7 @@ impl From<cli::Opt> for Config {
));
});

let blame_palette = make_blame_palette(opt.blame_palette, opt.computed.is_light_mode);
let blame_palette = make_blame_palette(opt.blame_palette, opt.computed.color_mode);

if blame_palette.is_empty() {
fatal("Option 'blame-palette' must not be empty.")
Expand Down Expand Up @@ -437,17 +437,17 @@ impl From<cli::Opt> for Config {
}
}

fn make_blame_palette(blame_palette: Option<String>, is_light_mode: bool) -> Vec<String> {
match (blame_palette, is_light_mode) {
fn make_blame_palette(blame_palette: Option<String>, mode: ColorMode) -> Vec<String> {
match (blame_palette, mode) {
(Some(string), _) => string
.split_whitespace()
.map(|s| s.to_owned())
.collect::<Vec<String>>(),
(None, true) => color::LIGHT_THEME_BLAME_PALETTE
(None, ColorMode::Light) => color::LIGHT_THEME_BLAME_PALETTE
.iter()
.map(|s| s.to_string())
.collect::<Vec<String>>(),
(None, false) => color::DARK_THEME_BLAME_PALETTE
(None, ColorMode::Dark) => color::DARK_THEME_BLAME_PALETTE
.iter()
.map(|s| s.to_string())
.collect::<Vec<String>>(),
Expand Down
20 changes: 11 additions & 9 deletions src/features/line_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::cmp::max;
use lazy_static::lazy_static;
use regex::Regex;

use crate::color::ColorMode::*;
use crate::config;
use crate::delta::State;
use crate::features::hyperlinks;
Expand Down Expand Up @@ -38,26 +39,27 @@ pub fn make_feature() -> Vec<(String, OptionValueFunction)> {
"line-numbers-minus-style",
String,
None,
opt => if opt.computed.is_light_mode {
"red".to_string()
} else {
"88".to_string()
opt => match opt.computed.color_mode {
Light => "red",
Dark => "88",
}
),
(
"line-numbers-zero-style",
String,
None,
opt => if opt.computed.is_light_mode {"#dddddd"} else {"#444444"}
opt => match opt.computed.color_mode {
Light => "#dddddd",
Dark => "#444444",
}
),
(
"line-numbers-plus-style",
String,
None,
opt => if opt.computed.is_light_mode {
"green".to_string()
} else {
"28".to_string()
opt => match opt.computed.color_mode {
Light => "green",
Dark => "28",
}
)
])
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn run_app() -> std::io::Result<i32> {
Some(subcommands::show_themes::show_themes(
opt.dark,
opt.light,
opt.computed.is_light_mode,
opt.computed.color_mode,
))
} else if opt.show_colors {
Some(subcommands::show_colors::show_colors())
Expand Down
2 changes: 1 addition & 1 deletion src/options/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub fn set_options(
// Setting ComputedValues
set_widths_and_isatty(opt);
set_true_color(opt);
theme::set__is_light_mode__syntax_theme__syntax_set(opt, assets);
theme::set__color_mode__syntax_theme__syntax_set(opt, assets);
opt.computed.inspect_raw_lines =
cli::InspectRawLines::from_str(&opt.inspect_raw_lines).unwrap();
opt.computed.paging_mode = parse_paging_mode(&opt.paging_mode);
Expand Down
Loading
Loading