urlsup 2.4.0

CLI to validate URLs in files
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
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Advanced terminal theme detection and adaptive color schemes
//!
//! This module provides intelligent terminal background detection
//! and adaptive color schemes for optimal readability across
//! different terminal themes and environments.

use once_cell::sync::Lazy;
use std::env;
use std::fmt;
use std::io::IsTerminal;
use std::str::FromStr;

use crate::ui::color::Colors;

/// Terminal theme detection results
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TerminalTheme {
    Light,
    Dark,
    Unknown,
}

impl fmt::Display for TerminalTheme {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Light => write!(f, "light"),
            Self::Dark => write!(f, "dark"),
            Self::Unknown => write!(f, "unknown"),
        }
    }
}

impl FromStr for TerminalTheme {
    type Err = ThemeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "light" => Ok(Self::Light),
            "dark" => Ok(Self::Dark),
            "unknown" => Ok(Self::Unknown),
            _ => Err(ThemeError::InvalidTheme(s.to_string())),
        }
    }
}

impl Default for TerminalTheme {
    fn default() -> Self {
        Self::Dark
    }
}

/// Adaptive color scheme based on terminal background
#[derive(Debug, Clone)]
pub struct ColorScheme {
    pub theme: TerminalTheme,
    pub primary: &'static str,
    pub success: &'static str,
    pub warning: &'static str,
    pub error: &'static str,
    pub info: &'static str,
    pub muted: &'static str,
    pub accent: &'static str,
    pub url: &'static str,
}

impl ColorScheme {
    /// Get color for semantic color type
    pub fn get_color(&self, semantic_color: SemanticColor) -> &'static str {
        match semantic_color {
            SemanticColor::Primary => self.primary,
            SemanticColor::Success => self.success,
            SemanticColor::Warning => self.warning,
            SemanticColor::Error => self.error,
            SemanticColor::Info => self.info,
            SemanticColor::Muted => self.muted,
            SemanticColor::Accent => self.accent,
            SemanticColor::Url => self.url,
        }
    }

    /// Create color scheme for a specific theme
    const fn for_theme(theme: TerminalTheme) -> Self {
        match theme {
            TerminalTheme::Light => Self::LIGHT_SCHEME,
            TerminalTheme::Dark => Self::DARK_SCHEME,
            TerminalTheme::Unknown => Self::UNIVERSAL_SCHEME,
        }
    }

    const LIGHT_SCHEME: Self = Self {
        theme: TerminalTheme::Light,
        primary: Colors::BLACK,
        success: Colors::GREEN,
        warning: "\x1b[38;5;166m", // Orange for better light theme visibility
        error: Colors::RED,
        info: Colors::BLUE,
        muted: "\x1b[38;5;102m", // Medium gray
        accent: Colors::MAGENTA,
        url: Colors::BLUE,
    };

    const DARK_SCHEME: Self = Self {
        theme: TerminalTheme::Dark,
        primary: Colors::BRIGHT_WHITE,
        success: Colors::BRIGHT_GREEN,
        warning: Colors::BRIGHT_YELLOW,
        error: Colors::BRIGHT_RED,
        info: Colors::BRIGHT_CYAN,
        muted: Colors::DIM,
        accent: Colors::BRIGHT_MAGENTA,
        url: Colors::CYAN,
    };

    const UNIVERSAL_SCHEME: Self = Self {
        theme: TerminalTheme::Unknown,
        // Conservative colors that work on both light and dark
        primary: Colors::WHITE,
        success: Colors::GREEN,
        warning: Colors::YELLOW,
        error: Colors::RED,
        info: Colors::CYAN,
        muted: Colors::DIM,
        accent: Colors::MAGENTA,
        url: Colors::BLUE,
    };
}

/// Semantic color types for adaptive theming
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SemanticColor {
    Primary,
    Success,
    Warning,
    Error,
    Info,
    Muted,
    Accent,
    Url,
}

impl fmt::Display for SemanticColor {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Primary => write!(f, "primary"),
            Self::Success => write!(f, "success"),
            Self::Warning => write!(f, "warning"),
            Self::Error => write!(f, "error"),
            Self::Info => write!(f, "info"),
            Self::Muted => write!(f, "muted"),
            Self::Accent => write!(f, "accent"),
            Self::Url => write!(f, "url"),
        }
    }
}

/// Error types for theme operations
#[derive(Debug)]
pub enum ThemeError {
    InvalidTheme(String),
    DetectionFailed(String),
}

impl fmt::Display for ThemeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidTheme(theme) => write!(f, "Invalid theme: {}", theme),
            Self::DetectionFailed(reason) => write!(f, "Theme detection failed: {}", reason),
        }
    }
}

impl std::error::Error for ThemeError {}

