rch 2.1.3

Remote Compilation Helper - PreToolUse hook CLI
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! Color and formatting definitions for CLI output.
//!
//! Provides consistent styling across the application with support for
//! color terminals, plain text, and ASCII fallbacks.

use colored::{Color, ColoredString, Colorize};

/// Unicode symbols with ASCII fallbacks for terminal compatibility.
#[derive(Debug, Clone, Copy)]
pub struct Symbols {
    pub success: &'static str,
    pub failure: &'static str,
    pub warning: &'static str,
    pub info: &'static str,
    pub bullet_filled: &'static str,
    pub bullet_empty: &'static str,
    pub bullet_half: &'static str,
    pub disabled: &'static str,
    pub arrow_right: &'static str,
    pub spinner_frames: &'static [&'static str],
    // TUI-specific symbols for build/worker states
    pub syncing: &'static str,
    pub downloading: &'static str,
    pub compiling: &'static str,
    pub waiting: &'static str,
    pub draining: &'static str,
    pub circuit_half_open: &'static str,
    pub unknown: &'static str,
}

impl Symbols {
    /// Unicode symbols for terminals with full unicode support.
    pub const UNICODE: Self = Self {
        success: "✓",
        failure: "✗",
        warning: "⚠",
        info: "ℹ",
        bullet_filled: "●",
        bullet_empty: "○",
        bullet_half: "◐",
        disabled: "⊘",
        arrow_right: "→",
        spinner_frames: &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
        // TUI-specific symbols
        syncing: "↑",
        downloading: "↓",
        compiling: "⚙",
        waiting: "◷",
        draining: "◑",
        circuit_half_open: "↻",
        unknown: "?",
    };

    /// ASCII fallbacks for terminals without unicode support.
    pub const ASCII: Self = Self {
        success: "[OK]",
        failure: "[FAIL]",
        warning: "[WARN]",
        info: "[INFO]",
        bullet_filled: "[*]",
        bullet_empty: "[ ]",
        bullet_half: "[~]",
        disabled: "[-]",
        arrow_right: "->",
        spinner_frames: &["|", "/", "-", "\\"],
        // TUI-specific symbols (ASCII fallbacks)
        syncing: "[^]",
        downloading: "[v]",
        compiling: "[#]",
        waiting: "[.]",
        draining: "[>]",
        circuit_half_open: "[*]",
        unknown: "[?]",
    };

    /// Select symbols based on unicode support.
    pub fn for_unicode(supports_unicode: bool) -> Self {
        if supports_unicode {
            Self::UNICODE
        } else {
            Self::ASCII
        }
    }
}

/// Standardized status indicators for consistent visual feedback.
///
/// Each indicator has a defined symbol and color for immediate visual recognition.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatusIndicator {
    /// Operation succeeded, healthy state (✓ green)
    Success,
    /// Operation failed, error state (✗ red)
    Error,
    /// Degraded state, needs attention (⚠ yellow)
    Warning,
    /// Neutral information (● cyan)
    Info,
    /// Waiting, not started (○ gray)
    Pending,
    /// Currently running (◐ blue)
    InProgress,
    /// Intentionally disabled (⊘ gray)
    Disabled,
}

