1use clap::{Parser, ValueEnum};
9
10#[derive(Parser, Debug, Clone)]
11#[command(
12 name = "ai-usagebar",
13 about = "Waybar widget for AI plan usage (Anthropic / OpenAI / Z.AI / OpenRouter)",
14 long_about = "\
15Drop-in replacement for `claudebar` with multi-vendor support.
16
17Output modes:
18 - Default: Waybar JSON ({text, tooltip, class}). Used when stdout is piped.
19 - --pretty: human-readable terminal output for local testing. Auto-enabled
20 when stdout is a TTY, so just running `ai-usagebar --vendor anthropic`
21 in a terminal Does The Right Thing.
22 - --watch N: like --pretty but refreshes every N seconds, clearing the screen
23 between ticks. Useful while iterating on `--format` or `--tooltip-format`.
24 - --json: force JSON output even when stdout is a TTY (for scripting)."
25)]
26pub struct Cli {
27 #[arg(long, value_enum)]
31 pub vendor: Option<Vendor>,
32
33 #[arg(long)]
36 pub icon: Option<String>,
37
38 #[arg(long)]
41 pub format: Option<String>,
42
43 #[arg(long)]
46 pub tooltip_format: Option<String>,
47
48 #[arg(long, default_value_t = 5)]
50 pub pace_tolerance: u32,
51
52 #[arg(long)]
55 pub format_pace_color: bool,
56
57 #[arg(long)]
61 pub tooltip_pace_pts: bool,
62
63 #[arg(long)]
65 pub color_low: Option<String>,
66 #[arg(long)]
68 pub color_mid: Option<String>,
69 #[arg(long)]
71 pub color_high: Option<String>,
72 #[arg(long)]
74 pub color_critical: Option<String>,
75
76 #[arg(long)]
79 pub pretty: bool,
80
81 #[arg(long, conflicts_with = "pretty")]
84 pub json: bool,
85
86 #[arg(long, value_name = "SECS")]
89 pub watch: Option<u64>,
90
91 #[arg(long, conflicts_with_all = ["cycle_prev", "watch", "pretty", "json"])]
96 pub cycle_next: bool,
97
98 #[arg(long, conflicts_with_all = ["cycle_next", "watch", "pretty", "json"])]
100 pub cycle_prev: bool,
101
102 #[arg(long, hide = true)]
104 pub cache_dir: Option<std::path::PathBuf>,
105
106 #[arg(long, hide = true)]
108 pub creds_path: Option<std::path::PathBuf>,
109}
110
111#[derive(Debug, Clone, Copy, ValueEnum, PartialEq, Eq)]
112pub enum Vendor {
113 Anthropic,
114 Openai,
115 Zai,
116 Openrouter,
117 Deepseek,
118}
119
120impl Vendor {
121 pub fn to_id(self) -> crate::vendor::VendorId {
122 match self {
123 Vendor::Anthropic => crate::vendor::VendorId::Anthropic,
124 Vendor::Openai => crate::vendor::VendorId::Openai,
125 Vendor::Zai => crate::vendor::VendorId::Zai,
126 Vendor::Openrouter => crate::vendor::VendorId::Openrouter,
127 Vendor::Deepseek => crate::vendor::VendorId::Deepseek,
128 }
129 }
130}
131
132impl Cli {
133 pub fn resolved_vendor(&self, config: &crate::config::Config) -> Vendor {
139 if let Some(v) = self.vendor {
140 return v;
141 }
142 if let Some(id) = crate::active::read() {
143 if config.is_enabled(id) {
144 return id_to_vendor(id);
145 }
146 }
147 match config.ui.primary {
148 Some(id) => id_to_vendor(id),
149 None => Vendor::Anthropic,
150 }
151 }
152}
153
154fn id_to_vendor(id: crate::vendor::VendorId) -> Vendor {
155 match id {
156 crate::vendor::VendorId::Anthropic => Vendor::Anthropic,
157 crate::vendor::VendorId::Openai => Vendor::Openai,
158 crate::vendor::VendorId::Zai => Vendor::Zai,
159 crate::vendor::VendorId::Openrouter => Vendor::Openrouter,
160 crate::vendor::VendorId::Deepseek => Vendor::Deepseek,
161 }
162}
163
164impl Cli {
165 pub fn output_json(&self) -> bool {
168 if self.json {
169 return true;
170 }
171 if self.pretty || self.watch.is_some() {
172 return false;
173 }
174 !is_stdout_tty()
176 }
177}
178
179fn is_stdout_tty() -> bool {
180 use std::io::IsTerminal;
181 std::io::stdout().is_terminal()
182}
183
184#[cfg(test)]
185mod tests {
186 use super::*;
187 use clap::Parser;
188
189 #[test]
190 fn defaults_match_claudebar() {
191 let cli = Cli::parse_from(["ai-usagebar"]);
192 assert_eq!(cli.vendor, None);
193 let cfg = crate::config::Config::default();
195 assert_eq!(cli.resolved_vendor(&cfg), Vendor::Anthropic);
196 assert_eq!(cli.pace_tolerance, 5);
197 assert!(cli.format.is_none());
198 assert!(cli.tooltip_format.is_none());
199 assert!(cli.icon.is_none());
200 assert!(!cli.format_pace_color);
201 assert!(!cli.tooltip_pace_pts);
202 assert!(!cli.pretty);
203 assert!(!cli.json);
204 assert!(cli.watch.is_none());
205 }
206
207 #[test]
208 fn primary_from_config_wins_when_vendor_unset() {
209 let cli = Cli::parse_from(["ai-usagebar"]);
210 let mut cfg = crate::config::Config::default();
211 cfg.ui.primary = Some(crate::vendor::VendorId::Openrouter);
212 assert_eq!(cli.resolved_vendor(&cfg), Vendor::Openrouter);
213 }
214
215 #[test]
216 fn explicit_vendor_overrides_config_primary() {
217 let cli = Cli::parse_from(["ai-usagebar", "--vendor", "zai"]);
218 let mut cfg = crate::config::Config::default();
219 cfg.ui.primary = Some(crate::vendor::VendorId::Openrouter);
220 assert_eq!(cli.resolved_vendor(&cfg), Vendor::Zai);
221 }
222
223 #[test]
224 fn claudebar_compatible_flag_surface() {
225 let cli = Cli::parse_from([
226 "ai-usagebar",
227 "--icon",
228 "",
229 "--format",
230 "{session_pct}% · {session_reset}",
231 "--tooltip-format",
232 "S:{session_pct}",
233 "--pace-tolerance",
234 "10",
235 "--format-pace-color",
236 "--tooltip-pace-pts",
237 "--color-low",
238 "#50fa7b",
239 "--color-mid",
240 "#f1fa8c",
241 "--color-high",
242 "#ffb86c",
243 "--color-critical",
244 "#ff5555",
245 ]);
246 assert_eq!(cli.icon.as_deref(), Some(""));
247 assert_eq!(
248 cli.format.as_deref(),
249 Some("{session_pct}% · {session_reset}")
250 );
251 assert_eq!(cli.tooltip_format.as_deref(), Some("S:{session_pct}"));
252 assert_eq!(cli.pace_tolerance, 10);
253 assert!(cli.format_pace_color);
254 assert!(cli.tooltip_pace_pts);
255 assert_eq!(cli.color_low.as_deref(), Some("#50fa7b"));
256 assert_eq!(cli.color_critical.as_deref(), Some("#ff5555"));
257 }
258
259 #[test]
260 fn pretty_and_json_conflict() {
261 let res = Cli::try_parse_from(["ai-usagebar", "--pretty", "--json"]);
262 assert!(res.is_err());
263 }
264
265 #[test]
266 fn watch_disables_json_output() {
267 let cli = Cli::parse_from(["ai-usagebar", "--watch", "5"]);
268 assert_eq!(cli.watch, Some(5));
269 assert!(!cli.output_json());
270 }
271}