/// Terminal theme detector with configurable fallback behavior
pub struct ThemeDetector {
    fallback_theme: TerminalTheme,
}

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

impl ThemeDetector {
    /// Create a new theme detector with default fallback
    pub const fn new() -> Self {
        Self {
            fallback_theme: TerminalTheme::Dark,
        }
    }

    /// Create theme detector with custom fallback
    pub const fn with_fallback(fallback: TerminalTheme) -> Self {
        Self {
            fallback_theme: fallback,
        }
    }

    /// Detect terminal theme with environment variable checks
    pub fn detect(&self) -> TerminalTheme {
        self.detect_from_env()
            .or_else(|| self.detect_from_terminal_program())
            .or_else(|| self.detect_from_context())
            .unwrap_or(self.fallback_theme)
    }

    /// Detect theme from explicit environment variables
    fn detect_from_env(&self) -> Option<TerminalTheme> {
        if let Ok(theme) = env::var("URLSUP_THEME") {
            theme.parse().ok()
        } else {
            None
        }
    }

    /// Detect theme from COLORFGBG environment variable
    fn detect_from_colorfgbg(&self) -> Option<TerminalTheme> {
        env::var("COLORFGBG").ok().and_then(|colorfgbg| {
            colorfgbg.split(';').nth(1).and_then(|bg| {
                bg.parse::<u8>().ok().map(|bg_num| {
                    if bg_num >= 8 {
                        TerminalTheme::Light
                    } else {
                        TerminalTheme::Dark
                    }
                })
            })
        })
    }

    /// Detect theme from terminal program hints
    fn detect_from_terminal_program(&self) -> Option<TerminalTheme> {
        env::var("TERM_PROGRAM").ok().and_then(|term_program| {
            match term_program.as_str() {
                "Apple_Terminal" => Some(TerminalTheme::Light),
                "vscode" => Some(TerminalTheme::Dark),
                "iTerm.app" => {
                    // Check if we can get more specific info
                    if env::var("ITERM_PROFILE").is_ok() {
                        Some(TerminalTheme::Unknown)
                    } else {
                        None
                    }
                }
                _ => None,
            }
        })
    }

    /// Detect theme from context (SSH, etc.)
    fn detect_from_context(&self) -> Option<TerminalTheme> {
        if env::var("SSH_CONNECTION").is_ok() || env::var("SSH_CLIENT").is_ok() {
            Some(TerminalTheme::Dark)
        } else {
            // Try COLORFGBG as last resort
            self.detect_from_colorfgbg()
        }
    }
}

/// Global color scheme instance
static COLOR_SCHEME: Lazy<ColorScheme> = Lazy::new(|| {
    let detector = ThemeDetector::new();
    let theme = detector.detect();
    ColorScheme::for_theme(theme)
});

/// Get the current color scheme
pub fn get_color_scheme() -> &'static ColorScheme {
    &COLOR_SCHEME
}

/// Apply adaptive color based on terminal theme
pub fn colorize_adaptive(text: &str, semantic_color: SemanticColor) -> String {
    use crate::ui::color::{colorize, supports_formatting};

    if !supports_formatting() {
        return text.to_string();
    }

    let scheme = get_color_scheme();
    let color = scheme.get_color(semantic_color);

    colorize(text, color)
}

/// Terminal capability detector for advanced features
pub struct TerminalCapabilityDetector;

impl TerminalCapabilityDetector {
    /// Check if terminal supports true color (24-bit)
    pub fn supports_true_color() -> bool {
        use crate::ui::color::supports_formatting;

        if !supports_formatting() {
            return false;
        }

        Self::check_explicit_support()
            || Self::check_term_capabilities()
            || Self::check_known_terminals()
    }

    /// Check explicit true color support environment variables
    fn check_explicit_support() -> bool {
        matches!(
            env::var("COLORTERM").as_deref(),
            Ok("truecolor") | Ok("24bit")
        )
    }

    /// Check TERM variable for true color indicators
    fn check_term_capabilities() -> bool {
        env::var("TERM")
            .map(|term| term.contains("truecolor") || term.contains("24bit"))
            .unwrap_or(false)
    }

    /// Check known terminal programs that support true color
    fn check_known_terminals() -> bool {
        const TRUE_COLOR_TERMINALS: &[&str] = &[
            "iTerm.app",
            "vscode",
            "Hyper",
            "Alacritty",
            "kitty",
            "WezTerm",
            "Apple_Terminal",
        ];

        env::var("TERM_PROGRAM")
            .map(|term_program| TRUE_COLOR_TERMINALS.contains(&term_program.as_str()))
            .unwrap_or(false)
    }
}

/// Compatibility wrapper for existing API
pub fn supports_true_color() -> bool {
    TerminalCapabilityDetector::supports_true_color()
}

