Define a BackgroundColor::is_dark helper

html_output
Wilfred Hughes 2022-02-14 15:54:42 +07:00
parent 395f6663e6
commit fe1b949cda
2 changed files with 48 additions and 45 deletions

@ -120,9 +120,10 @@ fn display_line_nums(
let s = format_line_num_padded(line_num, source_dims.lhs_line_nums_width);
if lhs_has_novel && use_color {
// TODO: factor out applying colours to line numbers.
match background {
BackgroundColor::Dark => s.bright_red().to_string(),
BackgroundColor::Light => s.red().to_string(),
if background.is_dark() {
s.bright_red().to_string()
} else {
s.red().to_string()
}
} else {
s
@ -138,9 +139,10 @@ fn display_line_nums(
Some(line_num) => {
let s = format_line_num_padded(line_num, source_dims.rhs_line_nums_width);
if rhs_has_novel && use_color {
match background {
BackgroundColor::Dark => s.bright_green().to_string(),
BackgroundColor::Light => s.green().to_string(),
if background.is_dark() {
s.bright_green().to_string()
} else {
s.green().to_string()
}
} else {
s
@ -470,9 +472,10 @@ pub fn print(
);
if let Some(line_num) = lhs_line_num {
if lhs_lines_with_novel.contains(&line_num) {
s = match background {
BackgroundColor::Dark => s.bright_red().to_string(),
BackgroundColor::Light => s.red().to_string(),
s = if background.is_dark() {
s.bright_red().to_string()
} else {
s.red().to_string()
};
}
}
@ -489,9 +492,10 @@ pub fn print(
);
if let Some(line_num) = rhs_line_num {
if rhs_lines_with_novel.contains(&line_num) {
s = match background {
BackgroundColor::Dark => s.bright_green().to_string(),
BackgroundColor::Light => s.green().to_string(),
s = if background.is_dark() {
s.bright_green().to_string()
} else {
s.green().to_string()
};
}
}

@ -11,13 +11,18 @@ use std::{
collections::HashMap,
};
// TODO: Define an is_dark helper rather than verbose matching.
#[derive(Clone, Copy, Debug)]
pub enum BackgroundColor {
Dark,
Light,
}
impl BackgroundColor {
pub fn is_dark(&self) -> bool {
matches!(self, BackgroundColor::Dark)
}
}
/// Slice `s` from `start` inclusive to `end` exclusive by codepoint. This is safer than
/// slicing by bytes, which panics if the byte isn't on a codepoint
/// boundary.
@ -200,20 +205,17 @@ fn apply(s: &str, styles: &[(SingleLineSpan, Style)]) -> String {
}
pub fn novel_style(style: Style, is_lhs: bool, background: BackgroundColor) -> Style {
match background {
BackgroundColor::Dark => {
if is_lhs {
style.bright_red()
} else {
style.bright_green()
}
if background.is_dark() {
if is_lhs {
style.bright_red()
} else {
style.bright_green()
}
BackgroundColor::Light => {
if is_lhs {
style.red()
} else {
style.green()
}
} else {
if is_lhs {
style.red()
} else {
style.green()
}
}
}
@ -229,24 +231,20 @@ pub fn color_positions(
match pos.kind {
MatchKind::UnchangedToken { highlight, .. } => match highlight {
TokenKind::Atom(atom_kind) => match atom_kind {
AtomKind::String => match background {
BackgroundColor::Dark => {
style = style.bright_magenta();
}
BackgroundColor::Light => {
style = style.magenta();
}
},
AtomKind::String => {
style = if background.is_dark() {
style.bright_magenta()
} else {
style.magenta()
};
}
AtomKind::Comment => {
style = style.italic();
match background {
BackgroundColor::Dark => {
style = style.bright_blue();
}
BackgroundColor::Light => {
style = style.blue();
}
}
style = if background.is_dark() {
style.bright_blue()
} else {
style.blue()
};
}
AtomKind::Keyword | AtomKind::Type => {
style = style.bold();
@ -306,9 +304,10 @@ pub fn header(
background: BackgroundColor,
) -> String {
let file_name_pretty = if use_color {
match background {
BackgroundColor::Dark => file_name.bright_yellow().to_string(),
BackgroundColor::Light => file_name.yellow().to_string(),
if background.is_dark() {
file_name.bright_yellow().to_string()
} else {
file_name.yellow().to_string()
}
.bold()
.to_string()