Skip to main content

osp_cli/ui/
interactive.rs

1//! Interactive terminal helpers for prompts and transient status UI.
2//!
3//! This module exists to keep blocking prompts and live terminal widgets behind
4//! a small runtime-aware surface. Callers decide whether prompting is part of
5//! their workflow; this module only answers whether the current terminal makes
6//! that safe and provides the mechanics when it does.
7//!
8//! Contract:
9//!
10//! - this module owns prompt/runtime gating and spinner mechanics
11//! - it should not absorb command policy, validation rules, or higher-level
12//!   workflow decisions
13//!
14//! Public API shape:
15//!
16//! - [`InteractiveRuntime`] uses the crate-wide constructor/factory naming:
17//!   `new(...)` for exact runtime hints and `detect()` for process probing
18//! - [`Interactive`] is a lightweight wrapper over those runtime hints rather
19//!   than another place to encode workflow policy
20
21use dialoguer::{Confirm, Password, theme::ColorfulTheme};
22use indicatif::{ProgressBar, ProgressStyle};
23use std::env;
24use std::io::{self, IsTerminal};
25use std::time::Duration;
26
27/// Interactive runtime hints used to decide whether live terminal UI is safe.
28///
29/// This mirrors the render/runtime split elsewhere in `osp-ui`: callers can
30/// inject explicit values for tests or special hosts, while `detect()` remains
31/// the boring default for normal CLI entrypoints.
32#[derive(Debug, Clone, Default, PartialEq, Eq)]
33#[non_exhaustive]
34pub struct InteractiveRuntime {
35    /// Whether stdin is attached to a terminal.
36    pub stdin_is_tty: bool,
37    /// Whether stderr is attached to a terminal.
38    pub stderr_is_tty: bool,
39    /// Detected terminal identifier such as `xterm-256color`.
40    pub terminal: Option<String>,
41}
42
43impl InteractiveRuntime {
44    /// Creates explicit interactive runtime hints.
45    pub fn new(stdin_is_tty: bool, stderr_is_tty: bool, terminal: Option<String>) -> Self {
46        Self {
47            stdin_is_tty,
48            stderr_is_tty,
49            terminal,
50        }
51    }
52
53    /// Detect interactive terminal capabilities from the current process.
54    pub fn detect() -> Self {
55        Self::new(
56            io::stdin().is_terminal(),
57            io::stderr().is_terminal(),
58            env::var("TERM").ok(),
59        )
60    }
61
62    /// Returns `true` when both stdin and stderr can support interactive
63    /// prompts.
64    ///
65    /// # Examples
66    ///
67    /// ```
68    /// use osp_cli::ui::InteractiveRuntime;
69    ///
70    /// let runtime = InteractiveRuntime::new(
71    ///     true,
72    ///     true,
73    ///     Some("xterm-256color".to_string()),
74    /// );
75    ///
76    /// assert!(runtime.allows_prompting());
77    /// ```
78    pub fn allows_prompting(&self) -> bool {
79        self.stdin_is_tty && self.stderr_is_tty
80    }
81
82    /// Returns `true` when transient terminal output such as spinners is safe
83    /// to show.
84    ///
85    /// # Examples
86    ///
87    /// ```
88    /// use osp_cli::ui::InteractiveRuntime;
89    ///
90    /// let runtime = InteractiveRuntime::new(true, true, Some("dumb".to_string()));
91    ///
92    /// assert!(!runtime.allows_live_output());
93    /// ```
94    pub fn allows_live_output(&self) -> bool {
95        self.stderr_is_tty && !matches!(self.terminal.as_deref(), Some("dumb"))
96    }
97}
98
99/// Result type used by interactive prompt helpers.
100pub type InteractiveResult<T> = io::Result<T>;
101
102#[derive(Debug, Clone)]
103/// Interactive prompt helper bound to a detected or injected terminal runtime.
104pub struct Interactive {
105    runtime: InteractiveRuntime,
106}
107
108impl Default for Interactive {
109    fn default() -> Self {
110        Self::detect()
111    }
112}
113
114impl Interactive {
115    /// Create an interaction helper from the current process runtime.
116    pub fn detect() -> Self {
117        Self::new(InteractiveRuntime::detect())
118    }
119
120    /// Creates an interaction helper from an explicit runtime description.
121    ///
122    /// # Examples
123    ///
124    /// ```
125    /// use osp_cli::ui::{Interactive, InteractiveRuntime};
126    ///
127    /// let ui = Interactive::new(InteractiveRuntime::new(
128    ///     true,
129    ///     false,
130    ///     Some("xterm-256color".to_string()),
131    /// ));
132    ///
133    /// assert!(!ui.runtime().allows_prompting());
134    /// ```
135    pub fn new(runtime: InteractiveRuntime) -> Self {
136        Self { runtime }
137    }
138
139    /// Returns the runtime hints used by this helper.
140    pub fn runtime(&self) -> &InteractiveRuntime {
141        &self.runtime
142    }
143
144    /// Prompts for confirmation with a default answer of `false`.
145    pub fn confirm(&self, prompt: &str) -> InteractiveResult<bool> {
146        self.confirm_default(prompt, false)
147    }
148
149    /// Prompt for a yes/no answer without baking business policy into the UI.
150    pub fn confirm_default(&self, prompt: &str, default: bool) -> InteractiveResult<bool> {
151        self.require_prompting("confirmation prompt")?;
152        Confirm::with_theme(&ColorfulTheme::default())
153            .with_prompt(prompt)
154            .default(default)
155            .interact()
156            .map_err(io::Error::other)
157    }
158
159    /// Prompt for a secret value. Blank handling is a caller policy.
160    pub fn password(&self, prompt: &str) -> InteractiveResult<String> {
161        self.password_with_options(prompt, false)
162    }
163
164    /// Prompts for a secret value and permits empty input.
165    pub fn password_allow_empty(&self, prompt: &str) -> InteractiveResult<String> {
166        self.password_with_options(prompt, true)
167    }
168
169    /// Creates a spinner that follows the current runtime policy.
170    pub fn spinner(&self, message: impl Into<String>) -> Spinner {
171        Spinner::with_runtime(&self.runtime, message)
172    }
173
174    fn password_with_options(&self, prompt: &str, allow_empty: bool) -> InteractiveResult<String> {
175        self.require_prompting("password prompt")?;
176        Password::with_theme(&ColorfulTheme::default())
177            .with_prompt(prompt)
178            .allow_empty_password(allow_empty)
179            .interact()
180            .map_err(io::Error::other)
181    }
182
183    fn require_prompting(&self, kind: &str) -> InteractiveResult<()> {
184        if self.runtime.allows_prompting() {
185            return Ok(());
186        }
187        Err(io::Error::other(format!(
188            "{kind} requires an interactive terminal"
189        )))
190    }
191}
192
193/// Handle for a transient spinner shown on stderr.
194pub struct Spinner {
195    pb: ProgressBar,
196}
197
198impl Spinner {
199    /// Convenience constructor for normal CLI entrypoints.
200    ///
201    /// Hosts that already resolved runtime policy should prefer
202    /// `Spinner::with_runtime(...)`.
203    pub fn new(message: impl Into<String>) -> Self {
204        Self::with_runtime(&InteractiveRuntime::detect(), message)
205    }
206
207    /// Build a spinner that respects explicit runtime hints.
208    pub fn with_runtime(runtime: &InteractiveRuntime, message: impl Into<String>) -> Self {
209        Self::with_enabled(runtime.allows_live_output(), message)
210    }
211
212    /// Build either a live spinner or a hidden no-op handle.
213    ///
214    /// Hidden spinners let callers keep the same lifecycle code path in tests,
215    /// pipes, and dumb terminals without branching on every update.
216    pub fn with_enabled(enabled: bool, message: impl Into<String>) -> Self {
217        let pb = if enabled {
218            let pb = ProgressBar::new_spinner();
219            pb.enable_steady_tick(Duration::from_millis(120));
220            pb.set_style(
221                ProgressStyle::default_spinner()
222                    .tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"])
223                    .template("{spinner:.cyan} {msg}")
224                    .unwrap_or_else(|_| ProgressStyle::default_spinner()),
225            );
226            pb
227        } else {
228            ProgressBar::hidden()
229        };
230        pb.set_message(message.into());
231        Self { pb }
232    }
233
234    /// Updates the spinner message.
235    pub fn set_message(&self, message: impl Into<String>) {
236        self.pb.set_message(message.into());
237    }
238
239    /// Runs `f` while temporarily suspending the spinner from the terminal.
240    pub fn suspend<F, R>(&self, f: F) -> R
241    where
242        F: FnOnce() -> R,
243    {
244        self.pb.suspend(f)
245    }
246
247    /// Marks the spinner as successfully finished and leaves the final message visible.
248    pub fn finish_success(&self, message: impl Into<String>) {
249        self.pb.finish_with_message(message.into());
250    }
251
252    /// Marks the spinner as failed and leaves the final message visible.
253    pub fn finish_failure(&self, message: impl Into<String>) {
254        self.pb.abandon_with_message(message.into());
255    }
256
257    /// Backward-compatible success alias matching `indicatif` naming.
258    ///
259    /// New callers should prefer [`Spinner::finish_success`] so the public API
260    /// makes the success/failure distinction explicit.
261    pub fn finish_with_message(&self, message: impl Into<String>) {
262        self.finish_success(message);
263    }
264
265    /// Finishes the spinner and clears it from the terminal.
266    pub fn finish_and_clear(&self) {
267        self.pb.finish_and_clear();
268    }
269}
270
271#[cfg(test)]
272mod tests {
273    use super::{Interactive, InteractiveRuntime, Spinner};
274
275    fn runtime(
276        stdin_is_tty: bool,
277        stderr_is_tty: bool,
278        terminal: Option<&str>,
279    ) -> InteractiveRuntime {
280        InteractiveRuntime::new(stdin_is_tty, stderr_is_tty, terminal.map(str::to_string))
281    }
282
283    #[test]
284    fn runtime_capability_matrix_covers_prompting_and_live_output_unit() {
285        let cases = [
286            (runtime(true, true, Some("xterm-256color")), true, true),
287            (runtime(true, true, Some("dumb")), true, false),
288            (runtime(false, true, Some("xterm-256color")), false, true),
289            (runtime(true, false, Some("xterm-256color")), false, false),
290            (runtime(true, true, None), true, true),
291        ];
292
293        for (runtime, allows_prompting, allows_live_output) in cases {
294            assert_eq!(runtime.allows_prompting(), allows_prompting);
295            assert_eq!(runtime.allows_live_output(), allows_live_output);
296        }
297    }
298
299    #[test]
300    fn hidden_spinner_supports_full_lifecycle() {
301        let spinner = Spinner::with_enabled(false, "Working");
302        spinner.set_message("Still working");
303        spinner.suspend(|| ());
304        spinner.finish_success("Done");
305        spinner.finish_failure("Failed");
306        spinner.finish_and_clear();
307    }
308
309    #[test]
310    fn spinner_respects_runtime_policy_and_finish_alias() {
311        let live_runtime = runtime(true, true, Some("xterm-256color"));
312        let muted_runtime = runtime(true, true, Some("dumb"));
313
314        let live = Spinner::with_runtime(&live_runtime, "Working");
315        live.set_message("Still working");
316        live.finish_with_message("Done");
317
318        let muted = Spinner::with_runtime(&muted_runtime, "Muted");
319        muted.finish_with_message("Still muted");
320    }
321
322    #[test]
323    fn interactive_runtime_accessor_and_spinner_follow_runtime() {
324        let runtime = runtime(true, true, Some("xterm-256color"));
325        let interactive = Interactive::new(runtime.clone());
326
327        assert_eq!(interactive.runtime(), &runtime);
328        interactive.spinner("Working").finish_and_clear();
329    }
330
331    #[test]
332    fn prompting_helpers_fail_fast_without_interactive_terminal_unit() {
333        let interactive = Interactive::new(runtime(false, false, None));
334
335        for err in [
336            interactive
337                .confirm("Proceed?")
338                .expect_err("confirm should fail"),
339            interactive
340                .password("Password")
341                .expect_err("password should fail"),
342            interactive
343                .password_allow_empty("Password")
344                .expect_err("password prompt should still require a TTY"),
345        ] {
346            assert!(
347                err.to_string().contains("interactive terminal"),
348                "unexpected error: {err}"
349            );
350        }
351    }
352
353    #[test]
354    fn spinner_new_and_detect_paths_are_callable() {
355        let interactive = Interactive::detect();
356        interactive.spinner("Working").finish_and_clear();
357        Spinner::new("Booting").finish_and_clear();
358    }
359
360    #[test]
361    fn default_interactive_matches_detected_runtime_shape() {
362        let detected = Interactive::detect();
363        let defaulted = Interactive::default();
364
365        assert_eq!(
366            defaulted.runtime().stdin_is_tty,
367            detected.runtime().stdin_is_tty
368        );
369        assert_eq!(
370            defaulted.runtime().stderr_is_tty,
371            detected.runtime().stderr_is_tty
372        );
373    }
374}