|
| 1 | +//! ANSI escape code handling utilities. |
| 2 | +//! |
| 3 | +//! Provides functions to detect terminal capabilities and strip ANSI codes |
| 4 | +//! when output is piped or redirected (#2811). |
| 5 | +
|
| 6 | +use std::io::IsTerminal; |
| 7 | + |
| 8 | +/// Check if stdout should output colors/ANSI codes. |
| 9 | +/// |
| 10 | +/// Returns false when: |
| 11 | +/// - stdout is not a terminal (piped/redirected) |
| 12 | +/// - NO_COLOR environment variable is set |
| 13 | +/// |
| 14 | +/// This implements the NO_COLOR standard: https://no-color.org/ |
| 15 | +pub fn should_colorize() -> bool { |
| 16 | + // Check NO_COLOR environment variable first (https://no-color.org/) |
| 17 | + if std::env::var("NO_COLOR").is_ok() { |
| 18 | + return false; |
| 19 | + } |
| 20 | + |
| 21 | + // Check if stdout is a terminal |
| 22 | + std::io::stdout().is_terminal() |
| 23 | +} |
| 24 | + |
| 25 | +/// Check if stderr should output colors/ANSI codes. |
| 26 | +pub fn should_colorize_stderr() -> bool { |
| 27 | + // Check NO_COLOR environment variable first |
| 28 | + if std::env::var("NO_COLOR").is_ok() { |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + // Check if stderr is a terminal |
| 33 | + std::io::stderr().is_terminal() |
| 34 | +} |
| 35 | + |
| 36 | +/// Strip ANSI escape codes from a string. |
| 37 | +/// |
| 38 | +/// This removes all ANSI escape sequences including: |
| 39 | +/// - Color codes (\x1b[...m) |
| 40 | +/// - Cursor movement (\x1b[...H, \x1b[...A, etc.) |
| 41 | +/// - Screen clearing (\x1b[2J, etc.) |
| 42 | +/// - Other CSI sequences |
| 43 | +pub fn strip_ansi_codes(s: &str) -> String { |
| 44 | + let mut result = String::with_capacity(s.len()); |
| 45 | + let mut chars = s.chars().peekable(); |
| 46 | + |
| 47 | + while let Some(c) = chars.next() { |
| 48 | + if c == '\x1b' { |
| 49 | + // Skip the escape sequence |
| 50 | + if let Some(&next) = chars.peek() { |
| 51 | + if next == '[' { |
| 52 | + chars.next(); // consume '[' |
| 53 | + // Skip until we hit a letter (the terminator) |
| 54 | + while let Some(&c) = chars.peek() { |
| 55 | + chars.next(); |
| 56 | + if c.is_ascii_alphabetic() { |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + continue; |
| 61 | + } else if next == ']' { |
| 62 | + // OSC sequence (e.g., terminal title) |
| 63 | + chars.next(); // consume ']' |
| 64 | + // Skip until BEL (\x07) or ST (\x1b\\) |
| 65 | + while let Some(c) = chars.next() { |
| 66 | + if c == '\x07' { |
| 67 | + break; |
| 68 | + } |
| 69 | + if c == '\x1b' { |
| 70 | + if chars.peek() == Some(&'\\') { |
| 71 | + chars.next(); |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + continue; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + result.push(c); |
| 81 | + } |
| 82 | + |
| 83 | + result |
| 84 | +} |
| 85 | + |
| 86 | +/// Conditionally wrap a string with ANSI color codes. |
| 87 | +/// |
| 88 | +/// Returns the colored string if colors are enabled, otherwise returns the plain string. |
| 89 | +pub fn maybe_color(text: &str, color_code: &str, reset: &str) -> String { |
| 90 | + if should_colorize() { |
| 91 | + format!("{}{}{}", color_code, text, reset) |
| 92 | + } else { |
| 93 | + text.to_string() |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// Conditionally wrap a string with ANSI color codes for stderr. |
| 98 | +pub fn maybe_color_stderr(text: &str, color_code: &str, reset: &str) -> String { |
| 99 | + if should_colorize_stderr() { |
| 100 | + format!("{}{}{}", color_code, text, reset) |
| 101 | + } else { |
| 102 | + text.to_string() |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/// ANSI color codes for common colors. |
| 107 | +pub mod colors { |
| 108 | + pub const RESET: &str = "\x1b[0m"; |
| 109 | + pub const BOLD: &str = "\x1b[1m"; |
| 110 | + pub const DIM: &str = "\x1b[2m"; |
| 111 | + |
| 112 | + pub const RED: &str = "\x1b[31m"; |
| 113 | + pub const GREEN: &str = "\x1b[32m"; |
| 114 | + pub const YELLOW: &str = "\x1b[33m"; |
| 115 | + pub const BLUE: &str = "\x1b[34m"; |
| 116 | + pub const MAGENTA: &str = "\x1b[35m"; |
| 117 | + pub const CYAN: &str = "\x1b[36m"; |
| 118 | + pub const WHITE: &str = "\x1b[37m"; |
| 119 | + |
| 120 | + pub const BOLD_RED: &str = "\x1b[1;31m"; |
| 121 | + pub const BOLD_GREEN: &str = "\x1b[1;32m"; |
| 122 | + pub const BOLD_YELLOW: &str = "\x1b[1;33m"; |
| 123 | + pub const BOLD_BLUE: &str = "\x1b[1;34m"; |
| 124 | + pub const BOLD_MAGENTA: &str = "\x1b[1;35m"; |
| 125 | + pub const BOLD_CYAN: &str = "\x1b[1;36m"; |
| 126 | +} |
| 127 | + |
| 128 | +#[cfg(test)] |
| 129 | +mod tests { |
| 130 | + use super::*; |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn test_strip_ansi_codes_basic() { |
| 134 | + let colored = "\x1b[31mRed\x1b[0m Normal"; |
| 135 | + assert_eq!(strip_ansi_codes(colored), "Red Normal"); |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn test_strip_ansi_codes_complex() { |
| 140 | + let colored = "\x1b[1;32mBold Green\x1b[0m \x1b[33mYellow\x1b[0m"; |
| 141 | + assert_eq!(strip_ansi_codes(colored), "Bold Green Yellow"); |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn test_strip_ansi_codes_cursor() { |
| 146 | + let with_cursor = "Hello\x1b[2JWorld\x1b[H"; |
| 147 | + assert_eq!(strip_ansi_codes(with_cursor), "HelloWorld"); |
| 148 | + } |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn test_strip_ansi_codes_no_codes() { |
| 152 | + let plain = "Hello World"; |
| 153 | + assert_eq!(strip_ansi_codes(plain), "Hello World"); |
| 154 | + } |
| 155 | + |
| 156 | + #[test] |
| 157 | + fn test_strip_ansi_codes_empty() { |
| 158 | + assert_eq!(strip_ansi_codes(""), ""); |
| 159 | + } |
| 160 | + |
| 161 | + #[test] |
| 162 | + fn test_strip_ansi_codes_only_codes() { |
| 163 | + let only_codes = "\x1b[31m\x1b[0m\x1b[32m\x1b[0m"; |
| 164 | + assert_eq!(strip_ansi_codes(only_codes), ""); |
| 165 | + } |
| 166 | +} |
0 commit comments