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.
This commit is contained in:
1 parent
987eb1446b
commit
ecdc426c2c
2 files changed
+127
-47
No files matched your search
+60
-12
@@ -66,6 +66,11 @@ struct Shared {
|
|||||||
/// Set once the widget has been removed from the terminal; afterwards all
|
/// Set once the widget has been removed from the terminal; afterwards all
|
||||||
/// rendering is skipped so late events cannot redraw stale frames.
|
/// rendering is skipped so late events cannot redraw stale frames.
|
||||||
suspended: AtomicBool,
|
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,
|
started: Instant,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,10 +119,10 @@ impl DebUi {
|
|||||||
let timestamp = chrono::Utc::now().format("%Y%m%dT%H%M%S").to_string();
|
let timestamp = chrono::Utc::now().format("%Y%m%dT%H%M%S").to_string();
|
||||||
let log_path = default_log_path(×tamp);
|
let log_path = default_log_path(×tamp);
|
||||||
|
|
||||||
// Prompts release this widget from the terminal (a steady-tick
|
// Prompts release this widget from the terminal while the raw-mode
|
||||||
// redraw would clobber a raw-mode question), and after the release
|
// question is up (a steady-tick redraw would clobber it) and bring
|
||||||
// the view narrates in plain lines. Registered only for enabled
|
// it back with its current state once answered. Registered only for
|
||||||
// views: headless runs have no widget to park.
|
// enabled views: headless runs have no widget to park.
|
||||||
let shared = Arc::new(Shared {
|
let shared = Arc::new(Shared {
|
||||||
multi: multi.clone(),
|
multi: multi.clone(),
|
||||||
top,
|
top,
|
||||||
@@ -135,13 +140,20 @@ impl DebUi {
|
|||||||
timestamp,
|
timestamp,
|
||||||
enabled,
|
enabled,
|
||||||
suspended: AtomicBool::new(false),
|
suspended: AtomicBool::new(false),
|
||||||
|
parked: AtomicBool::new(false),
|
||||||
started: Instant::now(),
|
started: Instant::now(),
|
||||||
});
|
});
|
||||||
let park_generation = if enabled {
|
let park_generation = if enabled {
|
||||||
Some(crate::ui::prompt::set_park_hook(Arc::new({
|
Some(crate::ui::prompt::set_park_hook(
|
||||||
let shared = shared.clone();
|
{
|
||||||
move || suspend_shared(&shared)
|
let shared = shared.clone();
|
||||||
})))
|
Arc::new(move || park_shared(&shared))
|
||||||
|
},
|
||||||
|
{
|
||||||
|
let shared = shared.clone();
|
||||||
|
Arc::new(move || resume_shared(&shared))
|
||||||
|
},
|
||||||
|
))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
@@ -293,8 +305,8 @@ impl crate::report::BuildView for DebUi {
|
|||||||
if self.active() {
|
if self.active() {
|
||||||
self.shared.top.set_prefix(target.display.clone());
|
self.shared.top.set_prefix(target.display.clone());
|
||||||
} else if self.shared.enabled {
|
} else if self.shared.enabled {
|
||||||
// Released from the terminal (parked for a prompt, or torn
|
// Torn down for good: narrate plainly (a park for a prompt
|
||||||
// down): narrate plainly
|
// keeps the widget alive, its draws just go unseen)
|
||||||
eprintln!("{}", target.display);
|
eprintln!("{}", target.display);
|
||||||
}
|
}
|
||||||
if target.tee_log {
|
if target.tee_log {
|
||||||
@@ -321,8 +333,8 @@ impl crate::report::BuildView for DebUi {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if self.shared.suspended.load(Ordering::SeqCst) {
|
if self.shared.suspended.load(Ordering::SeqCst) {
|
||||||
// Released from the terminal (parked for a prompt, or torn
|
// Torn down for good: narrate plainly (a park for a prompt
|
||||||
// down): narrate plainly, the spinner no longer redraws
|
// keeps the widget alive, its draws just go unseen)
|
||||||
eprintln!("{text}");
|
eprintln!("{text}");
|
||||||
} else {
|
} else {
|
||||||
self.shared.top.set_message(text.to_string());
|
self.shared.top.set_message(text.to_string());
|
||||||
@@ -661,6 +673,40 @@ fn suspend_shared(shared: &Shared) {
|
|||||||
shared.multi.set_draw_target(ProgressDrawTarget::hidden());
|
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
|
/// Termios snapshot taken when the echo is suppressed; `Some` only while the
|
||||||
/// live view is on screen
|
/// live view is on screen
|
||||||
static SAVED_TTY_TERMIOS: Mutex<Option<libc::termios>> = Mutex::new(None);
|
static SAVED_TTY_TERMIOS: Mutex<Option<libc::termios>> = Mutex::new(None);
|
||||||
@@ -796,6 +842,7 @@ mod tests {
|
|||||||
timestamp: String::new(),
|
timestamp: String::new(),
|
||||||
enabled: true,
|
enabled: true,
|
||||||
suspended: AtomicBool::new(false),
|
suspended: AtomicBool::new(false),
|
||||||
|
parked: AtomicBool::new(false),
|
||||||
started: Instant::now(),
|
started: Instant::now(),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -839,6 +886,7 @@ mod tests {
|
|||||||
timestamp: String::new(),
|
timestamp: String::new(),
|
||||||
enabled: true,
|
enabled: true,
|
||||||
suspended: AtomicBool::new(false),
|
suspended: AtomicBool::new(false),
|
||||||
|
parked: AtomicBool::new(false),
|
||||||
started: Instant::now(),
|
started: Instant::now(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+67
-35
@@ -3,9 +3,10 @@
|
|||||||
//! confirmation.
|
//! confirmation.
|
||||||
//!
|
//!
|
||||||
//! Every public prompt enables raw mode itself and always restores it, and
|
//! Every public prompt enables raw mode itself and always restores it, and
|
||||||
//! first releases the live build widget from the terminal (see [`PARK`]):
|
//! releases the live build widget from the terminal for the duration of
|
||||||
//! a steady-tick redraw would clobber the raw-mode question. When no
|
//! the question (see [`PARK`]): a steady-tick redraw would clobber the
|
||||||
//! interactive terminal is attached, [`select`] fails with
|
//! 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`]
|
//! [`PromptError::NotATty`] so callers decide on a fallback, while [`text`]
|
||||||
//! and [`confirm`] answer with their default.
|
//! 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
|
/// 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
|
/// terminal before drawing its raw-mode line and brings it back afterwards.
|
||||||
/// steady-tick redraws fight the prompt for the cursor — the question
|
/// Without this, the widget's steady-tick redraws fight the prompt for the
|
||||||
/// never stays on screen, and the flow reads as a hang. Parking is the
|
/// cursor — the question never stays on screen, and the flow reads as a
|
||||||
/// widget's full teardown, so after the first prompt the view stays
|
/// hang.
|
||||||
/// 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<dyn Fn() + Send + Sync>);
|
|
||||||
|
|
||||||
static PARK: Mutex<Option<ParkHook>> = Mutex::new(None);
|
static PARK: Mutex<Option<ParkHook>> = 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<dyn Fn() + Send + Sync>,
|
||||||
|
resume: Arc<dyn Fn() + Send + Sync>,
|
||||||
|
}
|
||||||
|
|
||||||
/// Monotonic hook identity, so a view's drop never clears the
|
/// Monotonic hook identity, so a view's drop never clears the
|
||||||
/// registration of a view created after it (`pkh new` runs two builds in
|
/// registration of a view created after it (`pkh new` runs two builds in
|
||||||
/// a row, each with its own view)
|
/// a row, each with its own view)
|
||||||
static PARK_GENERATION: AtomicU64 = AtomicU64::new(0);
|
static PARK_GENERATION: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
/// Register the widget-parking hook, returning the generation token for
|
/// Register the widget park/resume hooks, returning the generation token
|
||||||
/// [`clear_park_hook`].
|
/// for [`clear_park_hook`].
|
||||||
pub fn set_park_hook(hook: Arc<dyn Fn() + Send + Sync>) -> u64 {
|
pub fn set_park_hook(
|
||||||
|
park: Arc<dyn Fn() + Send + Sync>,
|
||||||
|
resume: Arc<dyn Fn() + Send + Sync>,
|
||||||
|
) -> u64 {
|
||||||
let generation = PARK_GENERATION.fetch_add(1, Ordering::SeqCst);
|
let generation = PARK_GENERATION.fetch_add(1, Ordering::SeqCst);
|
||||||
*PARK.lock().unwrap() = Some((generation, hook));
|
*PARK.lock().unwrap() = Some(ParkHook {
|
||||||
|
generation,
|
||||||
|
park,
|
||||||
|
resume,
|
||||||
|
});
|
||||||
generation
|
generation
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,17 +220,24 @@ pub fn clear_park_hook(generation: u64) {
|
|||||||
let mut park = PARK.lock().unwrap();
|
let mut park = PARK.lock().unwrap();
|
||||||
if park
|
if park
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.is_some_and(|(registered, _)| *registered == generation)
|
.is_some_and(|hook| hook.generation == generation)
|
||||||
{
|
{
|
||||||
*park = None;
|
*park = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Release the live widget from the terminal, if one is registered.
|
/// 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() {
|
fn park_widget() {
|
||||||
if let Some((_, hook)) = PARK.lock().unwrap().as_ref() {
|
if let Some(hook) = PARK.lock().unwrap().as_ref() {
|
||||||
hook();
|
(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<T>(
|
|||||||
// Try to enter raw mode — if not possible (e.g. piped input), the caller
|
// Try to enter raw mode — if not possible (e.g. piped input), the caller
|
||||||
// falls back to a non-interactive answer
|
// falls back to a non-interactive answer
|
||||||
if terminal::enable_raw_mode().is_err() {
|
if terminal::enable_raw_mode().is_err() {
|
||||||
|
resume_widget();
|
||||||
return Err(Box::new(PromptError::NotATty));
|
return Err(Box::new(PromptError::NotATty));
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = prompt();
|
let result = prompt();
|
||||||
|
|
||||||
// Always restore the terminal
|
// Always restore the terminal, and hand it back to the widget
|
||||||
let _ = terminal::disable_raw_mode();
|
let _ = terminal::disable_raw_mode();
|
||||||
|
resume_widget();
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
@@ -550,39 +570,51 @@ mod tests {
|
|||||||
/// its own view.
|
/// its own view.
|
||||||
#[test]
|
#[test]
|
||||||
fn park_hook_registration_and_stale_clear() {
|
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 first = {
|
||||||
let calls = calls.clone();
|
let parks = parks.clone();
|
||||||
set_park_hook(Arc::new(move || {
|
let resumes = resumes.clone();
|
||||||
calls.fetch_add(1, Ordering::SeqCst);
|
set_park_hook(
|
||||||
}))
|
Arc::new(move || {
|
||||||
|
parks.fetch_add(1, Ordering::SeqCst);
|
||||||
|
}),
|
||||||
|
Arc::new(move || {
|
||||||
|
resumes.fetch_add(1, Ordering::SeqCst);
|
||||||
|
}),
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
park_widget();
|
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
|
// A second view registers; the first view's drop clears by its
|
||||||
// stale token and must not remove the newer hook
|
// 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 = {
|
||||||
let second_calls = second_calls.clone();
|
let second_parks = second_parks.clone();
|
||||||
set_park_hook(Arc::new(move || {
|
set_park_hook(
|
||||||
second_calls.fetch_add(1, Ordering::SeqCst);
|
Arc::new(move || {
|
||||||
}))
|
second_parks.fetch_add(1, Ordering::SeqCst);
|
||||||
|
}),
|
||||||
|
Arc::new(|| {}),
|
||||||
|
)
|
||||||
};
|
};
|
||||||
clear_park_hook(first);
|
clear_park_hook(first);
|
||||||
park_widget();
|
park_widget();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
calls.load(Ordering::SeqCst),
|
parks.load(Ordering::SeqCst),
|
||||||
1,
|
1,
|
||||||
"the stale clear left the newer hook"
|
"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);
|
clear_park_hook(second);
|
||||||
park_widget();
|
park_widget();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
second_calls.load(Ordering::SeqCst),
|
second_parks.load(Ordering::SeqCst),
|
||||||
1,
|
1,
|
||||||
"the current clear removed the hook"
|
"the current clear removed the hook"
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in new issue
Block a user