1use std::fmt::{Display, Formatter};
3// Wrapper to avoid unnecessary branching when input doesn't have ANSI escape sequences.
5 attributes: Option<Attributes>,
10 AnsiStyle { attributes: None }
13 pub fn update(&mut self, sequence: &str) -> bool {
14 match &mut self.attributes {
15 Some(a) => a.update(sequence),
17 self.attributes = Some(Attributes::new());
18 self.attributes.as_mut().unwrap().update(sequence)
24impl Display for AnsiStyle {
25 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
26 match self.attributes {
27 Some(ref a) => a.fmt(f),
38 /// The character set to use.
39 /// REGEX: `\^[()][AB0-3]`
42 /// A buffer for unknown sequences.
43 unknown_buffer: String,
67 pub fn new() -> Self {
69 foreground: "".to_owned(),
70 background: "".to_owned(),
71 underlined: "".to_owned(),
72 charset: "".to_owned(),
73 unknown_buffer: "".to_owned(),
76 underline: "".to_owned(),
77 italic: "".to_owned(),
78 strike: "".to_owned(),
82 /// Update the attributes with an escape sequence.
83 /// Returns `false` if the sequence is unsupported.
84 pub fn update(&mut self, sequence: &str) -> bool {
85 let mut chars = sequence.char_indices().skip(1);
87 if let Some((_, t)) = chars.next() {
89 '(' => self.update_with_charset('(', chars.map(|(_, c)| c)),
90 ')' => self.update_with_charset(')', chars.map(|(_, c)| c)),
92 if let Some((i, last)) = chars.last() {
93 // SAFETY: Always starts with ^[ and ends with m.
94 self.update_with_csi(last, &sequence[2..i])
99 _ => self.update_with_unsupported(sequence),
106 fn sgr_reset(&mut self) {
107 self.foreground.clear();
108 self.background.clear();
109 self.underlined.clear();
112 self.underline.clear();
117 fn update_with_sgr(&mut self, parameters: &str) -> bool {
118 let mut iter = parameters
120 .map(|p| if p.is_empty() { "0" } else { p })
121 .map(|p| p.parse::<u16>())
122 .map(|p| p.unwrap_or(0)); // Treat errors as 0.
124 while let Some(p) = iter.next() {
126 0 => self.sgr_reset(),
127 1 => self.bold = format!("\x1B[{}m", parameters),
128 2 => self.dim = format!("\x1B[{}m", parameters),
129 3 => self.italic = format!("\x1B[{}m", parameters),
130 4 => self.underline = format!("\x1B[{}m", parameters),
131 23 => self.italic.clear(),
132 24 => self.underline.clear(),
137 30..=39 => self.foreground = Self::parse_color(p, &mut iter),
138 40..=49 => self.background = Self::parse_color(p, &mut iter),
139 58..=59 => self.underlined = Self::parse_color(p, &mut iter),
140 90..=97 => self.foreground = Self::parse_color(p, &mut iter),
141 100..=107 => self.foreground = Self::parse_color(p, &mut iter),
143 // Unsupported SGR sequence.
144 // Be compatible and pretend one just wasn't was provided.
152 fn update_with_csi(&mut self, finalizer: char, sequence: &str) -> bool {
153 if finalizer == 'm' {
154 self.update_with_sgr(sequence)
160 fn update_with_unsupported(&mut self, sequence: &str) -> bool {
161 self.unknown_buffer.push_str(sequence);
165 fn update_with_charset(&mut self, kind: char, set: impl Iterator<Item = char>) -> bool {
166 self.charset = format!("\x1B{}{}", kind, set.take(1).collect::<String>());
170 fn parse_color(color: u16, parameters: &mut dyn Iterator<Item = u16>) -> String {
172 8 => match parameters.next() {
173 Some(5) /* 256-color */ => format!("\x1B[{};5;{}m", color, join(";", 1, parameters)),
174 Some(2) /* 24-bit color */ => format!("\x1B[{};2;{}m", color, join(";", 3, parameters)),
175 Some(c) => format!("\x1B[{};{}m", color, c),
179 _ => format!("\x1B[{}m", color),
184impl Display for Attributes {
185 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
188 "{}{}{}{}{}{}{}{}{}",
205 iterator: &mut dyn Iterator<Item = impl ToString>,
209 .map(|i| i.to_string())
210 .collect::<Vec<String>>()
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list