|
| 1 | +use crate::homebrew; |
| 2 | +use std::fmt; |
| 3 | +use std::io::{self, IsTerminal}; |
| 4 | +use std::process::Command; |
| 5 | +use std::str; |
| 6 | +use std::sync::OnceLock; |
| 7 | + |
| 8 | +// Color codes |
| 9 | +const RED: &str = "31"; |
| 10 | +const YELLOW: &str = "33"; |
| 11 | +const BLUE: &str = "34"; |
| 12 | + |
| 13 | +// Style codes |
| 14 | +const RESET: &str = "0"; |
| 15 | +const BOLD: &str = "1"; |
| 16 | +const UNDERLINE: &str = "4"; |
| 17 | + |
| 18 | +static TTY_WIDTH: OnceLock<usize> = OnceLock::new(); |
| 19 | + |
| 20 | +pub struct AnsiBuilder { |
| 21 | + escape_sequences: Vec<&'static str>, |
| 22 | +} |
| 23 | + |
| 24 | +fn colorful_output() -> bool { |
| 25 | + if homebrew::brew_no_color() { |
| 26 | + return false; |
| 27 | + } |
| 28 | + if homebrew::brew_color() { |
| 29 | + return true; |
| 30 | + } |
| 31 | + io::stdout().is_terminal() |
| 32 | +} |
| 33 | + |
| 34 | +impl fmt::Display for AnsiBuilder { |
| 35 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 36 | + if colorful_output() { |
| 37 | + write!(f, "\x1B[{}m", self.escape_sequences.join(";")) |
| 38 | + } else { |
| 39 | + write!(f, "") |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl AnsiBuilder { |
| 45 | + pub fn new() -> Self { |
| 46 | + AnsiBuilder { |
| 47 | + escape_sequences: Vec::new(), |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +macro_rules! ansi_methods_and_functions { |
| 53 | + ($( $name:ident => $value:expr ),* $(,)?) => { |
| 54 | + impl AnsiBuilder { |
| 55 | + $( |
| 56 | + #[inline] |
| 57 | + pub fn $name(mut self) -> Self { |
| 58 | + self.escape_sequences.push($value); |
| 59 | + self |
| 60 | + } |
| 61 | + )* |
| 62 | + } |
| 63 | + |
| 64 | + $( |
| 65 | + #[inline] |
| 66 | + #[allow(unused)] |
| 67 | + pub fn $name() -> AnsiBuilder { |
| 68 | + AnsiBuilder::new().$name() |
| 69 | + } |
| 70 | + )* |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +ansi_methods_and_functions! { |
| 75 | + red => RED, |
| 76 | + yellow => YELLOW, |
| 77 | + blue => BLUE, |
| 78 | + reset => RESET, |
| 79 | + bold => BOLD, |
| 80 | + underline => UNDERLINE, |
| 81 | +} |
| 82 | + |
| 83 | +pub fn width() -> usize { |
| 84 | + *TTY_WIDTH.get_or_init(|| { |
| 85 | + if let Ok(command) = Command::new("/bin/stty").arg("size").output() { |
| 86 | + let mut output = command.stdout.split(|ch| ch.is_ascii_whitespace()); |
| 87 | + |
| 88 | + let _ = output.next(); |
| 89 | + if let Some(width) = output |
| 90 | + .next() |
| 91 | + .and_then(|string| str::from_utf8(string).ok()) |
| 92 | + .and_then(|string| string.parse().ok()) |
| 93 | + { |
| 94 | + return width; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if let Ok(output) = Command::new("/usr/bin/tput").arg("cols").output() |
| 99 | + && let Some(res) = str::from_utf8(&output.stdout) |
| 100 | + .ok() |
| 101 | + .and_then(|string| string.parse().ok()) |
| 102 | + { |
| 103 | + return res; |
| 104 | + } |
| 105 | + |
| 106 | + 80 |
| 107 | + }) |
| 108 | +} |
| 109 | + |
| 110 | +pub fn truncate(string: &str) -> &str { |
| 111 | + let w = width(); |
| 112 | + |
| 113 | + if w < 4 { |
| 114 | + return string; |
| 115 | + } |
| 116 | + |
| 117 | + let mut end = usize::min(string.len(), w - 4); |
| 118 | + |
| 119 | + while end > 0 && !string.is_char_boundary(end) { |
| 120 | + end -= 1; |
| 121 | + } |
| 122 | + |
| 123 | + &string[..end] |
| 124 | +} |
| 125 | + |
| 126 | +#[cfg(test)] |
| 127 | +mod test { |
| 128 | + use super::*; |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn test_ansi_builder() { |
| 132 | + let expected = if colorful_output() { |
| 133 | + "\x1B[31;1;4m" |
| 134 | + } else { |
| 135 | + "" |
| 136 | + }; |
| 137 | + assert_eq!( |
| 138 | + expected, |
| 139 | + AnsiBuilder::new() |
| 140 | + .red() |
| 141 | + .bold() |
| 142 | + .underline() |
| 143 | + .to_string() |
| 144 | + .as_str() |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + #[test] |
| 149 | + fn test_truncate() { |
| 150 | + let w = width(); |
| 151 | + |
| 152 | + if w < 4 { |
| 153 | + let string = "some string that is longer than 4 characters for sure"; |
| 154 | + assert_eq!(string.len(), truncate(string).len()); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + let not_trimmed = "a".repeat(w - 4); |
| 159 | + assert_eq!(not_trimmed.len(), truncate(¬_trimmed).len()); |
| 160 | + let trimmed = "a".repeat(w); |
| 161 | + assert_eq!(trimmed.len() - 4, truncate(&trimmed).len()); |
| 162 | + } |
| 163 | +} |
0 commit comments