Skip to content
Myr

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

1 file+73 added−32 removed

modified:src/lscolors/mod.rs+73 added-32 removed
31 unmodified lines
32 filenames: HashMap::new()33 }34 }3536 /// 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 }4546 /// Parse ANSI escape sequences like `38;5;10;1`.47 fn parse_style(code: &str) -> Option<Style> {48 let mut split = code.split(";");4950 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);5344 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();5758 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 };6653 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);5758 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 };6869 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 };8485 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 }9192 let ansi_style = decoration.unwrap_or(Colour::normal)(color);9380 Some(style)94 Some(ansi_style)95 } else {96 None97 }98 }
46 unmodified lines
145 }146}147148#[test]135fn test_parse_style() {149fn test_parse_simple() {150 assert_eq!(Some(Colour::Red.normal()),151 LsColors::parse_style("31"));152}153154#[test]155fn test_parse_decoration() {156 assert_eq!(Some(Colour::Red.normal()),157 LsColors::parse_style("00;31"));158159 assert_eq!(Some(Colour::Blue.italic()),160 LsColors::parse_style("03;34"));161162 assert_eq!(Some(Colour::Cyan.bold()),163 LsColors::parse_style("01;36"));164}165166#[test]167fn test_parse_decoration_backwards() {168 assert_eq!(Some(Colour::Blue.italic()),169 LsColors::parse_style("34;03"));170171 assert_eq!(Some(Colour::Cyan.bold()),172 LsColors::parse_style("36;01"));173174 assert_eq!(Some(Colour::Red.normal()),175 LsColors::parse_style("31;00"));176}177178#[test]179fn test_parse_256() {180 assert_eq!(Some(Colour::Fixed(115).normal()),181 LsColors::parse_style("38;5;115"));182183 assert_eq!(Some(Colour::Fixed(115).normal()),184 LsColors::parse_style("00;38;5;115"));185186 assert_eq!(Some(Colour::Fixed(119).bold()),187 LsColors::parse_style("01;38;5;119"));188189 assert_eq!(Some(Colour::Fixed(119).bold()),190 LsColors::parse_style("38;5;119;01"));191}192193#[test]153fn test_lscolors() {194fn test_from_string() {195 assert_eq!(LsColors::default(), LsColors::from_string(&String::new()));196197 let result = LsColors::from_string(198 &String::from("rs=0:di=03;34:ln=01;36:*.foo=01;35:*README=33"));
6 unmodified lines