Skip to main content

slack_rs/cli/
context.rs

1//! CLI execution context with non-interactive mode support
2use std::io::IsTerminal;
3
4/// CLI execution context
5///
6/// Tracks global CLI state such as non-interactive mode
7#[derive(Debug, Clone)]
8pub struct CliContext {
9    /// Whether to run in non-interactive mode (no prompts)
10    pub non_interactive: bool,
11}
12
13impl CliContext {
14    /// Create a new CLI context with auto-detection of TTY
15    ///
16    /// # Arguments
17    /// * `explicit_non_interactive` - Explicit --non-interactive flag from CLI
18    ///
19    /// # Behavior
20    /// - If `explicit_non_interactive` is true, force non-interactive mode
21    /// - Otherwise, auto-detect based on stdin TTY status
22    /// - When stdin is not a TTY, automatically enable non-interactive mode
23    pub fn new(explicit_non_interactive: bool) -> Self {
24        let non_interactive = explicit_non_interactive || !std::io::stdin().is_terminal();
25        Self { non_interactive }
26    }
27
28    /// Check if running in non-interactive mode
29    pub fn is_non_interactive(&self) -> bool {
30        self.non_interactive
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn test_explicit_non_interactive() {
40        let ctx = CliContext::new(true);
41        assert!(ctx.is_non_interactive());
42    }
43
44    #[test]
45    fn test_context_clone() {
46        let ctx = CliContext::new(true);
47        let cloned = ctx.clone();
48        assert_eq!(ctx.non_interactive, cloned.non_interactive);
49    }
50}