syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Color adaptation for different terminal backgrounds
//!
//! This module provides color schemes that work well on both light and dark terminal backgrounds,
//! ensuring good readability regardless of the user's terminal theme.

use colored::*;
use std::env;

/// Represents the detected or configured terminal background type
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ColorScheme {
    /// Dark background terminal (default assumption)
    Dark,
    /// Light background terminal
    Light,
}

/// Color adapter that provides appropriate colors based on terminal background
#[derive(Debug, Clone)]
pub struct ColorAdapter {
    scheme: ColorScheme,
}

impl ColorAdapter {
    /// Create a new ColorAdapter with automatic background detection
    pub fn new() -> Self {
        Self {
            scheme: Self::detect_terminal_background(),
        }
    }

    /// Create a ColorAdapter with a specific color scheme
    pub fn with_scheme(scheme: ColorScheme) -> Self {
        Self { scheme }
    }

    /// Detect terminal background based on environment variables and heuristics
    fn detect_terminal_background() -> ColorScheme {
        // Check COLORFGBG environment variable (format: "foreground;background")
        if let Ok(colorfgbg) = env::var("COLORFGBG")
            && let Some(bg_str) = colorfgbg.split(';').nth(1)
            && let Ok(bg_code) = bg_str.parse::<u8>()
        {
            // Background colors 0-6 are dark, 7-15 are light/bright
            // Be more aggressive about detecting light backgrounds
            return if bg_code >= 7 {
                ColorScheme::Light
            } else {
                ColorScheme::Dark
            };
        }

        // Check for common light terminal setups
        if let Ok(term_program) = env::var("TERM_PROGRAM") {
            match term_program.as_str() {
                "Apple_Terminal" => {
                    // macOS Terminal.app - check for light theme indicators
                    // Many users have light themes, so be more aggressive
                    if let Ok(_term_session_id) = env::var("TERM_SESSION_ID") {
                        // If we can't detect definitively, assume light for Terminal.app
                        // since many users use the default light theme
                        return ColorScheme::Light;
                    }
                    return ColorScheme::Light; // Default to light for Terminal.app
                }
                "iTerm.app" => {
                    // iTerm2 - check for theme hints
                    if let Ok(_iterm_session_id) = env::var("ITERM_SESSION_ID") {
                        // Default to dark for iTerm as it's more commonly used with dark themes
                        return ColorScheme::Dark;
                    }
                }
                "vscode" | "code" => {
                    // VS Code integrated terminal - often follows editor theme
                    // VS Code light themes are common
                    return ColorScheme::Light;
                }
                _ => {}
            }
        }

        // Check terminal type and name hints
        if let Ok(term) = env::var("TERM") {
            match term.as_str() {
                term if term.contains("light") => return ColorScheme::Light,
                term if term.contains("256color") => {
                    // Modern terminals with 256 color support
                    // Check other indicators
                    if env::var("TERM_PROGRAM")
                        .unwrap_or_default()
                        .contains("Terminal")
                    {
                        return ColorScheme::Light; // macOS Terminal default
                    }
                }
                _ => {}
            }
        }

        // Check for SSH session - often indicates server/dark environment
        if env::var("SSH_CONNECTION").is_ok() || env::var("SSH_CLIENT").is_ok() {
            return ColorScheme::Dark;
        }

        // Check background color hints from other variables
        if let Ok(bg_hint) = env::var("BACKGROUND") {
            match bg_hint.to_lowercase().as_str() {
                "light" | "white" => return ColorScheme::Light,
                "dark" | "black" => return ColorScheme::Dark,
                _ => {}
            }
        }

        // More aggressive light detection for common desktop environments
        if let Ok(desktop) = env::var("XDG_CURRENT_DESKTOP") {
            match desktop.to_lowercase().as_str() {
                "gnome" | "kde" | "xfce" => {
                    // Desktop environments often use light themes by default
                    return ColorScheme::Light;
                }
                _ => {}
            }
        }

        // Check if we're in a GUI environment (more likely to be light)
        if env::var("DISPLAY").is_ok() || env::var("WAYLAND_DISPLAY").is_ok() {
            // GUI environment - light themes are common
            // But don't override if we have other strong indicators
            if env::var("TERM_PROGRAM").is_err() {
                return ColorScheme::Light;
            }
        }

        // Default fallback - if we can't detect, prefer dark (most terminals)
        // But add a bias toward light for macOS users
        #[cfg(target_os = "macos")]
        {
            ColorScheme::Light // macOS Terminal.app default is light
        }

        #[cfg(not(target_os = "macos"))]
        {
            ColorScheme::Dark // Most other platforms default to dark
        }
    }

    /// Get the current color scheme
    pub fn scheme(&self) -> ColorScheme {
        self.scheme
    }

