Extend LS_COLORS parser
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent 7b1a457 · Browse files · 1 file·+73 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+73 added−32 removed
- other:+73 added−32 removed
Provenance
- Commit
ec1f23c- Tree
f502453- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
7b1a457
1 file changed+73 added−32 removed
modified:src/lscolors/mod.rs+73 added-32 removed
31 unmodified lines32 filenames: HashMap::new()33 }34 }35 36- /// Parse a single ANSI style like `38;5;10`.36+ fn parse_decoration(code: &str) -> Option<fn(Colour) -> Style> {37+ match code {38+ "0" | "00" => Some(Colour::normal),39+ "1" | "01" => Some(Colour::bold),40+ "3" | "03" => Some(Colour::italic),41+ "4" | "04" => Some(Colour::underline),42+ _ => None43+ }44+ }45+46+ /// Parse ANSI escape sequences like `38;5;10;1`.47 fn parse_style(code: &str) -> Option<Style> {48 let mut split = code.split(";");49 50 if let Some(first) = split.next() {41- let second = split.next();42- let third = split.next();51+ // Try to match the first part as a text-decoration argument52+ let mut decoration = LsColors::parse_decoration(first);53 44- let style =45- if first == "38" && second == Some("5") {54+ let c1 = if decoration.is_none() { Some(first) } else { split.next() };55+ let c2 = split.next();56+ let c3 = split.next();57+58+ let color =59+ if c1 == Some("38") && c2 == Some("5") {60 let n_white = 7;47- let n = if let Some(num) = third {61+ let n = if let Some(num) = c3 {62 u8::from_str_radix(num, 10).unwrap_or(n_white)63 } else {64 n_white65 };66 53- Colour::Fixed(n).normal()67+ Colour::Fixed(n)68 } else {55- let style_s = if second.is_some() { first } else { "" };56- let color_s = second.unwrap_or(first);57-58- let color = match color_s {59- "30" => Colour::Black,60- "31" => Colour::Red,61- "32" => Colour::Green,62- "33" => Colour::Yellow,63- "34" => Colour::Blue,64- "35" => Colour::Purple,65- "36" => Colour::Cyan,66- _ => Colour::White67- };68-69- match style_s {69+ if let Some(color_s) = c1 {70+ match color_s {71+ "30" => Colour::Black,72+ "31" => Colour::Red,73+ "32" => Colour::Green,74+ "33" => Colour::Yellow,75+ "34" => Colour::Blue,76+ "35" => Colour::Purple,77+ "36" => Colour::Cyan,78+ _ => Colour::White79+ }80+ } else {81+ Colour::White70- "1" => color.bold(),71- "01" => color.bold(),72- "3" => color.italic(),73- "03" => color.italic(),74- "4" => color.underline(),75- "04" => color.underline(),76- _ => color.normal()82 }83 };84 85+ if decoration.is_none() {86+ // Try to find a decoration somewhere in the sequence87+ decoration = code.split(";")88+ .flat_map(LsColors::parse_decoration)89+ .next();90+ }91+92+ let ansi_style = decoration.unwrap_or(Colour::normal)(color);93+80- Some(style)94+ Some(ansi_style)95 } else {96 None97 }98 }46 unmodified lines145 }146 }147 148 #[test]135-fn test_parse_style() {149+fn test_parse_simple() {150 assert_eq!(Some(Colour::Red.normal()),151 LsColors::parse_style("31"));152+}153 154+#[test]155+fn test_parse_decoration() {156 assert_eq!(Some(Colour::Red.normal()),157 LsColors::parse_style("00;31"));158 159 assert_eq!(Some(Colour::Blue.italic()),160 LsColors::parse_style("03;34"));161 162 assert_eq!(Some(Colour::Cyan.bold()),163 LsColors::parse_style("01;36"));164+}165 166+#[test]167+fn test_parse_decoration_backwards() {168+ assert_eq!(Some(Colour::Blue.italic()),169+ LsColors::parse_style("34;03"));170+171+ assert_eq!(Some(Colour::Cyan.bold()),172+ LsColors::parse_style("36;01"));173+174+ assert_eq!(Some(Colour::Red.normal()),175+ LsColors::parse_style("31;00"));176+}177+178+#[test]179+fn test_parse_256() {180 assert_eq!(Some(Colour::Fixed(115).normal()),181 LsColors::parse_style("38;5;115"));182+183+ assert_eq!(Some(Colour::Fixed(115).normal()),184+ LsColors::parse_style("00;38;5;115"));185+186+ assert_eq!(Some(Colour::Fixed(119).bold()),187+ LsColors::parse_style("01;38;5;119"));188+189+ assert_eq!(Some(Colour::Fixed(119).bold()),190+ LsColors::parse_style("38;5;119;01"));191 }192 193 #[test]153-fn test_lscolors() {194+fn test_from_string() {195 assert_eq!(LsColors::default(), LsColors::from_string(&String::new()));196 197 let result = LsColors::from_string(198 &String::from("rs=0:di=03;34:ln=01;36:*.foo=01;35:*README=33"));6 unmodified lines