impl StatusIndicator {
    /// Get the symbol for this indicator.
    pub fn symbol(&self, symbols: &Symbols) -> &'static str {
        match self {
            StatusIndicator::Success => symbols.success,
            StatusIndicator::Error => symbols.failure,
            StatusIndicator::Warning => symbols.warning,
            StatusIndicator::Info => symbols.bullet_filled,
            StatusIndicator::Pending => symbols.bullet_empty,
            StatusIndicator::InProgress => symbols.bullet_half,
            StatusIndicator::Disabled => symbols.disabled,
        }
    }

    /// Get the default unicode symbol as a static str.
    ///
    /// This is useful for TUI rendering where we need &'static str directly.
    pub fn unicode_symbol(&self) -> &'static str {
        self.symbol(&Symbols::UNICODE)
    }

    /// Get the ASCII symbol as a static str.
    pub fn ascii_symbol(&self) -> &'static str {
        self.symbol(&Symbols::ASCII)
    }

    /// Get the color for this indicator.
    pub fn color(&self, colors: &SemanticColors) -> Color {
        match self {
            StatusIndicator::Success => colors.success,
            StatusIndicator::Error => colors.error,
            StatusIndicator::Warning => colors.warning,
            StatusIndicator::Info => colors.info,
            StatusIndicator::Pending => colors.muted,
            StatusIndicator::InProgress => colors.info,
            StatusIndicator::Disabled => colors.muted,
        }
    }

    /// Format the indicator with its symbol and optional label.
    pub fn display(&self, style: &Style) -> ColoredString {
        let symbol = self.symbol(&style.symbols);
        if style.colors_enabled {
            symbol.color(self.color(&style.colors))
        } else {
            symbol.normal()
        }
    }

    /// Format the indicator with a label.
    pub fn with_label(&self, style: &Style, label: &str) -> String {
        let symbol = self.display(style);
        let styled_label = if style.colors_enabled {
            label.color(self.color(&style.colors))
        } else {
            label.normal()
        };
        format!("{} {}", symbol, styled_label)
    }
}

/// Semantic colors for different message types.
#[derive(Debug, Clone, Copy)]
pub struct SemanticColors {
    pub success: Color,
    pub error: Color,
    pub warning: Color,
    pub info: Color,
    pub muted: Color,
    pub highlight: Color,
    pub key: Color,
    pub value: Color,
}

impl Default for SemanticColors {
    fn default() -> Self {
        Self {
            success: Color::Green,
            error: Color::Red,
            warning: Color::Yellow,
            info: Color::Cyan,
            muted: Color::BrightBlack,
            highlight: Color::White,
            key: Color::Blue,
            value: Color::White,
        }
    }
}

/// Style configuration combining colors and symbols.
#[derive(Debug, Clone)]
pub struct Style {
    pub colors: SemanticColors,
    pub symbols: Symbols,
    pub colors_enabled: bool,
    supports_unicode: bool,
    supports_hyperlinks: bool,
}

impl Style {
    /// Create a new style with colors and unicode support.
    pub fn new(colors_enabled: bool, supports_unicode: bool, supports_hyperlinks: bool) -> Self {
        Self {
            colors: SemanticColors::default(),
            symbols: Symbols::for_unicode(supports_unicode),
            colors_enabled,
            supports_unicode,
            supports_hyperlinks,
        }
    }

    /// Check if unicode symbols are supported.
    pub fn supports_unicode(&self) -> bool {
        self.supports_unicode
    }

    /// Check if hyperlinks are supported.
    pub fn supports_hyperlinks(&self) -> bool {
        self.supports_hyperlinks
    }

    /// Apply success styling to text.
    pub fn success(&self, text: &str) -> ColoredString {
        if self.colors_enabled {
            text.color(self.colors.success)
        } else {
            text.normal()
        }
    }

    /// Apply error styling to text.
    pub fn error(&self, text: &str) -> ColoredString {
        if self.colors_enabled {
            text.color(self.colors.error).bold()
        } else {
            text.normal()
        }
    }

    /// Apply warning styling to text.
    pub fn warning(&self, text: &str) -> ColoredString {
        if self.colors_enabled {
            text.color(self.colors.warning)
        } else {
            text.normal()
        }
    }

    /// Apply info styling to text.
    pub fn info(&self, text: &str) -> ColoredString {
        if self.colors_enabled {
            text.color(self.colors.info)
        } else {
            text.normal()
        }
    }

    /// Apply muted/dim styling to text.
    pub fn muted(&self, text: &str) -> ColoredString {
        if self.colors_enabled {
            text.color(self.colors.muted)
        } else {
            text.normal()
        }
    }

    /// Backwards-compatible alias for muted styling.
    pub fn dim(&self, text: &str) -> ColoredString {
        self.muted(text)
    }

    /// Apply highlight/bold styling to text.
    pub fn highlight(&self, text: &str) -> ColoredString {
        if self.colors_enabled {
            text.color(self.colors.highlight).bold()
        } else {
            text.normal()
        }
    }

