refactor: drive the spinner by time instead of frame count

This commit is contained in:
Willem Vanhulle committed 2026-10-21 12:49:33 +02:00
1 parent a82cca2857
commit 3fbc2a7c78
7 files changed
+25 -18

No files matched your search

-2
View File
@@ -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<PasswordPrompt<'a>>,
}
@@ -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,
};
+2 -2
View File
@@ -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}");
-1
View File
@@ -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(),
+4 -4
View File
@@ -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)
+19 -3
View File
@@ -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<String> {
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,
-3
View File
@@ -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)? {
-3
View File
@@ -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;
}
}