use makeover_layout::{Awaiting, HEAT_BANDS, Heading, Heat, Token, Tone, progress_reading};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use crate::Palette;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct BadgeStyle {
pub fill: Style,
pub edge: Style,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct PieceStyle {
pub focus: Modifier,
pub meter_cells: u16,
pub meter_full: char,
pub meter_empty: char,
pub required_marker: &'static str,
pub badge_edges: [&'static str; 2],
}
impl Default for PieceStyle {
fn default() -> Self {
Self {
focus: Modifier::REVERSED,
meter_cells: 10,
meter_full: '#',
meter_empty: '-',
required_marker: "*",
badge_edges: ["\u{258C}", "\u{2590}"],
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Ink {
pub content: Style,
pub secondary: Style,
pub muted: Style,
pub info: Style,
pub success: Style,
pub warning: Style,
pub danger: Style,
pub page: Style,
pub section: Style,
pub subsection: Style,
pub action: Style,
pub filled: Style,
pub sunken: Style,
pub badges: [Option<BadgeStyle>; 5],
pub heats: [Style; HEAT_BANDS as usize],
pub style: PieceStyle,
}
impl Ink {
#[must_use]
pub fn new(palette: &Palette, style: &PieceStyle) -> Self {
let t = &palette.tones;
let fg = |c: Option<Color>| c.map_or(Style::new(), |c| Style::new().fg(c));
let or = |c: Option<Color>, missing: Modifier| match c {
Some(c) => Style::new().fg(c),
None => Style::new().add_modifier(missing),
};
let badge = |fill: Option<Color>, edge: Option<Color>| {
fill.map(|fill| BadgeStyle {
fill: fg(t.content).bg(fill),
edge: fg(edge),
})
};
Self {
content: fg(t.content),
secondary: fg(t.secondary),
muted: or(t.muted, Modifier::DIM),
info: fg(t.info),
success: fg(t.success),
warning: fg(t.warning),
danger: or(t.danger, Modifier::BOLD),
page: fg(t.action).add_modifier(Modifier::BOLD),
section: fg(t.content).add_modifier(Modifier::BOLD),
subsection: fg(t.secondary),
action: or(t.action, Modifier::UNDERLINED),
filled: match t.action {
Some(action) => fg(t.on_action).bg(action),
None => Style::new().add_modifier(Modifier::REVERSED),
},
sunken: match t.sunken {
Some(sunken) => Style::new().bg(sunken),
None => Style::new().add_modifier(Modifier::DIM),
},
badges: [
badge(t.neutral_surface, t.border),
badge(t.info_surface, t.info),
badge(t.success_surface, t.success),
badge(t.warning_surface, t.warning),
badge(t.danger_surface, t.danger),
],
heats: [None, t.heat_1, t.heat_2, t.heat_3, t.heat_4]
.map(|c| c.map_or(Style::new(), |c| Style::new().bg(c))),
style: *style,
}
}
#[must_use]
pub const fn tone(&self, tone: Tone) -> Style {
match tone {
Tone::Info => self.info,
Tone::Success => self.success,
Tone::Warning => self.warning,
Tone::Danger => self.danger,
_ => self.content,
}
}
#[must_use]
pub const fn heat(&self, heat: Heat) -> Style {
let last = HEAT_BANDS - 1;
self.heats[(if heat.0 > last { last } else { heat.0 }) as usize]
}
#[must_use]
pub const fn badge(&self, tone: Tone) -> Option<BadgeStyle> {
self.badges[match tone {
Tone::Info => 1,
Tone::Success => 2,
Tone::Warning => 3,
Tone::Danger => 4,
_ => 0,
}]
}
#[must_use]
pub const fn heading(&self, level: Heading) -> Style {
match level {
Heading::Page => self.page,
Heading::Section => self.section,
_ => self.subsection,
}
}
#[must_use]
pub fn focused(&self, focused: bool, style: Style) -> Style {
if focused {
style.add_modifier(self.style.focus)
} else {
style
}
}
}
pub use makeover_layout::Progress;
#[must_use]
pub fn activity(lit: bool, ink: &Ink) -> Span<'static> {
if lit {
Span::styled(ink.style.meter_full.to_string(), ink.action)
} else {
Span::styled(ink.style.meter_empty.to_string(), ink.muted)
}
}
#[must_use]
pub fn awaiting(awaiting: Awaiting, progress: Progress, lit: bool, ink: &Ink) -> Line<'static> {
let Some(total) = awaiting.amount else {
return Line::from(vec![activity(lit, ink)]);
};
let Some(done) = progress.delivered else {
return Line::from(vec![
activity(lit, ink),
Span::styled(format!(" {total}"), ink.muted),
]);
};
let cells = u32::from(ink.style.meter_cells);
let filled = u32::try_from(
done.saturating_mul(u64::from(cells))
.checked_div(total)
.unwrap_or(0),
)
.unwrap_or(cells)
.min(cells);
let bar = format!(
"{}{}",
ink.style.meter_full.to_string().repeat(filled as usize),
ink.style
.meter_empty
.to_string()
.repeat((cells - filled) as usize)
);
let reading = format!(" {}", progress_reading(done, total, progress.elapsed));
Line::from(vec![
Span::styled(bar, ink.action),
Span::styled(reading, ink.muted),
])
}
#[must_use]
pub fn token(
label: &str,
kind: Token,
tone: Tone,
latched: bool,
focused: bool,
ink: &Ink,
) -> Line<'static> {
let mark = |painted: Style| {
if latched {
painted.add_modifier(ink.style.focus)
} else {
ink.focused(focused, painted)
}
};
match (kind, ink.badge(tone)) {
(Token::Chip { .. }, _) => bracketed(label, ["[", "]"], tone, ink, mark),
(Token::Badge, Some(badge)) => {
let [open, close] = ink.style.badge_edges;
let edge = mark(badge.fill.patch(badge.edge));
Line::from(vec![
Span::styled(open, edge),
Span::styled(label.to_owned(), mark(badge.fill)),
Span::styled(close, edge),
])
}
_ => bracketed(label, ["(", ")"], tone, ink, mark),
}
}
fn bracketed(
label: &str,
[open, close]: [&'static str; 2],
tone: Tone,
ink: &Ink,
mark: impl Fn(Style) -> Style,
) -> Line<'static> {
let edge = mark(ink.tone(tone));
Line::from(vec![
Span::styled(open, edge),
Span::styled(label.to_owned(), mark(ink.content)),
Span::styled(close, edge),
])
}
#[cfg(test)]
mod tests;