claude_code_status_line/
config.rs

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), // Color for the separator character
12    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), // Default to muted gray
25            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// Section-specific configurations
37
38#[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: true,
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: true,
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 autocompact_buffer_size: u64,
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            autocompact_buffer_size: 45_000,
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    pub cache_ttl: u64,
118}
119
120impl Default for QuotaConfig {
121    fn default() -> Self {
122        QuotaConfig {
123            enabled: true,
124            show_time_remaining: true,
125            cache_ttl: 0,
126        }
127    }
128}
129
130#[derive(Debug, Deserialize, Serialize)]
131#[serde(default)]
132pub struct CostConfig {
133    pub enabled: bool,
134    pub show_durations: bool,
135}
136
137impl Default for CostConfig {
138    fn default() -> Self {
139        CostConfig {
140            enabled: false,
141            show_durations: true,
142        }
143    }
144}
145
146#[derive(Debug, Default, Deserialize, Serialize)]
147#[serde(default)]
148pub struct SectionsConfig {
149    pub cwd: CwdConfig,
150    pub git: GitConfig,
151    pub model: ModelConfig,
152    pub context: ContextConfig,
153    pub quota: QuotaConfig,
154    pub cost: CostConfig,
155}
156
157#[derive(Debug, Deserialize, Serialize)]
158#[serde(default)]
159pub struct DisplayConfig {
160    pub multiline: bool,
161    pub default_terminal_width: usize,
162    pub use_powerline: bool,
163    pub arrow: String,
164    pub segment_separator: String,
165    pub details_separator: String,
166    pub section_padding: usize,
167    pub show_background: bool,
168}
169
170impl Default for DisplayConfig {
171    fn default() -> Self {
172        DisplayConfig {
173            multiline: true,
174            default_terminal_width: 120,
175            use_powerline: false,
176            arrow: "\u{E0B0}".to_string(),
177            segment_separator: "".to_string(),
178            details_separator: ", ".to_string(),
179            section_padding: 1,
180            show_background: true,
181        }
182    }
183}
184
185/// Configuration loaded from ~/.claude/statusline/settings.json
186#[derive(Debug, Default, Deserialize, Serialize)]
187#[serde(default)]
188pub struct Config {
189    pub sections: SectionsConfig,
190    pub display: DisplayConfig,
191    #[serde(skip)] // Don't load theme from settings.json
192    pub theme: ThemeConfig,
193}
194
195fn get_config_dir() -> Option<std::path::PathBuf> {
196    #[cfg(unix)]
197    let home = std::env::var_os("HOME")?;
198    #[cfg(windows)]
199    let home = std::env::var_os("USERPROFILE")?;
200
201    Some(
202        std::path::Path::new(&home)
203            .join(".claude")
204            .join("statusline"),
205    )
206}
207
208fn load_theme(dir: &Path) -> ThemeConfig {
209    let path = dir.join("colors.json");
210    if !path.exists() {
211        let theme = ThemeConfig::default();
212        if let Ok(json) = serde_json::to_string_pretty(&theme) {
213            let _ = fs::write(&path, json);
214        }
215        return theme;
216    }
217
218    match std::fs::read_to_string(&path) {
219        Ok(content) => serde_json::from_str(&content).unwrap_or_else(|e| {
220            eprintln!("statusline warning: invalid colors.json: {}", e);
221            ThemeConfig::default()
222        }),
223        Err(_) => ThemeConfig::default(),
224    }
225}
226
227pub fn load_config() -> Config {
228    let dir = match get_config_dir() {
229        Some(d) => d,
230        None => return Config::default(),
231    };
232
233    if !dir.exists() {
234        let _ = fs::create_dir_all(&dir);
235    }
236
237    let config_path = dir.join("settings.json");
238    if !config_path.exists() {
239        let config = Config::default();
240        if let Ok(json) = serde_json::to_string_pretty(&config) {
241            let _ = fs::write(&config_path, json);
242        }
243        // Also ensure theme is created
244        let mut final_config = config;
245        final_config.theme = load_theme(&dir);
246        return final_config;
247    }
248
249    let mut config = match std::fs::read_to_string(&config_path) {
250        Ok(content) => serde_json::from_str::<Config>(&content).unwrap_or_else(|e| {
251            eprintln!("statusline warning: invalid settings.json: {}", e);
252            Config::default()
253        }),
254        Err(_) => Config::default(),
255    };
256
257    // Load theme from separate file
258    config.theme = load_theme(&dir);
259    config
260}
261
262#[cfg(test)]
263mod tests {
264    use super::*;
265
266    #[test]
267    fn test_defaults() {
268        let config = Config::default();
269        assert_eq!(config.display.segment_separator, "");
270        assert_eq!(config.display.details_separator, ", ");
271        assert_eq!(config.display.section_padding, 1);
272        assert!(config.sections.cwd.enabled);
273        assert!(!config.sections.context.show_decimals);
274        assert!(config.sections.context.show_token_counts);
275        assert_eq!(config.theme.cwd.background, Some((217, 119, 87)));
276    }
277
278    #[test]
279    fn test_theme_deserialization() {
280        let json = r#"{
281            "separator": [255, 0, 0],
282            "cwd": { "background": null, "foreground": [20, 20, 20], "details": [30, 30, 30] },
283            "git": { "background": [40, 40, 40], "foreground": [50, 50, 50], "details": [60, 60, 60] },
284            "model": { "background": [70, 70, 70], "foreground": [80, 80, 80], "details": [90, 90, 90] },
285            "context": { "background": [100, 100, 100], "foreground": [110, 110, 110], "details": [120, 120, 120] },
286            "quota_5h": { "background": [130, 130, 130], "foreground": [140, 140, 140], "details": [150, 150, 150] },
287            "quota_7d": { "background": [160, 160, 160], "foreground": [170, 170, 170], "details": [180, 180, 180] },
288            "cost": { "background": [190, 190, 190], "foreground": [200, 200, 200], "details": [210, 210, 210] }
289        }"#;
290
291        let theme: ThemeConfig = serde_json::from_str(json).unwrap();
292        assert_eq!(theme.separator, (255, 0, 0));
293        assert_eq!(theme.cwd.background, None);
294        assert_eq!(theme.git.background, Some((40, 40, 40)));
295    }
296}