From ecdc426c2c0bf9ba0eba146705a0773b5212008e Mon Sep 17 00:00:00 2001 From: Valentin Haudiquet Date: Tue, 17 Nov 2026 18:20:45 +0100 Subject: [PATCH] ui: bring the widget back after a prompt releases it The release was the widget's full teardown, so answering the upload question left the rest of the flow without its spinner. A prompt now parks instead of tearing down: the widget's lines are erased and its rendering suspended while the question owns the terminal, but the prefix, message, pane and captured lines keep updating under a hidden draw target, and the resume after the prompt repaints the widget with the state as of then. Only a real teardown (or a prompt ending after one, on the Ctrl+C path) skips the resume. --- src/ui/deb.rs | 72 +++++++++++++++++++++++++++------ src/ui/prompt.rs | 102 +++++++++++++++++++++++++++++++---------------- 2 files changed, 127 insertions(+), 47 deletions(-) diff --git a/src/ui/deb.rs b/src/ui/deb.rs index 85a0599..5bcfdd2 100644 --- a/src/ui/deb.rs +++ b/src/ui/deb.rs @@ -66,6 +66,11 @@ struct Shared { /// Set once the widget has been removed from the terminal; afterwards all /// rendering is skipped so late events cannot redraw stale frames. suspended: AtomicBool, + /// Set while a prompt holds the terminal: the widget's lines are erased + /// and drawing is suspended, but the state (prefix, message, pane, + /// captured lines) keeps updating, and [`resume_shared`] brings the + /// widget back with that state when the prompt is done. + parked: AtomicBool, started: Instant, } @@ -114,10 +119,10 @@ impl DebUi { let timestamp = chrono::Utc::now().format("%Y%m%dT%H%M%S").to_string(); let log_path = default_log_path(×tamp); - // Prompts release this widget from the terminal (a steady-tick - // redraw would clobber a raw-mode question), and after the release - // the view narrates in plain lines. Registered only for enabled - // views: headless runs have no widget to park. + // Prompts release this widget from the terminal while the raw-mode + // question is up (a steady-tick redraw would clobber it) and bring + // it back with its current state once answered. Registered only for + // enabled views: headless runs have no widget to park. let shared = Arc::new(Shared { multi: multi.clone(), top, @@ -135,13 +140,20 @@ impl DebUi { timestamp, enabled, suspended: AtomicBool::new(false), + parked: AtomicBool::new(false), started: Instant::now(), }); let park_generation = if enabled { - Some(crate::ui::prompt::set_park_hook(Arc::new({ - let shared = shared.clone(); - move || suspend_shared(&shared) - }))) + Some(crate::ui::prompt::set_park_hook( + { + let shared = shared.clone(); + Arc::new(move || park_shared(&shared)) + }, + { + let shared = shared.clone(); + Arc::new(move || resume_shared(&shared)) + }, + )) } else { None }; @@ -293,8 +305,8 @@ impl crate::report::BuildView for DebUi { if self.active() { self.shared.top.set_prefix(target.display.clone()); } else if self.shared.enabled { - // Released from the terminal (parked for a prompt, or torn - // down): narrate plainly + // Torn down for good: narrate plainly (a park for a prompt + // keeps the widget alive, its draws just go unseen) eprintln!("{}", target.display); } if target.tee_log { @@ -321,8 +333,8 @@ impl crate::report::BuildView for DebUi { return; } if self.shared.suspended.load(Ordering::SeqCst) { - // Released from the terminal (parked for a prompt, or torn - // down): narrate plainly, the spinner no longer redraws + // Torn down for good: narrate plainly (a park for a prompt + // keeps the widget alive, its draws just go unseen) eprintln!("{text}"); } else { self.shared.top.set_message(text.to_string()); @@ -661,6 +673,40 @@ fn suspend_shared(shared: &Shared) { shared.multi.set_draw_target(ProgressDrawTarget::hidden()); } +/// [`DebUi::park`] body, shared with the prompt hook +/// +/// Erase the widget from the terminal and stop its rendering (the prompt +/// now owns the screen) while keeping every state update flowing: the +/// prefix, the message, the pane and the captured lines all advance under +/// a hidden draw target, and [`resume_shared`] repaints the widget with +/// the state as of then. Reversible, unlike [`suspend_shared`]. +fn park_shared(shared: &Shared) { + if !shared.enabled { + return; + } + if shared.parked.swap(true, Ordering::SeqCst) { + return; + } + shared.top.disable_steady_tick(); + let _ = shared.multi.clear(); + shared.multi.set_draw_target(ProgressDrawTarget::hidden()); +} + +/// [`park_shared`]'s counterpart: bring the widget back on screen with the +/// state it accumulated while parked. No-op once the view is torn down for +/// good (a prompt ending after a teardown must not resurrect anything). +fn resume_shared(shared: &Shared) { + if shared.suspended.load(Ordering::SeqCst) { + return; + } + if !shared.parked.swap(false, Ordering::SeqCst) { + return; + } + shared.multi.set_draw_target(ProgressDrawTarget::stderr()); + shared.top.enable_steady_tick(Duration::from_millis(80)); + shared.top.force_draw(); +} + /// Termios snapshot taken when the echo is suppressed; `Some` only while the /// live view is on screen static SAVED_TTY_TERMIOS: Mutex> = Mutex::new(None); @@ -796,6 +842,7 @@ mod tests { timestamp: String::new(), enabled: true, suspended: AtomicBool::new(false), + parked: AtomicBool::new(false), started: Instant::now(), }; @@ -839,6 +886,7 @@ mod tests { timestamp: String::new(), enabled: true, suspended: AtomicBool::new(false), + parked: AtomicBool::new(false), started: Instant::now(), }; diff --git a/src/ui/prompt.rs b/src/ui/prompt.rs index 2209a22..354a9bb 100644 --- a/src/ui/prompt.rs +++ b/src/ui/prompt.rs @@ -3,9 +3,10 @@ //! confirmation. //! //! Every public prompt enables raw mode itself and always restores it, and -//! first releases the live build widget from the terminal (see [`PARK`]): -//! a steady-tick redraw would clobber the raw-mode question. When no -//! interactive terminal is attached, [`select`] fails with +//! releases the live build widget from the terminal for the duration of +//! the question (see [`PARK`]): a steady-tick redraw would clobber the +//! raw-mode line. The widget comes back as soon as the prompt is done. +//! When no interactive terminal is attached, [`select`] fails with //! [`PromptError::NotATty`] so callers decide on a fallback, while [`text`] //! and [`confirm`] answer with their default. @@ -180,27 +181,37 @@ impl crate::report::Prompter for TerminalPrompter { } /// The live widget's parking hook: any prompt releases the widget from the -/// terminal before drawing its raw-mode line. Without this, the widget's -/// steady-tick redraws fight the prompt for the cursor — the question -/// never stays on screen, and the flow reads as a hang. Parking is the -/// widget's full teardown, so after the first prompt the view stays -/// released and its later messages print as plain lines (see -/// [`super::deb`]). -/// A registered hook: its generation token plus the teardown closure. -type ParkHook = (u64, Arc); - +/// terminal before drawing its raw-mode line and brings it back afterwards. +/// Without this, the widget's steady-tick redraws fight the prompt for the +/// cursor — the question never stays on screen, and the flow reads as a +/// hang. static PARK: Mutex> = Mutex::new(None); +/// A registered hook: its generation token plus the release and restore +/// closures (the widget's park and resume). +struct ParkHook { + generation: u64, + park: Arc, + resume: Arc, +} + /// Monotonic hook identity, so a view's drop never clears the /// registration of a view created after it (`pkh new` runs two builds in /// a row, each with its own view) static PARK_GENERATION: AtomicU64 = AtomicU64::new(0); -/// Register the widget-parking hook, returning the generation token for -/// [`clear_park_hook`]. -pub fn set_park_hook(hook: Arc) -> u64 { +/// Register the widget park/resume hooks, returning the generation token +/// for [`clear_park_hook`]. +pub fn set_park_hook( + park: Arc, + resume: Arc, +) -> u64 { let generation = PARK_GENERATION.fetch_add(1, Ordering::SeqCst); - *PARK.lock().unwrap() = Some((generation, hook)); + *PARK.lock().unwrap() = Some(ParkHook { + generation, + park, + resume, + }); generation } @@ -209,17 +220,24 @@ pub fn clear_park_hook(generation: u64) { let mut park = PARK.lock().unwrap(); if park .as_ref() - .is_some_and(|(registered, _)| *registered == generation) + .is_some_and(|hook| hook.generation == generation) { *park = None; } } /// Release the live widget from the terminal, if one is registered. -/// Idempotent: the hook is the widget's teardown. +/// Idempotent: the hook is the widget's park. fn park_widget() { - if let Some((_, hook)) = PARK.lock().unwrap().as_ref() { - hook(); + if let Some(hook) = PARK.lock().unwrap().as_ref() { + (hook.park)(); + } +} + +/// Bring the live widget back, if one is registered. +fn resume_widget() { + if let Some(hook) = PARK.lock().unwrap().as_ref() { + (hook.resume)(); } } @@ -235,13 +253,15 @@ fn with_raw_mode( // Try to enter raw mode — if not possible (e.g. piped input), the caller // falls back to a non-interactive answer if terminal::enable_raw_mode().is_err() { + resume_widget(); return Err(Box::new(PromptError::NotATty)); } let result = prompt(); - // Always restore the terminal + // Always restore the terminal, and hand it back to the widget let _ = terminal::disable_raw_mode(); + resume_widget(); result } @@ -550,39 +570,51 @@ mod tests { /// its own view. #[test] fn park_hook_registration_and_stale_clear() { - let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let parks = Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let resumes = Arc::new(std::sync::atomic::AtomicUsize::new(0)); let first = { - let calls = calls.clone(); - set_park_hook(Arc::new(move || { - calls.fetch_add(1, Ordering::SeqCst); - })) + let parks = parks.clone(); + let resumes = resumes.clone(); + set_park_hook( + Arc::new(move || { + parks.fetch_add(1, Ordering::SeqCst); + }), + Arc::new(move || { + resumes.fetch_add(1, Ordering::SeqCst); + }), + ) }; park_widget(); - assert_eq!(calls.load(Ordering::SeqCst), 1, "the hook parked once"); + resume_widget(); + assert_eq!(parks.load(Ordering::SeqCst), 1, "the hook parked once"); + assert_eq!(resumes.load(Ordering::SeqCst), 1, "the hook resumed once"); // A second view registers; the first view's drop clears by its // stale token and must not remove the newer hook - let second_calls = Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let second_parks = Arc::new(std::sync::atomic::AtomicUsize::new(0)); let second = { - let second_calls = second_calls.clone(); - set_park_hook(Arc::new(move || { - second_calls.fetch_add(1, Ordering::SeqCst); - })) + let second_parks = second_parks.clone(); + set_park_hook( + Arc::new(move || { + second_parks.fetch_add(1, Ordering::SeqCst); + }), + Arc::new(|| {}), + ) }; clear_park_hook(first); park_widget(); assert_eq!( - calls.load(Ordering::SeqCst), + parks.load(Ordering::SeqCst), 1, "the stale clear left the newer hook" ); - assert_eq!(second_calls.load(Ordering::SeqCst), 1); + assert_eq!(second_parks.load(Ordering::SeqCst), 1); clear_park_hook(second); park_widget(); assert_eq!( - second_calls.load(Ordering::SeqCst), + second_parks.load(Ordering::SeqCst), 1, "the current clear removed the hook" );