    // Header and borders
    pub fn header_text(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.bright_white().bold(),
            ColorScheme::Light => text.black().bold(),
        }
    }

    pub fn border(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.bright_blue(),
            ColorScheme::Light => text.blue(),
        }
    }

    // Primary content colors
    pub fn primary(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.yellow(),
            ColorScheme::Light => text.red().bold(),
        }
    }

    pub fn secondary(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.green(),
            ColorScheme::Light => text.green().bold(),
        }
    }

    // Technology stack colors
    pub fn language(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.blue(),
            ColorScheme::Light => text.blue().bold(),
        }
    }

    pub fn framework(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.magenta(),
            ColorScheme::Light => text.magenta().bold(),
        }
    }

    pub fn database(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.cyan(),
            ColorScheme::Light => text.cyan().bold(),
        }
    }

    pub fn technology(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.magenta(),
            ColorScheme::Light => text.purple().bold(),
        }
    }

    // Status and metadata colors
    pub fn info(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.cyan(),
            ColorScheme::Light => text.blue().bold(),
        }
    }

    pub fn success(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.green(),
            ColorScheme::Light => text.green().bold(),
        }
    }

    pub fn warning(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.yellow(),
            ColorScheme::Light => text.red(),
        }
    }

    pub fn error(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.red(),
            ColorScheme::Light => text.red().bold(),
        }
    }

    // Label colors (for key-value pairs)
    pub fn label(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.bright_white(),
            ColorScheme::Light => text.black().bold(),
        }
    }

    pub fn value(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.white(),
            ColorScheme::Light => text.black(),
        }
    }

    // Dimmed/subtle text
    pub fn dimmed(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.dimmed(),
            ColorScheme::Light => text.dimmed(),
        }
    }

    // Architecture pattern colors
    pub fn architecture_pattern(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.green(),
            ColorScheme::Light => text.green().bold(),
        }
    }

    // Project type colors
    pub fn project_type(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.yellow(),
            ColorScheme::Light => text.red().bold(),
        }
    }

    // Metrics and numbers
    pub fn metric(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.cyan(),
            ColorScheme::Light => text.blue().bold(),
        }
    }

    // File paths and names
    pub fn path(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.cyan().bold(),
            ColorScheme::Light => text.blue().bold(),
        }
    }

    // Confidence indicators
    pub fn confidence_high(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.green(),
            ColorScheme::Light => text.green().bold(),
        }
    }

    pub fn confidence_medium(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.yellow(),
            ColorScheme::Light => text.red(),
        }
    }

    pub fn confidence_low(&self, text: &str) -> ColoredString {
        match self.scheme {
            ColorScheme::Dark => text.red(),
            ColorScheme::Light => text.red().bold(),
        }
    }
}

impl Default for ColorAdapter {
    fn default() -> Self {
        Self::new()
    }
}

/// Global color adapter instance
static COLOR_ADAPTER: std::sync::OnceLock<ColorAdapter> = std::sync::OnceLock::new();

/// Get the global color adapter instance
pub fn get_color_adapter() -> &'static ColorAdapter {
    COLOR_ADAPTER.get_or_init(ColorAdapter::new)
}

/// Initialize the global color adapter with a specific scheme
pub fn init_color_adapter(scheme: ColorScheme) {
    let _ = COLOR_ADAPTER.set(ColorAdapter::with_scheme(scheme));
}

/// Helper macro for quick color access
#[macro_export]
macro_rules! color {
    ($method:ident, $text:expr) => {
        $crate::analyzer::display::color_adapter::get_color_adapter().$method($text)
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_color_adapter_creation() {
        let adapter = ColorAdapter::new();
        assert!(matches!(
            adapter.scheme(),
            ColorScheme::Dark | ColorScheme::Light
        ));
    }

    #[test]
    #[ignore] // Flaky in CI - color codes stripped without terminal
    fn test_color_scheme_specific() {
        let dark_adapter = ColorAdapter::with_scheme(ColorScheme::Dark);
        let light_adapter = ColorAdapter::with_scheme(ColorScheme::Light);

        assert_eq!(dark_adapter.scheme(), ColorScheme::Dark);
        assert_eq!(light_adapter.scheme(), ColorScheme::Light);

        // Test that different schemes produce different outputs
        let test_text = "test";
        let dark_result = dark_adapter.header_text(test_text).to_string();
        let light_result = light_adapter.header_text(test_text).to_string();

        // Results should be different (due to different color codes)
        assert_ne!(dark_result, light_result);
    }

    #[test]
    fn test_color_methods() {
        let adapter = ColorAdapter::with_scheme(ColorScheme::Dark);
        let text = "test";

        // Ensure all color methods work without panicking
        let _ = adapter.header_text(text);
        let _ = adapter.border(text);
        let _ = adapter.primary(text);
        let _ = adapter.secondary(text);
        let _ = adapter.language(text);
        let _ = adapter.framework(text);
        let _ = adapter.database(text);
        let _ = adapter.technology(text);
        let _ = adapter.info(text);
        let _ = adapter.success(text);
        let _ = adapter.warning(text);
        let _ = adapter.error(text);
        let _ = adapter.label(text);
        let _ = adapter.value(text);
        let _ = adapter.dimmed(text);
        let _ = adapter.architecture_pattern(text);
        let _ = adapter.project_type(text);
        let _ = adapter.metric(text);
        let _ = adapter.path(text);
        let _ = adapter.confidence_high(text);
        let _ = adapter.confidence_medium(text);
        let _ = adapter.confidence_low(text);
    }
}