diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f807413..73c0ed7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -16,7 +16,7 @@ Work inside the dev shell (`direnv allow` once). Run `cargo clippy - `nix build .#checks.x86_64-linux.boot`: boots a VM and reads the screen with OCR; screenshots land in `result/`. Run it by hand with `nix run .#checks.x86_64-linux.boot.driverInteractive`, then `test_script()`. - The images in `docs/` are crops of these screenshots. + The images in `docs/` are copies of these screenshots. Prefix the VM commands with `env -u LD_LIBRARY_PATH` when the shell exports libraries that clash with QEMU's. diff --git a/README.md b/README.md index 3d962dd..9f8d638 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ units still pending and a timeline of the ones that ran. ![The initrd: pending units on top, the timeline below](docs/initrd.png) ![Stage 2: the initrd collapsed, a failed unit, three still running](docs/stage-2.png) +![Shutdown: the unit still stopping at the end of the chain waiting on it](docs/shutdown.png) It needs systemd ≥ 246 as PID 1, libdbus and a virtual terminal (it draws on tty12). With a systemd initrd it starts about a second after the kernel and diff --git a/docs/initrd.png b/docs/initrd.png index 4f7205e..0e8eb97 100644 Binary files a/docs/initrd.png and b/docs/initrd.png differ diff --git a/docs/shutdown.png b/docs/shutdown.png new file mode 100644 index 0000000..86a1244 Binary files /dev/null and b/docs/shutdown.png differ diff --git a/docs/stage-2.png b/docs/stage-2.png index fef2768..15c3d62 100644 Binary files a/docs/stage-2.png and b/docs/stage-2.png differ diff --git a/src/model.rs b/src/model.rs index 58ac80d..b2a590a 100644 --- a/src/model.rs +++ b/src/model.rs @@ -685,6 +685,57 @@ impl MilestoneWalk { } } +#[must_use] +pub fn paths_to_running(rows: Vec) -> Vec { + let mut kept_below: Vec = Vec::new(); + let mut kept: Vec = rows + .iter() + .rev() + .map(|row| { + let descendant_kept = kept_below.iter().skip(row.depth + 1).any(|kept| *kept); + kept_below.resize(row.depth + 1, false); + let anchors_the_tree = row.depth == 0 && row.is_target; + let keep = row.status == RowStatus::Running || descendant_kept || anchors_the_tree; + kept_below[row.depth] |= keep; + keep + }) + .collect(); + kept.reverse(); + let survivors: Vec = rows + .into_iter() + .zip(kept) + .filter_map(|(row, keep)| keep.then_some(row)) + .collect(); + relink_siblings(survivors) +} + +fn relink_siblings(rows: Vec) -> Vec { + let mut later_sibling: Vec = Vec::new(); + let mut is_last: Vec = rows + .iter() + .rev() + .map(|row| { + let last = !later_sibling.get(row.depth).copied().unwrap_or(false); + later_sibling.resize(row.depth + 1, false); + later_sibling[row.depth] = true; + last + }) + .collect(); + is_last.reverse(); + let mut ancestry: Vec = Vec::new(); + rows.into_iter() + .zip(is_last) + .map(|(row, last)| { + ancestry.truncate(row.depth); + ancestry.push(last); + PendingRow { + is_last_sibling: ancestry.clone(), + ..row + } + }) + .collect() +} + fn display_label(facts: &UnitFacts) -> String { if let Some(stem) = facts.name.strip_suffix(".service") { return stem.to_owned(); @@ -780,6 +831,43 @@ mod tests { ); } + #[test] + fn only_chains_leading_to_running_units_stay_in_view() { + let rows = paths_to_running(sample().pending_rows()); + let outline: Vec = rows + .iter() + .map(|row| format!("{}{}", " ".repeat(row.depth), row.label)) + .collect(); + assert_eq!( + outline, + [ + "graphical.target", + " multi-user.target", + " docker", + " nm-wait", + " nm", + ] + ); + let lasts: Vec = rows + .iter() + .map(|row| *row.is_last_sibling.last().unwrap()) + .collect(); + assert_eq!(lasts, [true, true, false, true, true]); + } + + #[test] + fn with_nothing_running_only_the_root_target_stays_in_view() { + let mut state = BootState::new(Phase::Host); + state.set_root("demo.target".to_owned()); + state.track_unit(facts("demo.target", &["idle.service"]), Activity::default()); + state.track_unit(facts("idle.service", &[]), Activity::default()); + state.queue_job(1, "demo.target", JobKind::BringsUp, Duration::ZERO); + state.queue_job(2, "idle.service", JobKind::BringsUp, Duration::ZERO); + let rows = paths_to_running(state.pending_rows()); + assert_eq!(rows.len(), 1); + assert_eq!(rows[0].label, "demo.target"); + } + #[test] fn finished_jobs_leave_the_tree_and_count_toward_their_milestone() { let mut state = sample(); diff --git a/src/run.rs b/src/run.rs index ab50f21..4260673 100644 --- a/src/run.rs +++ b/src/run.rs @@ -16,7 +16,8 @@ use bootvine::{ use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers}; use ratatui::{ DefaultTerminal, - backend::{Backend, ClearType}, + backend::Backend, + buffer::{Buffer, CellDiffOption}, }; use rustix::system::uname; use signal_hook::{consts::SIGTERM, flag}; @@ -89,6 +90,21 @@ fn asks_to_quit(event: &Event) -> bool { || (*code == KeyCode::Char('c') && modifiers.contains(KeyModifiers::CONTROL)) } +fn overwrite_every_cell(backend: &mut B, frame: &Buffer) -> Result<(), B::Error> { + backend.draw( + frame + .content + .iter() + .enumerate() + .filter(|(_, cell)| cell.diff_option != CellDiffOption::Skip) + .map(|(index, cell)| { + let (x, y) = frame.pos_of(index); + (x, y, cell) + }), + )?; + backend.flush() +} + fn watch( state: &mut BootState, feed: &mut Feed, @@ -103,16 +119,9 @@ fn watch( let mut last_full_repaint = Instant::now(); loop { feed.pump(state, FRAME_INTERVAL)?; - if feed.session.is_none() - || feed.connections != painted_connections - || last_full_repaint.elapsed() >= FULL_REPAINT_INTERVAL - { - terminal.backend_mut().clear_region(ClearType::All)?; - terminal.swap_buffers(); - painted_connections = feed.connections; - last_full_repaint = Instant::now(); - } - terminal.draw(|canvas| { + let repaint_due = feed.connections != painted_connections + || last_full_repaint.elapsed() >= FULL_REPAINT_INTERVAL; + let completed = terminal.draw(|canvas| { canvas.render_widget( BootView { state, @@ -124,6 +133,12 @@ fn watch( canvas.area(), ); })?; + if repaint_due { + let painted = completed.buffer.clone(); + overwrite_every_cell(terminal.backend_mut(), &painted)?; + painted_connections = feed.connections; + last_full_repaint = Instant::now(); + } if state.is_complete() { eprintln!( "bootvine: {} reached, handing the console over", diff --git a/src/view.rs b/src/view.rs index 09bbdee..e38481f 100644 --- a/src/view.rs +++ b/src/view.rs @@ -14,7 +14,7 @@ use ratatui::{ }; use crate::{ - model::{BootState, FinishedRun, MilestoneRow, PendingRow, RowStatus, Tally}, + model::{BootState, FinishedRun, MilestoneRow, PendingRow, RowStatus, Tally, paths_to_running}, palette::Palette, }; @@ -36,6 +36,7 @@ const TIMELINE_LABEL_WIDTH: u16 = 28; const MIN_BAR_WIDTH: u16 = 12; const MIN_HEIGHT_FOR_TIMELINE: u16 = 12; const TIMELINE_CHROME_HEIGHT: u16 = 3; +const MIN_TIMELINE_HEIGHT: u16 = TIMELINE_CHROME_HEIGHT + 4; const HEADER_HEIGHT: u16 = 2; const FOOTER_HEIGHT: u16 = 2; const MAX_FAILED_ROWS: usize = 3; @@ -80,7 +81,10 @@ struct TimelineContent<'a> { impl Widget for BootView<'_> { fn render(self, area: Rect, buf: &mut Buffer) { - let rows = self.state.pending_rows(); + let all_rows = self.state.pending_rows(); + let pending_count = all_rows.len(); + let rows = paths_to_running(all_rows); + let folded = pending_count - rows.len(); let mut running: Vec<&PendingRow> = rows .iter() .filter(|row| !row.is_target && row.status == RowStatus::Running) @@ -108,7 +112,7 @@ impl Widget for BootView<'_> { let heights = Heights::share( area.height .saturating_sub(HEADER_HEIGHT + stages_height + failures_height + FOOTER_HEIGHT), - u16::try_from(rows.len()).unwrap_or(u16::MAX), + u16::try_from(rows.len() + usize::from(folded > 0)).unwrap_or(u16::MAX), timeline_need, ); @@ -126,7 +130,7 @@ impl Widget for BootView<'_> { self.header().render(header, buf); Paragraph::new(stages).render(stage_area, buf); - self.tree(&rows, tree.height).render(tree, buf); + self.tree(&rows, folded, tree.height).render(tree, buf); Paragraph::new(failures).render(failed, buf); if heights.timeline > 0 { let bar_rows = usize::from(heights.timeline.saturating_sub(TIMELINE_CHROME_HEIGHT)); @@ -169,7 +173,9 @@ impl Heights { timeline: timeline_need, }; } - let timeline = timeline_need.min((free / 2).max(free.saturating_sub(tree_need))); + let timeline = timeline_need + .min(free.saturating_sub(tree_need).max(MIN_TIMELINE_HEIGHT)) + .min(free); Self { tree: free - timeline, timeline, @@ -342,14 +348,14 @@ impl BootView<'_> { } } - fn tree<'a>(&self, rows: &'a [PendingRow], height: u16) -> Table<'a> { + fn tree<'a>(&self, rows: &'a [PendingRow], folded: usize, height: u16) -> Table<'a> { let capacity = usize::from(height); - let shown = if rows.len() > capacity { - capacity.saturating_sub(1) + let shown = if rows.len() + usize::from(folded > 0) > capacity { + capacity.saturating_sub(1).min(rows.len()) } else { rows.len() }; - let hidden = rows.len() - shown; + let hidden = rows.len() - shown + folded; let overflow = (hidden > 0).then(|| { Row::new([Line::styled( format!("… {hidden} more"), @@ -829,8 +835,8 @@ mod tests { assert!(lines[3].starts_with("graphical.target"), "{screen}"); assert!(lines[4].starts_with("└─ multi-user.target"), "{screen}"); assert!(lines[5].starts_with(" ├─ [/] systemd-rfkill"), "{screen}"); - assert!(lines[6].starts_with(" ├─ [/] docker"), "{screen}"); - assert!(lines[7].starts_with(" └─ [ ] web"), "{screen}"); + assert!(lines[6].starts_with(" └─ [/] docker"), "{screen}"); + assert!(lines[7].starts_with("… 1 more"), "{screen}"); assert!( lines[8].starts_with("× nvidia-powerd.service failed"), "{screen}" @@ -966,6 +972,15 @@ mod tests { ); } + #[test] + fn a_long_tree_keeps_the_room_and_leaves_the_timeline_a_strip() { + let heights = Heights::share(30, 40, 50); + assert_eq!(heights.timeline, MIN_TIMELINE_HEIGHT); + assert_eq!(heights.tree, 30 - MIN_TIMELINE_HEIGHT); + let heights = Heights::share(30, 12, 50); + assert_eq!((heights.tree, heights.timeline), (12, 18)); + } + #[test] fn overflowing_trees_are_cut_with_a_count() { let mut state = BootState::new(Phase::Host);