Skip to main content

aperture_cli/
output.rs

1//! Output abstraction for quiet mode support.
2//!
3//! This module provides a centralized way to control CLI output based on
4//! quiet mode settings. It distinguishes between:
5//! - Informational messages (suppressed in quiet mode)
6//! - Success messages (suppressed in quiet mode)
7//! - Tips/hints (suppressed in quiet mode)
8//! - Data output (never suppressed)
9
10/// Output handler that respects quiet mode.
11///
12/// Quiet mode is enabled if either `--quiet` is passed or `--json-errors` is used.
13/// In quiet mode, only requested data and errors are output.
14#[derive(Debug, Clone)]
15pub struct Output {
16    quiet: bool,
17}
18
19impl Output {
20    /// Create new Output handler.
21    ///
22    /// Quiet mode is enabled if `--quiet` is passed OR `--json-errors` is passed.
23    #[must_use]
24    pub const fn new(quiet: bool, json_errors: bool) -> Self {
25        Self {
26            quiet: quiet || json_errors,
27        }
28    }
29
30    /// Print informational message (suppressed in quiet mode).
31    ///
32    /// Use for general status messages like "Registered API specifications:".
33    pub fn info(&self, msg: impl std::fmt::Display) {
34        if !self.quiet {
35            // Intentional CLI output, not debug logging
36            // ast-grep-ignore: no-println
37            println!("{msg}");
38        }
39    }
40
41    /// Print success message (suppressed in quiet mode).
42    ///
43    /// Use for confirmation messages like "Spec 'foo' added successfully".
44    pub fn success(&self, msg: impl std::fmt::Display) {
45        if !self.quiet {
46            // Intentional CLI output, not debug logging
47            // ast-grep-ignore: no-println
48            println!("{msg}");
49        }
50    }
51
52    /// Print tip or hint (suppressed in quiet mode).
53    ///
54    /// Use for helpful suggestions like usage tips after commands.
55    pub fn tip(&self, msg: impl std::fmt::Display) {
56        if !self.quiet {
57            // Intentional CLI output, not debug logging
58            // ast-grep-ignore: no-println
59            println!("{msg}");
60        }
61    }
62
63    /// Check if quiet mode is enabled.
64    #[must_use]
65    pub const fn is_quiet(&self) -> bool {
66        self.quiet
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn test_quiet_mode_from_quiet_flag() {
76        let output = Output::new(true, false);
77        assert!(output.is_quiet());
78    }
79
80    #[test]
81    fn test_quiet_mode_from_json_errors() {
82        let output = Output::new(false, true);
83        assert!(output.is_quiet());
84    }
85
86    #[test]
87    fn test_quiet_mode_both_flags() {
88        let output = Output::new(true, true);
89        assert!(output.is_quiet());
90    }
91
92    #[test]
93    fn test_not_quiet_when_no_flags() {
94        let output = Output::new(false, false);
95        assert!(!output.is_quiet());
96    }
97}