use atty::Stream;
use owo_colors::Style;
#[derive(Debug, Clone)]
pub struct GraphicalTheme {
pub characters: ThemeCharacters,
pub styles: ThemeStyles,
}
impl GraphicalTheme {
pub fn ascii() -> Self {
Self {
characters: ThemeCharacters::ascii(),
styles: ThemeStyles::ansi(),
}
}
pub fn unicode() -> Self {
Self {
characters: ThemeCharacters::unicode(),
styles: ThemeStyles::rgb(),
}
}
pub fn unicode_nocolor() -> Self {
Self {
characters: ThemeCharacters::unicode(),
styles: ThemeStyles::none(),
}
}
pub fn none() -> Self {
Self {
characters: ThemeCharacters::ascii(),
styles: ThemeStyles::none(),
}
}
}
impl Default for GraphicalTheme {
fn default() -> Self {
match std::env::var("NO_COLOR") {
_ if !atty::is(Stream::Stdout) || !atty::is(Stream::Stderr) => Self::ascii(),
Ok(string) if string != "0" => Self::unicode_nocolor(),
_ => Self::unicode(),
}
}
}
#[derive(Debug, Clone)]
pub struct ThemeStyles {
pub error: Style,
pub warning: Style,
pub advice: Style,
pub code: Style,
pub help: Style,
pub filename: Style,
pub highlights: Vec<Style>,
}
fn style() -> Style {
Style::new()
}
impl ThemeStyles {
pub fn rgb() -> Self {
Self {
error: style().fg_rgb::<172, 65, 66>(),
warning: style().fg_rgb::<244, 191, 117>(),
advice: style().fg_rgb::<106, 159, 181>(),
code: style().fg_rgb::<170, 117, 159>(),
help: style().fg_rgb::<106, 159, 181>(),
filename: style().fg_rgb::<117, 181, 170>().underline().bold(),
highlights: vec![
style().fg_rgb::<255, 135, 162>(),
style().fg_rgb::<150, 232, 133>(),
style().fg_rgb::<62, 238, 210>(),
style().fg_rgb::<234, 207, 182>(),
style().fg_rgb::<130, 221, 255>(),
style().fg_rgb::<255, 188, 242>(),
],
}
}
pub fn ansi() -> Self {
Self {
error: style().red(),
warning: style().yellow(),
advice: style().cyan(),
code: style().yellow(),
help: style().cyan(),
filename: style().cyan().underline().bold(),
highlights: vec![
style().red().bold(),
style().yellow().bold(),
style().cyan().bold(),
],
}
}
pub fn none() -> Self {
Self {
error: style(),
warning: style(),
advice: style(),
code: style(),
help: style(),
filename: style(),
highlights: vec![style()],
}
}
}
#[allow(missing_docs)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ThemeCharacters {
pub hbar: char,
pub vbar: char,
pub xbar: char,
pub vbar_break: char,
pub uarrow: char,
pub rarrow: char,
pub ltop: char,
pub mtop: char,
pub rtop: char,
pub lbot: char,
pub rbot: char,
pub mbot: char,
pub lbox: char,
pub rbox: char,
pub lcross: char,
pub rcross: char,
pub underbar: char,
pub underline: char,
pub fyi: char,
pub x: char,
pub warning: char,
pub point_right: char,
}
impl ThemeCharacters {
pub fn unicode() -> Self {
Self {
hbar: '─',
vbar: '│',
xbar: '┼',
vbar_break: '·',
uarrow: '▲',
rarrow: '▶',
ltop: '╭',
mtop: '┬',
rtop: '╮',
lbot: '╰',
mbot: '┴',
rbot: '╯',
lbox: '[',
rbox: ']',
lcross: '├',
rcross: '┤',
underbar: '┬',
underline: '─',
fyi: '‽',
x: '×',
warning: '⚠',
point_right: '☞',
}
}
pub fn ascii() -> Self {
Self {
hbar: '-',
vbar: '|',
xbar: '+',
vbar_break: ':',
uarrow: '^',
rarrow: '>',
ltop: ',',
mtop: 'v',
rtop: '.',
lbot: '`',
mbot: '^',
rbot: '\'',
lbox: '[',
rbox: ']',
lcross: '|',
rcross: '|',
underbar: '|',
underline: '^',
fyi: 'i',
x: 'x',
warning: '!',
point_right: '>',
}
}
}