refactor: group modules by data-flow stage behind a thin main

This commit is contained in:
Willem Vanhulle committed 2026-10-21 12:12:49 +02:00
1 parent f9cc94c633
commit 2191a27b82
38 files changed
+104 -88

No files matched your search

+169
View File
@@ -0,0 +1,169 @@
use ratatui::style::Color;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
pub struct Colors {
pub header: HeaderColors,
pub stages: StageColors,
pub tree: TreeColors,
pub timeline: TimelineColors,
pub footer: FooterColors,
pub password: PasswordColors,
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
pub struct HeaderColors {
pub origin: Color,
pub separators: Color,
pub total_time: Color,
}
impl Default for HeaderColors {
fn default() -> Self {
Self {
origin: Color::White,
separators: Color::DarkGray,
total_time: Color::White,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
pub struct StageColors {
pub branches: Color,
pub finished_label: Color,
pub finished_time: Color,
pub unit_count: Color,
pub failed_count: Color,
pub current: Color,
}
impl Default for StageColors {
fn default() -> Self {
Self {
branches: Color::DarkGray,
finished_label: Color::Gray,
finished_time: Color::Green,
unit_count: Color::Gray,
failed_count: Color::LightRed,
current: Color::White,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
pub struct TreeColors {
pub branches: Color,
pub columns: Color,
pub target: Color,
pub current_stage: Color,
pub reached_stage: Color,
pub running_unit: Color,
pub running_marker: Color,
pub waiting_unit: Color,
pub finished_unit: Color,
pub finished_marker: Color,
pub failed_unit: Color,
pub overflow: Color,
}
impl Default for TreeColors {
fn default() -> Self {
Self {
branches: Color::DarkGray,
columns: Color::DarkGray,
target: Color::Cyan,
current_stage: Color::Yellow,
reached_stage: Color::Green,
running_unit: Color::White,
running_marker: Color::Yellow,
waiting_unit: Color::Gray,
finished_unit: Color::DarkGray,
finished_marker: Color::Green,
failed_unit: Color::LightRed,
overflow: Color::Gray,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
pub struct TimelineColors {
pub rule: Color,
pub title: Color,
pub axis: Color,
pub milestone: Color,
pub finished: Color,
pub finished_label: Color,
pub failed: Color,
pub queued_bar: Color,
pub running: Color,
pub running_label: Color,
}
impl Default for TimelineColors {
fn default() -> Self {
Self {
rule: Color::DarkGray,
title: Color::Gray,
axis: Color::DarkGray,
milestone: Color::Green,
finished: Color::Green,
finished_label: Color::Gray,
failed: Color::LightRed,
queued_bar: Color::Gray,
running: Color::Yellow,
running_label: Color::White,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
pub struct FooterColors {
pub rule: Color,
pub done: Color,
pub running: Color,
pub waiting: Color,
pub failed: Color,
pub no_failures: Color,
}
impl Default for FooterColors {
fn default() -> Self {
Self {
rule: Color::DarkGray,
done: Color::Green,
running: Color::Yellow,
waiting: Color::Gray,
failed: Color::LightRed,
no_failures: Color::Gray,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
pub struct PasswordColors {
pub frame: Color,
pub message: Color,
pub hint: Color,
pub checking: Color,
pub backdrop: Color,
}
impl Default for PasswordColors {
fn default() -> Self {
Self {
frame: Color::Yellow,
message: Color::White,
hint: Color::DarkGray,
checking: Color::Gray,
backdrop: Color::DarkGray,
}
}
}
+112
View File
@@ -0,0 +1,112 @@
use std::{fs, io, path::Path, time::Duration};
use heck::ToKebabCase;
use jiff::fmt::serde::unsigned_duration::friendly::compact::required as friendly_duration;
use serde::{Deserialize, Serialize};
use toml::{Table, Value, de::Error as ParseError, ser::Error as SerializeError};
use crate::screen_settings::color_scheme::Colors;
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
pub struct Config {
pub colors: Colors,
pub timing: Timing,
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
pub struct Timing {
#[serde(with = "friendly_duration")]
pub keep_finished_units_for: Duration,
#[serde(with = "friendly_duration")]
pub hide_runs_shorter_than: Duration,
#[serde(with = "friendly_duration")]
pub keep_final_frame_for: Duration,
}
impl Default for Timing {
fn default() -> Self {
Self {
keep_finished_units_for: Duration::from_secs(2),
hide_runs_shorter_than: Duration::from_secs(1),
keep_final_frame_for: Duration::from_secs(1),
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
#[error("cannot read: {0}")]
Unreadable(#[from] io::Error),
#[error("invalid: {0}")]
Invalid(#[from] ParseError),
}
pub fn read_config_file(path: &Path) -> Result<Config, ConfigError> {
Ok(toml::from_str(&fs::read_to_string(path)?)?)
}
pub fn default_config_table() -> Result<Table, SerializeError> {
Table::try_from(Config::default()).map(kebab_color_names)
}
fn kebab_color_names(table: Table) -> Table {
table
.into_iter()
.map(|(key, value)| {
let value = match value {
Value::Table(nested) => Value::Table(kebab_color_names(nested)),
Value::String(name) if name.starts_with(char::is_alphabetic) => {
Value::String(name.to_kebab_case())
}
other => other,
};
(key, value)
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn printed_defaults() -> String {
toml::to_string_pretty(&default_config_table().expect("serializable defaults"))
.expect("printable defaults")
}
#[test]
fn timing_reads_friendly_durations_and_keeps_unset_defaults() {
let config: Config = toml::from_str("[timing]\nkeep-finished-units-for = \"500ms\"\n")
.expect("valid config");
assert_eq!(
config.timing.keep_finished_units_for,
Duration::from_millis(500)
);
assert_eq!(
config.timing.keep_final_frame_for,
Timing::default().keep_final_frame_for
);
}
#[test]
fn nonsense_durations_are_rejected() {
assert!(toml::from_str::<Config>("[timing]\nkeep-final-frame-for = \"soon\"\n").is_err());
}
#[test]
fn unknown_color_keys_are_rejected() {
assert!(toml::from_str::<Config>("[colors.tree]\nbogus = \"red\"\n").is_err());
}
#[test]
fn printed_defaults_read_back_unchanged() {
let printed = printed_defaults();
assert!(printed.contains("failed-unit = \"light-red\""), "{printed}");
assert_eq!(
toml::from_str::<Config>(&printed).expect("valid defaults"),
Config::default()
);
}
}
+102
View File
@@ -0,0 +1,102 @@
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Glyphs {
pub done: char,
pub failed: char,
pub bead: char,
pub waiting_bead: char,
pub still_going: char,
pub running_mark: char,
pub root_switch_mark: char,
pub stage_current: char,
pub stage_upcoming: char,
pub ellipsis: &'static str,
pub chain_link: &'static str,
}
const DONE: [char; 2] = ['√', '+'];
const FAILED: [char; 2] = ['×', 'x'];
const BEAD: [char; 2] = ['■', '#'];
const WAITING_BEAD: [char; 2] = ['·', '.'];
const STILL_GOING: [char; 2] = ['▶', '>'];
const RUNNING_MARK: [char; 3] = ['►', '▶', '>'];
const ROOT_SWITCH_MARK: [char; 2] = ['│', '|'];
const STAGE_MARKS: [&str; 3] = ["◆◇", "●○", "*o"];
const ELLIPSIS: [&str; 2] = ["…", "..."];
const CHAIN_LINK: [&str; 2] = ["─", "-"];
impl Default for Glyphs {
fn default() -> Self {
Self::fitting(|_| true)
}
}
impl Glyphs {
pub fn fitting(covers: impl Fn(char) -> bool) -> Self {
let symbol = |candidates: &[char]| {
candidates
.iter()
.copied()
.find(|candidate| covers(*candidate))
.unwrap_or(candidates[candidates.len() - 1])
};
let text = |candidates: &[&'static str]| {
candidates
.iter()
.copied()
.find(|candidate| candidate.chars().all(&covers))
.unwrap_or(candidates[candidates.len() - 1])
};
let stage_marks: Vec<char> = text(&STAGE_MARKS).chars().collect();
Self {
done: symbol(&DONE),
failed: symbol(&FAILED),
bead: symbol(&BEAD),
waiting_bead: symbol(&WAITING_BEAD),
still_going: symbol(&STILL_GOING),
running_mark: symbol(&RUNNING_MARK),
root_switch_mark: symbol(&ROOT_SWITCH_MARK),
stage_current: stage_marks[0],
stage_upcoming: stage_marks[1],
ellipsis: text(&ELLIPSIS),
chain_link: text(&CHAIN_LINK),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const LAT2_TERMINUS16: &str = "×■●▶·•…│├└─━█▒░↑↓←→≥";
const DEFAULT8X16: &str = "√■○►▶·•│├└─▌▐█▓▒░▀▄↑↓←→≥";
fn covered_by(font: &'static str) -> impl Fn(char) -> bool {
move |symbol| symbol.is_ascii() || font.contains(symbol)
}
#[test]
fn a_font_without_the_square_root_and_pointer_falls_back() {
let glyphs = Glyphs::fitting(covered_by(LAT2_TERMINUS16));
assert_eq!(glyphs.done, '+');
assert_eq!(glyphs.running_mark, '▶');
assert_eq!(glyphs.failed, '×');
assert_eq!(glyphs.ellipsis, "…");
assert_eq!((glyphs.stage_current, glyphs.stage_upcoming), ('*', 'o'));
}
#[test]
fn the_built_in_kernel_font_loses_the_cross_and_ellipsis() {
let glyphs = Glyphs::fitting(covered_by(DEFAULT8X16));
assert_eq!(glyphs.done, '√');
assert_eq!(glyphs.failed, 'x');
assert_eq!(glyphs.ellipsis, "...");
assert_eq!(glyphs.chain_link, "─");
assert_eq!((glyphs.stage_current, glyphs.stage_upcoming), ('*', 'o'));
}
#[test]
fn full_unicode_keeps_every_preferred_glyph() {
let glyphs = Glyphs::default();
assert_eq!((glyphs.done, glyphs.running_mark), ('√', '►'));
}
}