Skip to main content

ctl_core/flags/
output.rs

1use clap::Args;
2
3use crate::color::ColorMode;
4use crate::flags::resolve_color;
5use crate::format::OutputFormat;
6
7/// Global pretty/JSON output, color, quiet. Flatten onto the root parser.
8///
9/// `-c/--color` matches forkctl. A consumer that already owns `-c`
10/// should flatten [`super::ColorLong`] plus [`FormatArgs`] instead.
11#[derive(Args, Clone, Debug, Default, Eq, PartialEq)]
12#[command(about = None, long_about = None)]
13pub struct OutputArgs {
14    /// Output representation.
15    #[arg(
16        short = 'f',
17        long,
18        global = true,
19        value_enum,
20        default_value = "pretty",
21        help_heading = "Output"
22    )]
23    pub format: OutputFormat,
24    /// Pretty-output color policy. JSON never contains ANSI.
25    #[arg(
26        short = 'c',
27        long,
28        global = true,
29        value_enum,
30        default_value = "auto",
31        help_heading = "Output"
32    )]
33    pub color: ColorMode,
34    /// Force colorless pretty output. Wins over `--color`.
35    #[arg(long, global = true, help_heading = "Output")]
36    pub no_color: bool,
37    /// Suppress successful pretty output.
38    #[arg(short = 'q', long, global = true, help_heading = "Output")]
39    pub quiet: bool,
40}
41
42impl OutputArgs {
43    /// Effective color after `--no-color`.
44    #[must_use]
45    pub fn color(&self) -> ColorMode {
46        resolve_color(self.color, self.no_color)
47    }
48
49    /// Build a [`View`](crate::view::View) from these flags.
50    #[cfg(feature = "view")]
51    #[must_use]
52    pub fn view(&self) -> crate::view::View {
53        crate::view::View::new(self.format, self.color()).quiet(self.quiet)
54    }
55}
56
57/// `-f/--format` and `-q/--quiet` without `-c`.
58#[derive(Args, Clone, Debug, Default, Eq, PartialEq)]
59#[command(about = None, long_about = None)]
60pub struct FormatArgs {
61    /// Output representation.
62    #[arg(
63        short = 'f',
64        long,
65        global = true,
66        value_enum,
67        default_value = "pretty",
68        help_heading = "Output"
69    )]
70    pub format: OutputFormat,
71    /// Suppress successful pretty output.
72    #[arg(short = 'q', long, global = true, help_heading = "Output")]
73    pub quiet: bool,
74}
75
76impl FormatArgs {
77    /// Build a [`View`](crate::view::View) with an explicit color policy.
78    #[cfg(feature = "view")]
79    #[must_use]
80    pub fn view(&self, color: ColorMode) -> crate::view::View {
81        crate::view::View::new(self.format, color).quiet(self.quiet)
82    }
83}
84
85/// `--format` and `-q`/`--quiet` without `-f`, for CLIs that already use
86/// `-f` for a domain flag such as `--file`.
87#[derive(Args, Clone, Debug, Default, Eq, PartialEq)]
88#[command(about = None, long_about = None)]
89pub struct FormatLong {
90    /// Output representation.
91    #[arg(
92        long,
93        global = true,
94        value_enum,
95        default_value = "pretty",
96        help_heading = "Output"
97    )]
98    pub format: OutputFormat,
99    /// Suppress successful pretty output.
100    #[arg(short = 'q', long, global = true, help_heading = "Output")]
101    pub quiet: bool,
102}
103
104impl FormatLong {
105    /// Build a [`View`](crate::view::View) with an explicit color policy.
106    #[cfg(feature = "view")]
107    #[must_use]
108    pub fn view(&self, color: ColorMode) -> crate::view::View {
109        crate::view::View::new(self.format, color).quiet(self.quiet)
110    }
111}