    /// Apply key styling for key-value pairs.
    pub fn key(&self, text: &str) -> ColoredString {
        if self.colors_enabled {
            text.color(self.colors.key)
        } else {
            text.normal()
        }
    }

    /// Apply value styling for key-value pairs.
    pub fn value(&self, text: &str) -> ColoredString {
        if self.colors_enabled {
            text.color(self.colors.value)
        } else {
            text.normal()
        }
    }

    /// Format a success message with symbol and styling.
    pub fn format_success(&self, msg: &str) -> String {
        format!(
            "{} {}",
            self.success(self.symbols.success),
            self.success(msg)
        )
    }

    /// Format an error message with symbol and styling.
    pub fn format_error(&self, msg: &str) -> String {
        format!("{} {}", self.error(self.symbols.failure), self.error(msg))
    }

    /// Format a warning message with symbol and styling.
    pub fn format_warning(&self, msg: &str) -> String {
        format!(
            "{} {}",
            self.warning(self.symbols.warning),
            self.warning(msg)
        )
    }

    /// Format an info message with symbol and styling.
    pub fn format_info(&self, msg: &str) -> String {
        format!("{} {}", self.info(self.symbols.info), self.info(msg))
    }

    /// Format a key-value pair with proper alignment.
    pub fn format_key_value(&self, key: &str, value: &str, key_width: usize) -> String {
        format!(
            "{:width$} {} {}",
            self.key(key),
            self.muted(":"),
            self.value(value),
            width = key_width
        )
    }

    /// Format a section header.
    pub fn format_header(&self, title: &str) -> String {
        if self.colors_enabled {
            format!(
                "\n{}\n{}",
                self.highlight(title),
                self.muted(&"─".repeat(title.len()))
            )
        } else {
            format!("\n{}\n{}", title, "-".repeat(title.len()))
        }
    }

    /// Format a hyperlink (OSC 8).
    pub fn link(&self, text: &str, url: &str) -> String {
        if self.supports_hyperlinks {
            format!("\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\", url, text)
        } else if text == url {
            url.to_string()
        } else {
            format!("{} ({})", text, url)
        }
    }
}

/// Type alias for backwards compatibility with code expecting Theme.
/// The Style struct provides all theming functionality.
pub type Theme = Style;

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

    fn log_test_start(name: &str) {
        info!("TEST START: {}", name);
    }

    #[test]
    fn test_symbols_unicode() {
        log_test_start("test_symbols_unicode");
        let sym = Symbols::UNICODE;
        assert_eq!(sym.success, "✓");
        assert_eq!(sym.failure, "✗");
    }

    #[test]
    fn test_symbols_ascii() {
        log_test_start("test_symbols_ascii");
        let sym = Symbols::ASCII;
        assert_eq!(sym.success, "[OK]");
        assert_eq!(sym.failure, "[FAIL]");
    }

    #[test]
    fn test_symbols_selection() {
        log_test_start("test_symbols_selection");
        let unicode_sym = Symbols::for_unicode(true);
        assert_eq!(unicode_sym.success, "✓");

        let ascii_sym = Symbols::for_unicode(false);
        assert_eq!(ascii_sym.success, "[OK]");
    }

    #[test]
    fn test_style_with_colors_disabled() {
        log_test_start("test_style_with_colors_disabled");
        let style = Style::new(false, true, false);
        let styled = style.success("test");
        // When colors are disabled, the string should not contain escape codes
        let output = styled.to_string();
        assert!(!output.contains("\x1b["));
    }

    #[test]
    fn test_format_key_value() {
        log_test_start("test_format_key_value");
        let style = Style::new(false, true, false);
        let formatted = style.format_key_value("Status", "Healthy", 10);
        assert!(formatted.contains("Status"));
        assert!(formatted.contains("Healthy"));
        assert!(formatted.contains(":"));
    }

    #[test]
    fn test_format_header() {
        log_test_start("test_format_header");
        let style = Style::new(false, true, false);
        let header = style.format_header("Workers");
        assert!(header.contains("Workers"));
        assert!(header.contains("-------"));
    }

