use crossterm::style::Color;
use crate::highlight::TokenKind;
use crate::screen::{Style, Zone};
#[cfg(test)]
mod tests;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Look {
pub fg: Option<Color>,
pub bg: Option<Color>,
pub bold: bool,
pub dim: bool,
pub italic: bool,
pub underlined: bool,
pub struck: bool,
pub reverse: bool,
}
const fn rgb(hex: u32) -> Color {
Color::Rgb {
r: (hex >> 16) as u8,
g: (hex >> 8) as u8,
b: hex as u8,
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Palette {
base: Color,
side: Color,
panel: Color,
popup: Color,
bar: Color,
line: Color,
select: Color,
text: Color,
muted: Color,
faint: Color,
bright: Color,
accent: Color,
on_accent: Color,
red: Color,
orange: Color,
yellow: Color,
green: Color,
cyan: Color,
blue: Color,
purple: Color,
pink: Color,
}
const DARK: Palette = Palette {
base: rgb(0x14161b),
side: rgb(0x191c23),
panel: rgb(0x171a21),
popup: rgb(0x252a36),
bar: rgb(0x1f232d),
line: rgb(0x1d212a),
select: rgb(0x2c3a57),
text: rgb(0xd3d8e3),
muted: rgb(0x8590a6),
faint: rgb(0x565f73),
bright: rgb(0xf2f4f8),
accent: rgb(0x6ea8fe),
on_accent: rgb(0x0f1218),
red: rgb(0xf47c7c),
orange: rgb(0xf5a66b),
yellow: rgb(0xedc66b),
green: rgb(0x86d99b),
cyan: rgb(0x62d3cf),
blue: rgb(0x6ea8fe),
purple: rgb(0xb692f6),
pink: rgb(0xf08fc0),
};
const LIGHT: Palette = Palette {
base: rgb(0xfbfbfd),
side: rgb(0xf1f3f7),
panel: rgb(0xf5f6f9),
popup: rgb(0xe6e9f0),
bar: rgb(0xe9ecf2),
line: rgb(0xf0f2f7),
select: rgb(0xcfe0fb),
text: rgb(0x2b303b),
muted: rgb(0x6b7385),
faint: rgb(0x9aa2b3),
bright: rgb(0x11141a),
accent: rgb(0x2f6fdd),
on_accent: rgb(0xffffff),
red: rgb(0xc93c3c),
orange: rgb(0xb5621b),
yellow: rgb(0x8f6b00),
green: rgb(0x2f8548),
cyan: rgb(0x147f86),
blue: rgb(0x2f6fdd),
purple: rgb(0x7b4fd0),
pink: rgb(0xb83a86),
};
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Theme(Option<Palette>);
impl Theme {
pub const CLASSIC: Self = Self(None);
pub const DARK: Self = Self(Some(DARK));
pub const LIGHT: Self = Self(Some(LIGHT));
pub fn choose(setting: Option<&str>, colorterm: Option<&str>) -> Self {
let true_colour = matches!(colorterm, Some("truecolor" | "24bit"));
match setting.map(|setting| setting.trim().to_ascii_lowercase()).as_deref() {
Some("dark") => Self::DARK,
Some("light") => Self::LIGHT,
Some("classic") => Self::CLASSIC,
_ if true_colour => Self::DARK,
_ => Self::CLASSIC,
}
}
pub fn background(&self, zone: Zone) -> Option<Color> {
let palette = self.0.as_ref()?;
Some(match zone {
Zone::Text => palette.base,
Zone::Side => palette.side,
Zone::Panel => palette.panel,
Zone::Popup => palette.popup,
})
}
pub fn look(&self, style: Style, zone: Zone) -> Look {
match &self.0 {
Some(palette) => painted(palette, style, self.background(zone)),
None => classic(style),
}
}
}
fn classic(style: Style) -> Look {
let plain = Look::default();
match style {
Style::Plain | Style::Field => plain,
Style::Dim | Style::Tab | Style::LineNumber(_) | Style::Border | Style::FieldNote => {
Look { dim: true, ..plain }
}
Style::Bold | Style::Accent => Look { bold: true, ..plain },
Style::Bar
| Style::BarBold
| Style::BarWarn
| Style::BarError
| Style::BarDim
| Style::Title
| Style::Selected
| Style::TabActive
| Style::Input => Look { reverse: true, ..plain },
Style::Error => Look { fg: Some(Color::Red), ..plain },
Style::Warning => Look { fg: Some(Color::Yellow), ..plain },
Style::Colored(color) => Look { fg: Some(color), ..plain },
Style::Menu => Look {
fg: Some(Color::White),
bg: Some(Color::DarkGrey),
..plain
},
Style::Code(decoration) => Look {
fg: decoration.kind.map(|kind| match kind {
TokenKind::Keyword => Color::Magenta,
TokenKind::Function => Color::Blue,
TokenKind::Type => Color::Cyan,
TokenKind::String => Color::Green,
TokenKind::Constant => Color::DarkYellow,
TokenKind::Comment => Color::DarkGrey,
TokenKind::Macro => Color::DarkCyan,
TokenKind::Namespace => Color::DarkGreen,
TokenKind::Added => Color::Green,
TokenKind::Removed => Color::Red,
}),
underlined: decoration.underlined,
dim: decoration.faded,
struck: decoration.struck,
reverse: decoration.marked,
..plain
},
}
}
fn painted(palette: &Palette, style: Style, zone: Option<Color>) -> Look {
let on_zone = |fg: Color| Look {
fg: Some(fg),
bg: zone,
..Look::default()
};
let on = |fg: Color, bg: Color| Look {
fg: Some(fg),
bg: Some(bg),
..Look::default()
};
match style {
Style::Plain => on_zone(palette.text),
Style::Dim => on_zone(palette.muted),
Style::Bold => Look { bold: true, ..on_zone(palette.bright) },
Style::Error => on_zone(palette.red),
Style::Warning => on_zone(palette.yellow),
Style::Colored(color) => on_zone(match color {
Color::Red | Color::DarkRed => palette.red,
Color::Green | Color::DarkGreen => palette.green,
Color::Yellow | Color::DarkYellow => palette.yellow,
Color::Blue | Color::DarkBlue => palette.blue,
Color::Cyan | Color::DarkCyan => palette.cyan,
Color::Magenta | Color::DarkMagenta => palette.purple,
other => other,
}),
Style::Menu => on(palette.text, palette.popup),
Style::Bar => on(palette.text, palette.bar),
Style::BarBold => Look { bold: true, ..on(palette.bright, palette.bar) },
Style::BarWarn => on(palette.yellow, palette.bar),
Style::BarError => on(palette.red, palette.bar),
Style::BarDim => on(palette.muted, palette.bar),
Style::Title => Look { bold: true, ..on(palette.on_accent, palette.accent) },
Style::Selected => on(palette.bright, palette.select),
Style::TabActive => Look { bold: true, ..on(palette.bright, palette.base) },
Style::Tab => on(palette.muted, palette.bar),
Style::Input => Look { bold: true, ..on(palette.bright, palette.popup) },
Style::Border => on_zone(palette.faint),
Style::Accent => Look { bold: true, ..on_zone(palette.accent) },
Style::Field => on(palette.text, palette.popup),
Style::FieldNote => on(palette.muted, palette.popup),
Style::LineNumber(false) => on_zone(palette.faint),
Style::LineNumber(true) => on(palette.bright, palette.line),
Style::Code(decoration) => {
let fg = match decoration.kind {
_ if decoration.faded => palette.faint,
Some(TokenKind::Keyword) => palette.purple,
Some(TokenKind::Function) => palette.blue,
Some(TokenKind::Type) => palette.cyan,
Some(TokenKind::String) => palette.green,
Some(TokenKind::Constant) => palette.orange,
Some(TokenKind::Comment) => palette.faint,
Some(TokenKind::Macro) => palette.pink,
Some(TokenKind::Namespace) => palette.yellow,
Some(TokenKind::Added) => palette.green,
Some(TokenKind::Removed) => palette.red,
None => palette.text,
};
let bg = if decoration.marked {
Some(palette.select)
} else if decoration.current {
Some(palette.line)
} else {
zone
};
Look {
fg: Some(fg),
bg,
italic: decoration.kind == Some(TokenKind::Comment),
underlined: decoration.underlined,
struck: decoration.struck,
..Look::default()
}
}
}
}