1use crate::colors::{
2 SectionColors, CONTEXT_COLORS, COST_COLORS, CWD_COLORS, GIT_COLORS, MODEL_COLORS,
3 QUOTA_5H_COLORS, QUOTA_7D_COLORS,
4};
5use serde::{Deserialize, Serialize};
6use std::fs;
7use std::path::Path;
8
9#[derive(Debug, Deserialize, Serialize)]
10pub struct ThemeConfig {
11 pub separator: (u8, u8, u8), pub cwd: SectionColors,
13 pub git: SectionColors,
14 pub model: SectionColors,
15 pub context: SectionColors,
16 pub quota_5h: SectionColors,
17 pub quota_7d: SectionColors,
18 pub cost: SectionColors,
19}
20
21impl Default for ThemeConfig {
22 fn default() -> Self {
23 ThemeConfig {
24 separator: (65, 65, 62), cwd: CWD_COLORS,
26 git: GIT_COLORS,
27 model: MODEL_COLORS,
28 context: CONTEXT_COLORS,
29 quota_5h: QUOTA_5H_COLORS,
30 quota_7d: QUOTA_7D_COLORS,
31 cost: COST_COLORS,
32 }
33 }
34}
35
36#[derive(Debug, Deserialize, Serialize)]
39#[serde(default)]
40pub struct CwdConfig {
41 pub enabled: bool,
42 pub full_path: bool,
43 pub show_username: bool,
44}
45
46impl Default for CwdConfig {
47 fn default() -> Self {
48 CwdConfig {
49 enabled: true,
50 full_path: true,
51 show_username: false,
52 }
53 }
54}
55
56#[derive(Debug, Deserialize, Serialize)]
57#[serde(default)]
58pub struct GitConfig {
59 pub enabled: bool,
60 pub show_repo_name: bool,
61 pub show_diff_stats: bool,
62}
63
64impl Default for GitConfig {
65 fn default() -> Self {
66 GitConfig {
67 enabled: true,
68 show_repo_name: false,
69 show_diff_stats: true,
70 }
71 }
72}
73
74#[derive(Debug, Deserialize, Serialize)]
75#[serde(default)]
76pub struct ModelConfig {
77 pub enabled: bool,
78 pub show_output_style: bool,
79 pub show_thinking_mode: bool,
80}
81
82impl Default for ModelConfig {
83 fn default() -> Self {
84 ModelConfig {
85 enabled: true,
86 show_output_style: false,
87 show_thinking_mode: true,
88 }
89 }
90}
91
92#[derive(Debug, Deserialize, Serialize)]
93#[serde(default)]
94pub struct ContextConfig {
95 pub enabled: bool,
96 pub show_decimals: bool,
97 pub show_token_counts: bool,
98 pub display_mode: String,
99}
100
101impl Default for ContextConfig {
102 fn default() -> Self {
103 ContextConfig {
104 enabled: true,
105 show_decimals: false,
106 show_token_counts: true,
107 display_mode: "used".to_string(),
108 }
109 }
110}
111
112#[derive(Debug, Deserialize, Serialize)]
113#[serde(default)]
114pub struct QuotaConfig {
115 pub enabled: bool,
116 pub show_time_remaining: bool,
117}
118
119impl Default for QuotaConfig {
120 fn default() -> Self {
121 QuotaConfig {
122 enabled: true,
123 show_time_remaining: true,
124 }
125 }
126}
127
128#[derive(Debug, Deserialize, Serialize)]
129#[serde(default)]
130pub struct CostConfig {
131 pub enabled: bool,
132 pub show_durations: bool,
133}
134
135impl Default for CostConfig {
136 fn default() -> Self {
137 CostConfig {
138 enabled: false,
139 show_durations: true,
140 }
141 }
142}
143
144#[derive(Debug, Default, Deserialize, Serialize)]
145#[serde(default)]
146pub struct SectionsConfig {
147 pub cwd: CwdConfig,
148 pub git: GitConfig,
149 pub model: ModelConfig,
150 pub context: ContextConfig,
151 pub quota: QuotaConfig,
152 pub cost: CostConfig,
153}
154
155pub const POWERLINE_ARROW: &str = "\u{E0B0}";
157
158#[derive(Debug, Deserialize, Serialize)]
159#[serde(default)]
160pub struct DisplayConfig {
161 pub multiline: bool,
162 pub default_terminal_width: usize,
163 pub use_powerline: bool,
164 #[serde(skip)] pub arrow: String,
166 pub segment_separator: String,
167 pub details_separator: String,
168 pub section_padding: usize,
169 pub show_background: bool,
170}
171
172impl Default for DisplayConfig {
173 fn default() -> Self {
174 DisplayConfig {
175 multiline: true,
176 default_terminal_width: 120,
177 use_powerline: false,
178 arrow: POWERLINE_ARROW.to_string(),
179 segment_separator: "".to_string(),
180 details_separator: ", ".to_string(),
181 section_padding: 1,
182 show_background: true,
183 }
184 }
185}
186
187#[derive(Debug, Default, Deserialize, Serialize)]
189#[serde(default)]
190pub struct Config {
191 pub sections: SectionsConfig,
192 pub display: DisplayConfig,
193 #[serde(skip)] pub theme: ThemeConfig,
195}
196
197fn get_config_dir() -> Option<std::path::PathBuf> {
198 Some(
199 crate::utils::get_home_dir()?
200 .join(".claude")
201 .join("statusline"),
202 )
203}
204
205fn load_theme(dir: &Path) -> ThemeConfig {
206 let path = dir.join("colors.json");
207 if !path.exists() {
208 let theme = ThemeConfig::default();
209 if let Ok(json) = serde_json::to_string_pretty(&theme) {
210 let _ = fs::write(&path, json);
211 }
212 return theme;
213 }
214
215 match std::fs::read_to_string(&path) {
216 Ok(content) => serde_json::from_str(&content).unwrap_or_else(|e| {
217 eprintln!("statusline warning: invalid colors.json: {}", e);
218 ThemeConfig::default()
219 }),
220 Err(_) => ThemeConfig::default(),
221 }
222}
223
224pub fn load_config() -> Config {
225 let dir = match get_config_dir() {
226 Some(d) => d,
227 None => return Config::default(),
228 };
229
230 if !dir.exists() {
231 let _ = fs::create_dir_all(&dir);
232 }
233
234 let config_path = dir.join("settings.json");
235 if !config_path.exists() {
236 let config = Config::default();
237 if let Ok(json) = serde_json::to_string_pretty(&config) {
238 let _ = fs::write(&config_path, json);
239 }
240 let mut final_config = config;
242 final_config.theme = load_theme(&dir);
243 return final_config;
244 }
245
246 let mut config = match std::fs::read_to_string(&config_path) {
247 Ok(content) => serde_json::from_str::<Config>(&content).unwrap_or_else(|e| {
248 eprintln!("statusline warning: invalid settings.json: {}", e);
249 Config::default()
250 }),
251 Err(_) => Config::default(),
252 };
253
254 config.theme = load_theme(&dir);
256 config
257}
258
259#[cfg(test)]
260mod tests {
261 use super::*;
262
263 #[test]
264 fn test_defaults() {
265 let config = Config::default();
266 assert_eq!(config.display.segment_separator, "");
267 assert_eq!(config.display.details_separator, ", ");
268 assert_eq!(config.display.section_padding, 1);
269 assert!(config.sections.cwd.enabled);
270 assert!(!config.sections.cwd.show_username);
271 assert!(!config.sections.git.show_repo_name);
272 assert!(!config.sections.context.show_decimals);
273 assert!(config.sections.context.show_token_counts);
274 assert_eq!(config.theme.cwd.background, Some((217, 119, 87)));
275 }
276
277 #[test]
278 fn test_theme_deserialization() {
279 let json = r#"{
280 "separator": [255, 0, 0],
281 "cwd": { "background": null, "foreground": [20, 20, 20], "details": [30, 30, 30] },
282 "git": { "background": [40, 40, 40], "foreground": [50, 50, 50], "details": [60, 60, 60] },
283 "model": { "background": [70, 70, 70], "foreground": [80, 80, 80], "details": [90, 90, 90] },
284 "context": { "background": [100, 100, 100], "foreground": [110, 110, 110], "details": [120, 120, 120] },
285 "quota_5h": { "background": [130, 130, 130], "foreground": [140, 140, 140], "details": [150, 150, 150] },
286 "quota_7d": { "background": [160, 160, 160], "foreground": [170, 170, 170], "details": [180, 180, 180] },
287 "cost": { "background": [190, 190, 190], "foreground": [200, 200, 200], "details": [210, 210, 210] }
288 }"#;
289
290 let theme: ThemeConfig = serde_json::from_str(json).unwrap();
291 assert_eq!(theme.separator, (255, 0, 0));
292 assert_eq!(theme.cwd.background, None);
293 assert_eq!(theme.git.background, Some((40, 40, 40)));
294 }
295
296 #[test]
297 fn test_root_settings_sample_deserializes() {
298 let sample = include_str!("../settings.json");
299 let config: Config = serde_json::from_str(sample).expect("root settings.json should parse");
300
301 assert_eq!(config.sections.context.display_mode, "used");
302 assert!(!config.sections.cwd.show_username);
303 }
304
305 #[test]
306 fn test_representative_config_snippet_deserializes() {
307 let sample = r#"
308 {
309 "sections": {
310 "cwd": { "enabled": true, "full_path": false, "show_username": true },
311 "git": { "enabled": true, "show_repo_name": true, "show_diff_stats": false },
312 "model": { "enabled": true, "show_output_style": true, "show_thinking_mode": true },
313 "context": { "enabled": true, "show_decimals": true, "show_token_counts": true, "display_mode": "remaining" },
314 "quota": { "enabled": true, "show_time_remaining": false, "cache_ttl": 300 },
315 "cost": { "enabled": true, "show_durations": false }
316 },
317 "display": {
318 "multiline": false,
319 "default_terminal_width": 140,
320 "use_powerline": false,
321 "segment_separator": " | ",
322 "details_separator": " | ",
323 "section_padding": 2,
324 "show_background": false
325 }
326 }
327 "#;
328
329 let config: Config =
330 serde_json::from_str(sample).expect("representative config should parse");
331
332 assert_eq!(config.sections.context.display_mode, "remaining");
333 assert!(!config.display.show_background);
334 }
335}