/// Get terminal information for debugging
pub fn get_terminal_info() -> TerminalInfo {
    use crate::ui::color::supports_formatting;

    let detector = ThemeDetector::new();

    TerminalInfo {
        supports_color: supports_formatting(),
        supports_true_color: supports_true_color(),
        theme: detector.detect(),
        term_var: env::var("TERM").ok(),
        term_program: env::var("TERM_PROGRAM").ok(),
        colorterm: env::var("COLORTERM").ok(),
        colorfgbg: env::var("COLORFGBG").ok(),
        force_color: env::var("FORCE_COLOR").ok(),
        no_color: env::var("NO_COLOR").is_ok(),
        is_tty: std::io::stdout().is_terminal(),
    }
}

/// Terminal capability information
#[derive(Debug, Clone)]
pub struct TerminalInfo {
    pub supports_color: bool,
    pub supports_true_color: bool,
    pub theme: TerminalTheme,
    pub term_var: Option<String>,
    pub term_program: Option<String>,
    pub colorterm: Option<String>,
    pub colorfgbg: Option<String>,
    pub force_color: Option<String>,
    pub no_color: bool,
    pub is_tty: bool,
}

/// WCAG accessibility levels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AccessibilityLevel {
    AA,  // Minimum standard (4.5:1 contrast)
    AAA, // Enhanced standard (7:1 contrast)
}

impl AccessibilityLevel {
    /// Get the minimum contrast ratio for this level
    pub const fn contrast_ratio(self) -> f32 {
        match self {
            Self::AA => 4.5,
            Self::AAA => 7.0,
        }
    }
}

impl fmt::Display for AccessibilityLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::AA => write!(f, "AA (4.5:1)"),
            Self::AAA => write!(f, "AAA (7:1)"),
        }
    }
}

/// Accessibility checker for color combinations
pub struct AccessibilityChecker;

impl AccessibilityChecker {
    /// Get contrast ratio between two colors (simplified implementation)
    pub fn get_contrast_ratio(fg: &str, bg: &str) -> f32 {
        // Simplified contrast calculation for basic colors
        // In a real implementation, this would parse RGB values
        const HIGH_CONTRAST_PAIRS: &[(&str, &str, f32)] = &[
            (Colors::WHITE, Colors::BLACK, 21.0),
            (Colors::BRIGHT_WHITE, Colors::BLACK, 21.0),
            (Colors::BLACK, Colors::WHITE, 21.0),
            (Colors::BLACK, Colors::BRIGHT_WHITE, 21.0),
            (Colors::YELLOW, Colors::BLACK, 19.6),
            (Colors::BRIGHT_YELLOW, Colors::BLACK, 19.6),
            (Colors::CYAN, Colors::BLACK, 16.3),
            (Colors::BRIGHT_CYAN, Colors::BLACK, 16.3),
        ];

        HIGH_CONTRAST_PAIRS
            .iter()
            .find(|(f, b, _)| *f == fg && *b == bg)
            .map(|(_, _, ratio)| *ratio)
            .unwrap_or(7.0) // Assume decent contrast for other combinations
    }

    /// Check if color combination meets WCAG accessibility standards
    pub fn meets_standard(fg: &str, bg: &str, level: AccessibilityLevel) -> bool {
        let ratio = Self::get_contrast_ratio(fg, bg);
        ratio >= level.contrast_ratio()
    }

    /// Find the best color for a background that meets accessibility standards
    pub fn find_accessible_color(bg: &str, level: AccessibilityLevel) -> &'static str {
        const CANDIDATE_COLORS: &[&str] = &[
            Colors::WHITE,
            Colors::BRIGHT_WHITE,
            Colors::BLACK,
            Colors::YELLOW,
            Colors::BRIGHT_YELLOW,
            Colors::CYAN,
            Colors::BRIGHT_CYAN,
        ];

        CANDIDATE_COLORS
            .iter()
            .find(|&&fg| Self::meets_standard(fg, bg, level))
            .copied()
            .unwrap_or(Colors::WHITE) // Fallback to white
    }
}

/// Compatibility wrappers for existing API
pub fn get_contrast_ratio(fg: &str, bg: &str) -> f32 {
    AccessibilityChecker::get_contrast_ratio(fg, bg)
}

