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
//! Color, emoji, and formatting utilities for terminal output

pub struct Colors;

impl Colors {
    pub const RESET: &'static str = "\x1b[0m";
    pub const BOLD: &'static str = "\x1b[1m";
    pub const DIM: &'static str = "\x1b[2m";

    // Basic colors
    pub const BLACK: &'static str = "\x1b[30m";
    pub const RED: &'static str = "\x1b[31m";
    pub const GREEN: &'static str = "\x1b[32m";
    pub const YELLOW: &'static str = "\x1b[33m";
    pub const BLUE: &'static str = "\x1b[34m";
    pub const MAGENTA: &'static str = "\x1b[35m";
    pub const CYAN: &'static str = "\x1b[36m";
    pub const WHITE: &'static str = "\x1b[37m";

    // Bright colors
    pub const BRIGHT_BLACK: &'static str = "\x1b[90m";
    pub const BRIGHT_RED: &'static str = "\x1b[91m";
    pub const BRIGHT_GREEN: &'static str = "\x1b[92m";
    pub const BRIGHT_YELLOW: &'static str = "\x1b[93m";
    pub const BRIGHT_BLUE: &'static str = "\x1b[94m";
    pub const BRIGHT_MAGENTA: &'static str = "\x1b[95m";
    pub const BRIGHT_CYAN: &'static str = "\x1b[96m";
    pub const BRIGHT_WHITE: &'static str = "\x1b[97m";
}

/// Apply color to text if terminal supports it
pub fn colorize(text: &str, color: &str) -> String {
    if supports_formatting() {
        format!("{}{}{}", color, text, Colors::RESET)
    } else {
        text.to_string()
    }
}

/// Enhanced terminal capability detection
pub fn supports_formatting() -> bool {
    use std::env;
    use std::io::IsTerminal;

    // Check if colors/emojis are explicitly disabled
    if env::var("NO_COLOR").is_ok() || env::var("FORCE_COLOR").as_deref() == Ok("0") {
        return false;
    }

    // Force enable if explicitly requested
    if env::var("FORCE_COLOR").is_ok() {
        return true;
    }

    // Disable formatting when running tests
    if cfg!(test) || env::var("RUST_TEST_TIME_UNIT").is_ok() {
        return false;
    }

    // Check if output is being redirected
    if !std::io::stdout().is_terminal() {
        return false;
    }

    // Check TERM environment variable
    if let Ok(term) = env::var("TERM") {
        if term == "dumb" || term.is_empty() {
            return false;
        }

        // Check for known capable terminals
        if term.contains("color")
            || term.contains("256")
            || term.starts_with("xterm")
            || term.starts_with("screen")
            || term.starts_with("tmux")
            || term == "linux"
        {
            return true;
        }
    }

    // Check for modern terminal programs
    if let Ok(term_program) = env::var("TERM_PROGRAM") {
        match term_program.as_str() {
            "Apple_Terminal" | "iTerm.app" | "vscode" | "Hyper" | "Alacritty" | "kitty"
            | "WezTerm" => return true,
            _ => {}
        }
    }

    // Check CI environments that support colors
    if env::var("CI").is_ok() {
        let ci_supports_color = [
            "GITHUB_ACTIONS",
            "TRAVIS",
            "CIRCLECI",
            "APPVEYOR",
            "GITLAB_CI",
            "AZURE_HTTP_USER_AGENT",
            "BUILDKITE",
        ]
        .iter()
        .any(|var| env::var(var).is_ok());

        if ci_supports_color {
            return true;
        }
    }

    // Default: assume no support if we can't detect
    false
}

