Skip to main content

ados_term/progress/
events.rs

1//! Progress events, the step→group map, and the per-flow wording.
2//!
3//! A step engine emits [`ProgressEvent`]s onto a channel; a renderer thread
4//! consumes them and draws the live progress. Technical steps are collapsed
5//! into human-readable groups so the checklist reads at a glance; a group
6//! lights up when any of its member steps starts and finalizes when all of them
7//! have a result.
8
9use std::collections::VecDeque;
10
11use crate::outcome::StepOutcome;
12use crate::theme::Theme;
13
14/// A checklist group set: friendly labels (in display order) each mapping to one
15/// or more technical step ids. The renderers are generic over this.
16pub type GroupMap = &'static [(&'static str, &'static [&'static str])];
17
18/// The display group index a step belongs to within `groups`, if any.
19pub fn group_index_for_step(groups: GroupMap, step_id: &str) -> Option<usize> {
20    groups
21        .iter()
22        .position(|(_, steps)| steps.contains(&step_id))
23}
24
25/// One flow's groups and wording (an install, an uninstall, a deploy).
26#[derive(Debug, Clone, Copy)]
27pub struct ProgressSpec {
28    /// The checklist groups.
29    pub groups: GroupMap,
30    /// The full-screen footer reassurance line.
31    pub footer: &'static str,
32    /// The title on the inline renderer's top border.
33    pub rich_title: &'static str,
34    /// The checklist token for an all-skipped group (inline + full-screen).
35    pub skipped: &'static str,
36    /// The detail for an all-skipped group on the plain renderer.
37    pub plain_skipped: &'static str,
38    /// The line printed after Ctrl-C leaves the full-screen view (exit 130).
39    pub interrupted: &'static str,
40}
41
42/// The closing summary a renderer draws once the run ends: a success card or a
43/// failure panel. The tool owns its content; the renderers only place it.
44pub trait SummaryView: Send + std::fmt::Debug {
45    /// Escape-free lines for the plain and quiet renderers.
46    fn plain_lines(&self) -> Vec<String>;
47    /// Styled lines printed on the primary screen after the live view ends.
48    /// `logs` is the renderer's ring of recent log lines (for a failure panel).
49    fn rich_lines(&self, theme: &Theme, logs: &VecDeque<String>) -> Vec<String>;
50    /// A full-screen grid (`rows` lines of `cols` visible columns) shown inside
51    /// the alternate screen at the end of an interactive run.
52    fn fullscreen_grid(
53        &self,
54        theme: &Theme,
55        journal: &VecDeque<String>,
56        cols: usize,
57        rows: usize,
58    ) -> Vec<String>;
59    /// Whether the run failed. An interactive full-screen run holds a closing
60    /// card for a key press only when it did not.
61    fn failed(&self) -> bool;
62}
63
64/// An event emitted from the step engine to the renderer thread.
65#[derive(Debug)]
66pub enum ProgressEvent {
67    /// A step's `run()` is about to execute (lights up its group's spinner).
68    StepStarted {
69        /// The step id.
70        id: String,
71    },
72    /// A step finished (ran or was skipped) with the given outcome.
73    StepResult {
74        /// The step id.
75        id: String,
76        /// What happened.
77        outcome: StepOutcome,
78    },
79    /// Incremental sub-progress for a step that reports a fraction.
80    /// `done`/`total` are in the step's own units.
81    SubProgress {
82        /// The step id.
83        id: String,
84        /// Units done.
85        done: u64,
86        /// Units in total.
87        total: u64,
88    },
89    /// A curated one-line headline for the running step ("installing ffmpeg")
90    /// — the accent line in the live-detail pane.
91    Activity {
92        /// The step id.
93        id: String,
94        /// The headline.
95        message: String,
96    },
97    /// One raw subprocess line for the running step's dim scrolling log tail.
98    SubLog {
99        /// The step id.
100        id: String,
101        /// The raw line.
102        line: String,
103    },
104    /// Byte-level download progress for the running step's current file.
105    /// `total` is 0 when the size is unknown. `label` names the file so the
106    /// detail pane reads "service-name 4.2/8.1 MB".
107    ByteProgress {
108        /// The step id.
109        id: String,
110        /// Bytes done.
111        done: u64,
112        /// Bytes in total, or 0 when unknown.
113        total: u64,
114        /// The file being fetched.
115        label: String,
116    },
117    /// A forwarded log line (from the tracing layer).
118    Log {
119        /// The log level.
120        level: tracing::Level,
121        /// The message.
122        line: String,
123    },
124    /// The closing summary: render the success card / failure panel.
125    Summary(Box<dyn SummaryView>),
126    /// Stop the renderer loop and restore the terminal.
127    Finished,
128}
129
130#[cfg(test)]
131mod tests {
132    use super::*;
133
134    const GROUPS: GroupMap = &[("Prepare", &["a", "b"]), ("Finish", &["c"])];
135
136    #[test]
137    fn group_lookup_resolves() {
138        assert_eq!(group_index_for_step(GROUPS, "a"), Some(0));
139        assert_eq!(group_index_for_step(GROUPS, "b"), Some(0));
140        assert_eq!(group_index_for_step(GROUPS, "c"), Some(1));
141        assert_eq!(group_index_for_step(GROUPS, "nope"), None);
142    }
143}