pub fn meets_accessibility_standard(fg: &str, bg: &str, level: AccessibilityLevel) -> bool {
    AccessibilityChecker::meets_standard(fg, bg, level)
}

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

    #[test]
    fn test_terminal_info_debug() {
        let info = get_terminal_info();
        println!("Terminal info: {:#?}", info);
        // Just ensure it doesn't panic
    }

    #[test]
    fn test_semantic_colors() {
        let scheme = get_color_scheme();
        println!("Color scheme: {:#?}", scheme);

        // Test semantic coloring
        let _success_text = colorize_adaptive("Success!", SemanticColor::Success);
        let _error_text = colorize_adaptive("Error!", SemanticColor::Error);

        // In test environment, should return plain text
        assert_eq!(colorize_adaptive("test", SemanticColor::Primary), "test");
    }

    #[test]
    fn test_theme_detection() {
        let detector = ThemeDetector::new();
        let theme = detector.detect();
        assert!(matches!(
            theme,
            TerminalTheme::Light | TerminalTheme::Dark | TerminalTheme::Unknown
        ));
    }

    #[test]
    fn test_theme_from_str() {
        assert_eq!(
            "light".parse::<TerminalTheme>().unwrap(),
            TerminalTheme::Light
        );
        assert_eq!(
            "dark".parse::<TerminalTheme>().unwrap(),
            TerminalTheme::Dark
        );
        assert_eq!(
            "LIGHT".parse::<TerminalTheme>().unwrap(),
            TerminalTheme::Light
        );
        assert!("invalid".parse::<TerminalTheme>().is_err());
    }

    #[test]
    fn test_theme_display() {
        assert_eq!(TerminalTheme::Light.to_string(), "light");
        assert_eq!(TerminalTheme::Dark.to_string(), "dark");
        assert_eq!(TerminalTheme::Unknown.to_string(), "unknown");
    }

    #[test]
    fn test_semantic_color_display() {
        assert_eq!(SemanticColor::Primary.to_string(), "primary");
        assert_eq!(SemanticColor::Success.to_string(), "success");
    }

    #[test]
    fn test_accessibility_standards() {
        assert!(AccessibilityChecker::meets_standard(
            Colors::WHITE,
            Colors::BLACK,
            AccessibilityLevel::AA
        ));
        assert!(AccessibilityChecker::meets_standard(
            Colors::WHITE,
            Colors::BLACK,
            AccessibilityLevel::AAA
        ));
        assert!(AccessibilityChecker::meets_standard(
            Colors::BLACK,
            Colors::WHITE,
            AccessibilityLevel::AA
        ));

        // Test compatibility wrappers
        assert!(meets_accessibility_standard(
            Colors::WHITE,
            Colors::BLACK,
            AccessibilityLevel::AA
        ));
    }

    #[test]
    fn test_accessibility_level_display() {
        assert_eq!(AccessibilityLevel::AA.to_string(), "AA (4.5:1)");
        assert_eq!(AccessibilityLevel::AAA.to_string(), "AAA (7:1)");
    }

    #[test]
    fn test_find_accessible_color() {
        let color =
            AccessibilityChecker::find_accessible_color(Colors::BLACK, AccessibilityLevel::AA);
        assert!(AccessibilityChecker::meets_standard(
            color,
            Colors::BLACK,
            AccessibilityLevel::AA
        ));
    }

    #[test]
    fn test_true_color_detection() {
        // Should not panic and return a boolean
        let supports = supports_true_color();
        // Test that the function returns without panicking

        // Test direct detector call
        let direct_supports = TerminalCapabilityDetector::supports_true_color();
        assert_eq!(supports, direct_supports);
    }

    #[test]
    fn test_color_scheme_consistency() {
        let scheme = get_color_scheme();

        // All colors should be non-empty
        assert!(!scheme.primary.is_empty());
        assert!(!scheme.success.is_empty());
        assert!(!scheme.warning.is_empty());
        assert!(!scheme.error.is_empty());
        assert!(!scheme.info.is_empty());
        assert!(!scheme.muted.is_empty());
        assert!(!scheme.accent.is_empty());
        assert!(!scheme.url.is_empty());

        // Colors should start with escape sequence
        assert!(scheme.primary.starts_with('\x1b'));
        assert!(scheme.success.starts_with('\x1b'));

        // Test get_color method
        assert_eq!(scheme.get_color(SemanticColor::Primary), scheme.primary);
        assert_eq!(scheme.get_color(SemanticColor::Success), scheme.success);
    }

    #[test]
    fn test_theme_detector_with_fallback() {
        let detector = ThemeDetector::with_fallback(TerminalTheme::Light);
        let theme = detector.detect();
        assert!(matches!(
            theme,
            TerminalTheme::Light | TerminalTheme::Dark | TerminalTheme::Unknown
        ));
    }

    #[test]
    fn test_color_scheme_for_theme() {
        let light_scheme = ColorScheme::for_theme(TerminalTheme::Light);
        assert_eq!(light_scheme.theme, TerminalTheme::Light);

        let dark_scheme = ColorScheme::for_theme(TerminalTheme::Dark);
        assert_eq!(dark_scheme.theme, TerminalTheme::Dark);
    }
}