/// Check if the current environment supports ANSI colors (alias for backwards compatibility)
pub fn supports_color() -> bool {
    supports_formatting()
}

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

    #[test]
    fn test_colorize_with_no_color() {
        unsafe {
            std::env::set_var("NO_COLOR", "1");
        }
        let result = colorize("test", Colors::RED);
        assert_eq!(result, "test");
        unsafe {
            std::env::remove_var("NO_COLOR");
        }
    }

    #[test]
    fn test_supports_formatting_with_no_color() {
        unsafe {
            std::env::set_var("NO_COLOR", "1");
        }
        assert!(!supports_formatting());
        unsafe {
            std::env::remove_var("NO_COLOR");
        }
    }

    #[test]
    fn test_supports_formatting_with_dumb_term() {
        unsafe {
            std::env::set_var("TERM", "dumb");
        }
        assert!(!supports_formatting());
        unsafe {
            std::env::remove_var("TERM");
        }
    }

    #[test]
    fn test_supports_color_alias() {
        // Test that supports_color() is equivalent to supports_formatting()
        assert_eq!(supports_color(), supports_formatting());
    }

    #[test]
    fn test_colorize_with_formatting_enabled() {
        // Save current environment
        let original_no_color = std::env::var("NO_COLOR").ok();
        let original_term = std::env::var("TERM").ok();

        unsafe {
            std::env::remove_var("NO_COLOR");
            std::env::set_var("TERM", "xterm-256color");
        }
        let result = colorize("test", Colors::RED);
        assert!(result.contains("test"));

        // Restore original environment
        unsafe {
            if let Some(val) = original_no_color {
                std::env::set_var("NO_COLOR", val);
            } else {
                std::env::remove_var("NO_COLOR");
            }
            if let Some(val) = original_term {
                std::env::set_var("TERM", val);
            } else {
                std::env::remove_var("TERM");
            }
        }
    }

    #[test]
    fn test_supports_formatting_with_empty_term() {
        // Save current environment
        let original_no_color = std::env::var("NO_COLOR").ok();
        let original_term = std::env::var("TERM").ok();
        let original_test_time = std::env::var("RUST_TEST_TIME_UNIT").ok();

        unsafe {
            std::env::remove_var("NO_COLOR");
            std::env::remove_var("RUST_TEST_TIME_UNIT");
            std::env::set_var("TERM", ""); // Empty TERM
        }

        assert!(!supports_formatting());

        // Restore original environment
        unsafe {
            if let Some(val) = original_no_color {
                std::env::set_var("NO_COLOR", val);
            }
            if let Some(val) = original_term {
                std::env::set_var("TERM", val);
            } else {
                std::env::remove_var("TERM");
            }
            if let Some(val) = original_test_time {
                std::env::set_var("RUST_TEST_TIME_UNIT", val);
            }
        }
    }

    #[test]
    fn test_supports_formatting_with_missing_term() {
        // Save current environment
        let original_no_color = std::env::var("NO_COLOR").ok();
        let original_term = std::env::var("TERM").ok();
        let original_test_time = std::env::var("RUST_TEST_TIME_UNIT").ok();

        unsafe {
            std::env::remove_var("NO_COLOR");
            std::env::remove_var("RUST_TEST_TIME_UNIT");
            std::env::remove_var("TERM"); // Missing TERM
        }

        assert!(!supports_formatting());

        // Restore original environment
        unsafe {
            if let Some(val) = original_no_color {
                std::env::set_var("NO_COLOR", val);
            }
            if let Some(val) = original_term {
                std::env::set_var("TERM", val);
            }
            if let Some(val) = original_test_time {
                std::env::set_var("RUST_TEST_TIME_UNIT", val);
            }
        }
    }

    #[test]
    fn test_supports_formatting_with_rust_test_time_unit() {
        // Save current environment
        let original_no_color = std::env::var("NO_COLOR").ok();
        let original_term = std::env::var("TERM").ok();
        let original_test_time = std::env::var("RUST_TEST_TIME_UNIT").ok();

        unsafe {
            std::env::remove_var("NO_COLOR");
            std::env::set_var("TERM", "xterm-256color");
            std::env::set_var("RUST_TEST_TIME_UNIT", "1"); // Test time unit set
        }

        assert!(!supports_formatting());

        // Restore original environment
        unsafe {
            if let Some(val) = original_no_color {
                std::env::set_var("NO_COLOR", val);
            }
            if let Some(val) = original_term {
                std::env::set_var("TERM", val);
            } else {
                std::env::remove_var("TERM");
            }
            if let Some(val) = original_test_time {
                std::env::set_var("RUST_TEST_TIME_UNIT", val);
            } else {
                std::env::remove_var("RUST_TEST_TIME_UNIT");
            }
        }
    }

    #[test]
    fn test_supports_formatting_with_valid_term() {
        // Save current environment
        let original_no_color = std::env::var("NO_COLOR").ok();
        let original_term = std::env::var("TERM").ok();
        let original_test_time = std::env::var("RUST_TEST_TIME_UNIT").ok();

        unsafe {
            std::env::remove_var("NO_COLOR");
            std::env::remove_var("RUST_TEST_TIME_UNIT");
            std::env::set_var("TERM", "xterm-256color"); // Valid TERM
        }

        // In test environment, cfg!(test) is true, so this should still return false
        assert!(!supports_formatting());

        // Restore original environment
        unsafe {
            if let Some(val) = original_no_color {
                std::env::set_var("NO_COLOR", val);
            }
            if let Some(val) = original_term {
                std::env::set_var("TERM", val);
            } else {
                std::env::remove_var("TERM");
            }
            if let Some(val) = original_test_time {
                std::env::set_var("RUST_TEST_TIME_UNIT", val);
            }
        }
    }

    #[test]
    fn test_all_color_constants() {
        // Test all color constants are accessible and have expected values
        assert_eq!(Colors::RESET, "\x1b[0m");
        assert_eq!(Colors::BOLD, "\x1b[1m");
        assert_eq!(Colors::DIM, "\x1b[2m");

        // Basic colors
        assert_eq!(Colors::RED, "\x1b[31m");
        assert_eq!(Colors::GREEN, "\x1b[32m");
        assert_eq!(Colors::YELLOW, "\x1b[33m");
        assert_eq!(Colors::BLUE, "\x1b[34m");
        assert_eq!(Colors::MAGENTA, "\x1b[35m");
        assert_eq!(Colors::CYAN, "\x1b[36m");
        assert_eq!(Colors::WHITE, "\x1b[37m");

        // Bright colors
        assert_eq!(Colors::BRIGHT_RED, "\x1b[91m");
        assert_eq!(Colors::BRIGHT_GREEN, "\x1b[92m");
        assert_eq!(Colors::BRIGHT_YELLOW, "\x1b[93m");
        assert_eq!(Colors::BRIGHT_BLUE, "\x1b[94m");
        assert_eq!(Colors::BRIGHT_MAGENTA, "\x1b[95m");
        assert_eq!(Colors::BRIGHT_CYAN, "\x1b[96m");
        assert_eq!(Colors::BRIGHT_WHITE, "\x1b[97m");
    }

    #[test]
    fn test_colorize_with_all_colors() {
        // Test colorize function with various colors
        unsafe {
            std::env::set_var("NO_COLOR", "1");
        }

        // When formatting is disabled, all should return plain text
        assert_eq!(colorize("test", Colors::RED), "test");
        assert_eq!(colorize("test", Colors::BRIGHT_GREEN), "test");
        assert_eq!(colorize("test", Colors::BLUE), "test");
        assert_eq!(colorize("test", Colors::YELLOW), "test");
        assert_eq!(colorize("test", Colors::MAGENTA), "test");
        assert_eq!(colorize("test", Colors::CYAN), "test");
        assert_eq!(colorize("test", Colors::WHITE), "test");
        assert_eq!(colorize("test", Colors::BRIGHT_RED), "test");
        assert_eq!(colorize("test", Colors::BRIGHT_YELLOW), "test");
        assert_eq!(colorize("test", Colors::BRIGHT_BLUE), "test");
        assert_eq!(colorize("test", Colors::BRIGHT_MAGENTA), "test");
        assert_eq!(colorize("test", Colors::BRIGHT_CYAN), "test");
        assert_eq!(colorize("test", Colors::BRIGHT_WHITE), "test");

        unsafe {
            std::env::remove_var("NO_COLOR");
        }
    }

    #[test]
    fn test_disable_formatting_when_running_tests() {
        unsafe {
            std::env::set_var("RUST_TEST_TIME_UNIT", "1");
        }

        assert!(!supports_formatting());

        unsafe {
            std::env::remove_var("RUST_TEST_TIME_UNIT");
        }
    }

    #[test]
    fn test_disable_formatting_when_dumb_terminal() {
        unsafe {
            std::env::set_var("TERM", "dumb");
        }

        assert!(!supports_formatting());

        unsafe {
            std::env::remove_var("TERM");
        }
    }

    #[test]
    fn test_colorize_edge_cases() {
        unsafe {
            std::env::set_var("NO_COLOR", "1");
        }

        // Test empty string
        assert_eq!(colorize("", Colors::RED), "");

        // Test string with special characters
        assert_eq!(
            colorize("test\nwith\ttabs", Colors::BLUE),
            "test\nwith\ttabs"
        );

        // Test unicode characters
        assert_eq!(
            colorize("test 🚀 unicode", Colors::GREEN),
            "test 🚀 unicode"
        );

        unsafe {
            std::env::remove_var("NO_COLOR");
        }
    }

    #[test]
    fn test_supports_formatting_with_term_variations() {
        let original_no_color = std::env::var("NO_COLOR").ok();
        let original_term = std::env::var("TERM").ok();
        let original_test_time = std::env::var("RUST_TEST_TIME_UNIT").ok();

        unsafe {
            std::env::remove_var("NO_COLOR");
            std::env::remove_var("RUST_TEST_TIME_UNIT");
        }

        // Test various TERM values that should support formatting
        let supporting_terms = ["xterm", "xterm-256color", "screen", "tmux", "linux"];
        for term in &supporting_terms {
            unsafe {
                std::env::set_var("TERM", term);
            }
            // Should still be false due to cfg!(test)
            assert!(!supports_formatting(), "Failed for TERM={}", term);
        }

        // Restore original environment
        unsafe {
            if let Some(val) = original_no_color {
                std::env::set_var("NO_COLOR", val);
            }
            if let Some(val) = original_term {
                std::env::set_var("TERM", val);
            } else {
                std::env::remove_var("TERM");
            }
            if let Some(val) = original_test_time {
                std::env::set_var("RUST_TEST_TIME_UNIT", val);
            }
        }
    }

    #[test]
    fn test_all_color_constants_coverage() {
        // This test ensures all color constants are included in test coverage
        // by accessing each one individually
        let constants = [
            Colors::RESET,
            Colors::BOLD,
            Colors::DIM,
            Colors::RED,
            Colors::GREEN,
            Colors::YELLOW,
            Colors::BLUE,
            Colors::MAGENTA,
            Colors::CYAN,
            Colors::WHITE,
            Colors::BRIGHT_RED,
            Colors::BRIGHT_GREEN,
            Colors::BRIGHT_YELLOW,
            Colors::BRIGHT_BLUE,
            Colors::BRIGHT_MAGENTA,
            Colors::BRIGHT_CYAN,
            Colors::BRIGHT_WHITE,
        ];

        // Verify each constant is accessible and not empty
        for constant in &constants {
            assert!(!constant.is_empty(), "Color constant should not be empty");
            assert!(
                constant.starts_with('\x1b'),
                "Color constant should start with escape sequence"
            );
        }

        // Test that each constant produces a unique string
        let mut unique_values = std::collections::HashSet::new();
        for constant in &constants {
            assert!(
                unique_values.insert(*constant),
                "Color constant should be unique: {}",
                constant
            );
        }

        // Verify we have the expected number of constants
        assert_eq!(constants.len(), 17, "Expected 17 color constants");
    }
}