use crate::CursorShape;
use crate::style::{Color, Style};
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct StyleState {
pub base: Style,
pub text: Style,
pub line_number: Style,
pub cursor_line_number: Style,
pub cursor_line: Style,
pub end_of_buffer: Style,
pub placeholder: Style,
pub prompt: Style,
pub selection: Style,
}
impl StyleState {
pub(super) const fn cursor_line(&self) -> Style {
self.cursor_line.plain().inherit(self.base.plain())
}
pub(super) const fn cursor_line_number(&self) -> Style {
self.cursor_line_number
.plain()
.inherit(self.cursor_line.plain())
.inherit(self.base.plain())
}
pub(super) const fn end_of_buffer(&self) -> Style {
self.end_of_buffer.plain().inherit(self.base.plain())
}
pub(super) const fn line_number(&self) -> Style {
self.line_number.plain().inherit(self.base.plain())
}
pub(super) const fn placeholder(&self) -> Style {
self.placeholder.plain().inherit(self.base.plain())
}
pub(super) const fn prompt(&self) -> Style {
self.prompt.plain().inherit(self.base.plain())
}
pub(super) const fn text(&self) -> Style {
self.text.plain().inherit(self.base.plain())
}
pub(super) const fn selection(&self) -> Style {
self.selection
.plain()
.inherit(self.text.plain())
.inherit(self.base.plain())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CursorStyle {
pub color: Color,
pub shape: CursorShape,
pub blink: bool,
pub blink_speed: Duration,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Styles {
pub focused: StyleState,
pub blurred: StyleState,
pub cursor: CursorStyle,
}
impl Styles {
#[must_use]
pub fn new(is_dark: bool) -> Self {
let pick = |dark: u8, light: u8| if is_dark { color(dark) } else { color(light) };
let fg = |c: Color| Style::new().fg(c);
let common = StyleState {
base: Style::new(),
text: Style::new(),
line_number: fg(pick(7, 249)),
cursor_line_number: fg(color(240)),
cursor_line: Style::new().bg(pick(0, 255)),
end_of_buffer: fg(pick(0, 254)),
placeholder: fg(color(240)),
prompt: fg(color(7)),
selection: Style::new().bg(pick(8, 253)),
};
Self {
focused: common,
blurred: StyleState {
cursor_line: fg(pick(7, 245)),
cursor_line_number: fg(pick(7, 249)),
text: fg(pick(7, 245)),
..common
},
cursor: CursorStyle {
color: Color::Ansi(7),
shape: CursorShape::Block,
blink: true,
blink_speed: Duration::ZERO,
},
}
}
}
const fn color(n: u8) -> Color {
if n < 16 {
Color::Ansi(n)
} else {
Color::Palette(n)
}
}