From 3fbc2a7c7827148803dc86776c24bffeb4bef71a Mon Sep 17 00:00:00 2001 From: Willem Vanhulle Date: Wed, 21 Oct 2026 12:10:46 +0200 Subject: [PATCH] refactor: drive the spinner by time instead of frame count --- src/boot_view.rs | 2 -- src/boot_view/panel_render_tests.rs | 4 ++-- src/boot_view/screen_render_fixtures.rs | 1 - src/boot_view/tree_render_tests.rs | 8 ++++---- src/boot_view/unit_tree.rs | 22 +++++++++++++++++++--- src/console_session/watch_loop.rs | 3 --- src/demo_script/scripted_boot.rs | 3 --- 7 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/boot_view.rs b/src/boot_view.rs index f9b8c4b..2f282b7 100644 --- a/src/boot_view.rs +++ b/src/boot_view.rs @@ -61,7 +61,6 @@ pub struct BootView<'a> { pub glyphs: &'a Glyphs, pub now: Duration, pub origin: &'a BootOrigin, - pub frame: u64, pub password_prompt: Option>, } @@ -149,7 +148,6 @@ impl Widget for BootView<'_> { hidden: pending_count - shown_units(&rows), now: self.now, hide_runs_shorter_than: self.timing.hide_runs_shorter_than, - frame: self.frame, colors: &self.colors.tree, glyphs: self.glyphs, }; diff --git a/src/boot_view/panel_render_tests.rs b/src/boot_view/panel_render_tests.rs index 3c022ed..b3aa749 100644 --- a/src/boot_view/panel_render_tests.rs +++ b/src/boot_view/panel_render_tests.rs @@ -28,10 +28,10 @@ fn tree_on_top_timeline_below() { "{screen}" ); assert!( - lines[3].starts_with(" │ ├─ [/] systemd-rfkill"), + lines[3].starts_with(" │ ├─ [|] systemd-rfkill"), "{screen}" ); - assert!(lines[4].starts_with(" │ ├─ [/] docker"), "{screen}"); + assert!(lines[4].starts_with(" │ ├─ [|] docker"), "{screen}"); assert!(lines[5].starts_with(" │ └─ [ ] web"), "{screen}"); assert!(lines[6].starts_with(" ├─ ◇ graphical.target"), "{screen}"); assert!(lines[7].starts_with(" └─ [×] nvidia-powerd"), "{screen}"); diff --git a/src/boot_view/screen_render_fixtures.rs b/src/boot_view/screen_render_fixtures.rs index df46328..89d204e 100644 --- a/src/boot_view/screen_render_fixtures.rs +++ b/src/boot_view/screen_render_fixtures.rs @@ -36,7 +36,6 @@ pub fn render_prompted( generation: "2771".to_owned(), kernel: "Linux 7.2.9".to_owned(), }, - frame: 1, password_prompt, }, frame.area(), diff --git a/src/boot_view/tree_render_tests.rs b/src/boot_view/tree_render_tests.rs index a3e5a42..8d3c98f 100644 --- a/src/boot_view/tree_render_tests.rs +++ b/src/boot_view/tree_render_tests.rs @@ -51,7 +51,7 @@ fn finished_units_linger_in_place_before_leaving() { let lingering = render(&state, 4000 * MS, 110, 24); let screen = lingering.join("\n"); assert!(lingering[3].starts_with(" ├─ [√] early"), "{screen}"); - assert!(lingering[4].starts_with(" └─ [/] late"), "{screen}"); + assert!(lingering[4].starts_with(" └─ [|] late"), "{screen}"); let gone = render(&state, 5500 * MS, 110, 24); let tree: Vec<&String> = gone .iter() @@ -86,7 +86,7 @@ fn chains_stay_unfolded_while_they_fit() { "{screen}" ); assert!( - lines[7].starts_with(" └─ [/] udev-settle"), + lines[7].starts_with(" └─ [-] udev-settle"), "{screen}" ); } @@ -100,7 +100,7 @@ fn chains_fold_when_short_and_shorten_to_their_ends_when_narrow() { short.join("\n") ); assert!( - short[4].starts_with(" └─ [/] udev-settle"), + short[4].starts_with(" └─ [-] udev-settle"), "{}", short.join("\n") ); @@ -149,7 +149,7 @@ fn non_service_units_carry_their_kind() { .iter() .find(|line| line.contains("Docker Socket")) .unwrap_or_else(|| panic!("{screen}")); - assert!(socket_row.contains("[/] docker "), "{screen}"); + assert!(socket_row.contains("[-] docker "), "{screen}"); assert!(socket_row.contains(" Socket "), "{screen}"); assert!( render(&described_boot(), 3000 * MS, 110, 24) diff --git a/src/boot_view/unit_tree.rs b/src/boot_view/unit_tree.rs index 41b7d8f..652cd68 100644 --- a/src/boot_view/unit_tree.rs +++ b/src/boot_view/unit_tree.rs @@ -16,6 +16,7 @@ use crate::{ }; const SPINNER: [char; 4] = ['|', '/', '-', '\\']; +const SPINNER_STEP: Duration = Duration::from_millis(100); const MIN_DESCRIPTION_WIDTH: u16 = 12; const MIN_TAG_WIDTH: u16 = 6; const MIN_LABEL_WIDTH: u16 = 24; @@ -27,7 +28,6 @@ pub struct UnitTree<'a> { pub hidden: usize, pub now: Duration, pub hide_runs_shorter_than: Duration, - pub frame: u64, pub colors: &'a TreeColors, pub glyphs: &'a Glyphs, } @@ -50,6 +50,11 @@ fn opens_a_stage(row: &TreeRow) -> bool { row.depth == 0 && row.kind == UnitKind::Target } +fn spinner_at(now: Duration) -> char { + let steps = now.as_millis() / SPINNER_STEP.as_millis(); + SPINNER[usize::try_from(steps % 4).unwrap_or_default()] +} + fn width_of(text: &str) -> u16 { u16::try_from(text.width()).unwrap_or(u16::MAX) } @@ -134,8 +139,7 @@ impl UnitTree<'_> { RowStatus::Failed => format!("[{}] ", self.glyphs.failed).fg(self.colors.failed_unit), _ if row.kind == UnitKind::Target => Span::default(), RowStatus::Running { .. } => { - let spinner = SPINNER[usize::try_from(self.frame % 4).unwrap_or_default()]; - format!("[{spinner}] ").fg(self.colors.running_marker) + format!("[{}] ", spinner_at(self.now)).fg(self.colors.running_marker) } RowStatus::Waiting => "[ ] ".fg(self.colors.waiting_unit), RowStatus::Done => format!("[{}] ", self.glyphs.done).fg(self.colors.finished_marker), @@ -303,6 +307,18 @@ fn tree_prefixes(rows: &[TreeRow]) -> Vec { mod tests { use super::*; + #[test] + fn the_spinner_turns_once_per_step_whatever_the_frame_rate() { + let turned: String = (0..5) + .map(|step| spinner_at(SPINNER_STEP * step + Duration::from_millis(16))) + .collect(); + assert_eq!(turned, "|/-\\|"); + assert_eq!( + spinner_at(Duration::from_millis(99)), + spinner_at(Duration::ZERO) + ); + } + fn at_depth(depth: usize) -> TreeRow { TreeRow { depth, diff --git a/src/console_session/watch_loop.rs b/src/console_session/watch_loop.rs index 7f5d4d2..a924b2f 100644 --- a/src/console_session/watch_loop.rs +++ b/src/console_session/watch_loop.rs @@ -137,7 +137,6 @@ fn watch( ) -> Result<(), Error> { let mut origin = BootOrigin::read(); let mut origin_phase = state.phase; - let mut frame: u64 = 0; let mut painted_connections = feed.connections; let mut last_full_repaint = Instant::now(); let mut next_frame = Instant::now(); @@ -171,7 +170,6 @@ fn watch( glyphs: &console.glyphs, now: monotonic_now(), origin: &origin, - frame, password_prompt: password_entry.as_ref().map(PasswordEntry::prompt), }, canvas.area(), @@ -208,7 +206,6 @@ fn watch( next_frame.saturating_duration_since(Instant::now()), )?; if Instant::now() >= next_frame { - frame += 1; next_frame = Instant::now() + FRAME_INTERVAL; } while event::poll(Duration::ZERO)? { diff --git a/src/demo_script/scripted_boot.rs b/src/demo_script/scripted_boot.rs index e77bb73..0001aad 100644 --- a/src/demo_script/scripted_boot.rs +++ b/src/demo_script/scripted_boot.rs @@ -303,7 +303,6 @@ pub fn play(terminal: &mut DefaultTerminal, scenario: &Scenario) -> io::Result<( let mut script = Script::begin(scenario.steps, scenario.describe, 1, &mut state, now()); let mut switched_root = scenario.after_switch_root.is_empty(); let mut settled_at = None; - let mut frame = 0; let mut typed = passphrase_input::empty(); loop { script.advance(&mut state, now()); @@ -327,7 +326,6 @@ pub fn play(terminal: &mut DefaultTerminal, scenario: &Scenario) -> io::Result<( glyphs: &Glyphs::default(), now: now(), origin: &origin, - frame, password_prompt: script.prompt(&typed, now()), }, canvas.area(), @@ -349,6 +347,5 @@ pub fn play(terminal: &mut DefaultTerminal, scenario: &Scenario) -> io::Result<( script.answer(&submitted, now()); } } - frame += 1; } }