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)]
12pub struct OutputArgs {
13    /// Output representation.
14    #[arg(
15        short = 'f',
16        long,
17        global = true,
18        value_enum,
19        default_value = "pretty",
20        help_heading = "Output"
21    )]
22    pub format: OutputFormat,
23    /// Pretty-output color policy. JSON never contains ANSI.
24    #[arg(
25        short = 'c',
26        long,
27        global = true,
28        value_enum,
29        default_value = "auto",
30        help_heading = "Output"
31    )]
32    pub color: ColorMode,
33    /// Force colorless pretty output. Wins over `--color`.
34    #[arg(long, global = true, help_heading = "Output")]
35    pub no_color: bool,
36    /// Suppress successful pretty output.
37    #[arg(short = 'q', long, global = true, help_heading = "Output")]
38    pub quiet: bool,
39}
40
41impl OutputArgs {
42    /// Effective color after `--no-color`.
43    #[must_use]
44    pub fn color(&self) -> ColorMode {
45        resolve_color(self.color, self.no_color)
46    }
47
48    /// Build a [`View`](crate::view::View) from these flags.
49    #[cfg(feature = "view")]
50    #[must_use]
51    pub fn view(&self) -> crate::view::View {
52        crate::view::View::new(self.format, self.color()).quiet(self.quiet)
53    }
54}
55
56/// `-f/--format` and `-q/--quiet` without `-c`.
57#[derive(Args, Clone, Debug, Default, Eq, PartialEq)]
58pub struct FormatArgs {
59    /// Output representation.
60    #[arg(
61        short = 'f',
62        long,
63        global = true,
64        value_enum,
65        default_value = "pretty",
66        help_heading = "Output"
67    )]
68    pub format: OutputFormat,
69    /// Suppress successful pretty output.
70    #[arg(short = 'q', long, global = true, help_heading = "Output")]
71    pub quiet: bool,
72}
73
74impl FormatArgs {
75    /// Build a [`View`](crate::view::View) with an explicit color policy.
76    #[cfg(feature = "view")]
77    #[must_use]
78    pub fn view(&self, color: ColorMode) -> crate::view::View {
79        crate::view::View::new(self.format, color).quiet(self.quiet)
80    }
81}