    #[test]
    fn test_status_indicator_symbols_unicode() {
        log_test_start("test_status_indicator_symbols_unicode");
        let symbols = Symbols::UNICODE;
        assert_eq!(StatusIndicator::Success.symbol(&symbols), "✓");
        assert_eq!(StatusIndicator::Error.symbol(&symbols), "✗");
        assert_eq!(StatusIndicator::Warning.symbol(&symbols), "⚠");
        assert_eq!(StatusIndicator::Info.symbol(&symbols), "●");
        assert_eq!(StatusIndicator::Pending.symbol(&symbols), "○");
        assert_eq!(StatusIndicator::InProgress.symbol(&symbols), "◐");
        assert_eq!(StatusIndicator::Disabled.symbol(&symbols), "⊘");
    }

    #[test]
    fn test_status_indicator_symbols_ascii() {
        log_test_start("test_status_indicator_symbols_ascii");
        let symbols = Symbols::ASCII;
        assert_eq!(StatusIndicator::Success.symbol(&symbols), "[OK]");
        assert_eq!(StatusIndicator::Error.symbol(&symbols), "[FAIL]");
        assert_eq!(StatusIndicator::Warning.symbol(&symbols), "[WARN]");
        assert_eq!(StatusIndicator::Info.symbol(&symbols), "[*]");
        assert_eq!(StatusIndicator::Pending.symbol(&symbols), "[ ]");
        assert_eq!(StatusIndicator::InProgress.symbol(&symbols), "[~]");
        assert_eq!(StatusIndicator::Disabled.symbol(&symbols), "[-]");
    }

    #[test]
    fn test_status_indicator_colors() {
        log_test_start("test_status_indicator_colors");
        let colors = SemanticColors::default();
        assert_eq!(StatusIndicator::Success.color(&colors), Color::Green);
        assert_eq!(StatusIndicator::Error.color(&colors), Color::Red);
        assert_eq!(StatusIndicator::Warning.color(&colors), Color::Yellow);
        assert_eq!(StatusIndicator::Info.color(&colors), Color::Cyan);
        assert_eq!(StatusIndicator::Pending.color(&colors), Color::BrightBlack);
        assert_eq!(StatusIndicator::InProgress.color(&colors), Color::Cyan);
        assert_eq!(StatusIndicator::Disabled.color(&colors), Color::BrightBlack);
    }

    #[test]
    fn test_status_indicator_display_no_color() {
        log_test_start("test_status_indicator_display_no_color");
        let style = Style::new(false, true, false);
        let display = StatusIndicator::Success.display(&style);
        let output = display.to_string();
        assert_eq!(output, "✓");
        assert!(!output.contains("\x1b[")); // No ANSI codes
    }

    #[test]
    fn test_status_indicator_with_label() {
        log_test_start("test_status_indicator_with_label");
        let style = Style::new(false, true, false);
        let output = StatusIndicator::Success.with_label(&style, "Running");
        assert!(output.contains("✓"));
        assert!(output.contains("Running"));
    }

    #[test]
    fn test_hyperlink_formatting() {
        log_test_start("test_hyperlink_formatting");
        let style = Style::new(false, true, true);
        let link = style.link("Click me", "https://example.com");
        assert_eq!(
            link,
            "\x1b]8;;https://example.com\x1b\\Click me\x1b]8;;\x1b\\"
        );
    }

    #[test]
    fn test_hyperlink_fallback() {
        log_test_start("test_hyperlink_fallback");
        let style = Style::new(false, true, false);
        let link = style.link("Click me", "https://example.com");
        assert_eq!(link, "Click me (https://example.com)");

        let link_same = style.link("https://example.com", "https://example.com");
        assert_eq!(link_same, "https://example.com");
    }

    #[test]
    fn test_disabled_symbol() {
        log_test_start("test_disabled_symbol");
        let unicode_sym = Symbols::UNICODE;
        assert_eq!(unicode_sym.disabled, "⊘");

        let ascii_sym = Symbols::ASCII;
        assert_eq!(ascii_sym.disabled, "[-]");
    }
}