mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-11-19 18:55:11 +00:00
More ls_colors parsing
This commit is contained in:
parent
5e0003784d
commit
8b0e483c0f
@ -1,3 +1,5 @@
|
|||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use ansi_term::Style;
|
use ansi_term::Style;
|
||||||
@ -17,23 +19,83 @@ impl<'var> LSColors<'var> {
|
|||||||
.take(3)
|
.take(3)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
if bits.len() == 2 { Some((bits[0], bits[1])) }
|
if bits.len() != 2 || bits[0].is_empty() || bits[1].is_empty() { None }
|
||||||
else { None }
|
else { Some( (bits[0], bits[1]) ) }
|
||||||
}).collect();
|
}).collect();
|
||||||
LSColors { contents }
|
LSColors { contents }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(&self, facet_name: &str) -> Option<Style> {
|
fn get(&self, facet_name: &str) -> Option<Style> {
|
||||||
self.contents.get(facet_name).and_then(ansi_to_style)
|
self.contents.get(facet_name).map(ansi_to_style)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ansi_to_style(ansi: &&str) -> Option<Style> {
|
fn ansi_to_style(ansi: &&str) -> Style {
|
||||||
match *ansi {
|
let mut style = Style::default();
|
||||||
"31" => Some(Red.normal()),
|
|
||||||
"34" => Some(Blue.normal()),
|
for num in ansi.split(";") {
|
||||||
_ => None,
|
match num {
|
||||||
|
|
||||||
|
// Bold and italic
|
||||||
|
"1" => style = style.bold(),
|
||||||
|
"4" => style = style.underline(),
|
||||||
|
|
||||||
|
// Foreground colours
|
||||||
|
"30" => style = style.fg(Black),
|
||||||
|
"31" => style = style.fg(Red),
|
||||||
|
"32" => style = style.fg(Green),
|
||||||
|
"33" => style = style.fg(Yellow),
|
||||||
|
"34" => style = style.fg(Blue),
|
||||||
|
"35" => style = style.fg(Purple),
|
||||||
|
"36" => style = style.fg(Cyan),
|
||||||
|
"37" => style = style.fg(White),
|
||||||
|
|
||||||
|
// Background colours
|
||||||
|
"40" => style = style.on(Black),
|
||||||
|
"41" => style = style.on(Red),
|
||||||
|
"42" => style = style.on(Green),
|
||||||
|
"43" => style = style.on(Yellow),
|
||||||
|
"44" => style = style.on(Blue),
|
||||||
|
"45" => style = style.on(Purple),
|
||||||
|
"46" => style = style.on(Cyan),
|
||||||
|
"47" => style = style.on(White),
|
||||||
|
_ => {/* ignore the error and do nothing */},
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
style
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod ansi_test {
|
||||||
|
use super::*;
|
||||||
|
use ansi_term::Style;
|
||||||
|
|
||||||
|
macro_rules! test {
|
||||||
|
($name:ident: $input:expr => $result:expr) => {
|
||||||
|
#[test]
|
||||||
|
fn $name() {
|
||||||
|
assert_eq!(ansi_to_style(&$input), $result);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Styles
|
||||||
|
test!(bold: "1" => Style::default().bold());
|
||||||
|
test!(under: "4" => Style::default().underline());
|
||||||
|
test!(both: "1;4" => Style::default().bold().underline());
|
||||||
|
test!(fg: "31" => Red.normal());
|
||||||
|
test!(bg: "43" => Style::default().on(Yellow));
|
||||||
|
test!(bfg: "31;43" => Red.on(Yellow));
|
||||||
|
test!(all: "43;31;1;4" => Red.on(Yellow).bold().underline());
|
||||||
|
test!(again: "1;1;1;1;1" => Style::default().bold());
|
||||||
|
|
||||||
|
// Failure cases
|
||||||
|
test!(empty: "" => Style::default());
|
||||||
|
test!(semis: ";;;;;;" => Style::default());
|
||||||
|
test!(nines: "99999999" => Style::default());
|
||||||
|
test!(word: "GREEN" => Style::default());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -42,59 +104,45 @@ fn ansi_to_style(ansi: &&str) -> Option<Style> {
|
|||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
macro_rules! test {
|
||||||
|
($name:ident: $input:expr, $facet:expr => $result:expr) => {
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_empty() {
|
fn $name() {
|
||||||
let lsc = LSColors::parse("");
|
let lsc = LSColors::parse($input);
|
||||||
assert_eq!(lsc.get("di"), None);
|
assert_eq!(lsc.get($facet), $result.into());
|
||||||
assert_eq!(lsc.get(""), None);
|
assert_eq!(lsc.get(""), None);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
#[test]
|
|
||||||
fn parse_gibberish() {
|
|
||||||
let lsc = LSColors::parse("gibberish");
|
|
||||||
assert_eq!(lsc.get("di"), None);
|
|
||||||
assert_eq!(lsc.get("gibberish"), None);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bad parses
|
||||||
|
test!(empty: "", "di" => None);
|
||||||
|
test!(jibber: "blah", "di" => None);
|
||||||
|
|
||||||
#[test]
|
test!(equals: "=", "di" => None);
|
||||||
fn parse_one() {
|
test!(starts: "=di", "di" => None);
|
||||||
let lsc = LSColors::parse("di=34");
|
test!(ends: "id=", "id" => None);
|
||||||
assert_eq!(lsc.get("di"), Some(Blue.normal()));
|
|
||||||
assert_eq!(lsc.get("ln"), None);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
// Foreground colours
|
||||||
fn parse_and_ignore_one() {
|
test!(red: "di=31", "di" => Red.normal());
|
||||||
let lsc = LSColors::parse("di=34=56");
|
test!(green: "cb=32", "cb" => Green.normal());
|
||||||
assert_eq!(lsc.get("di"), None);
|
test!(blue: "la=34", "la" => Blue.normal());
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
// Background colours
|
||||||
fn parse_and_ignore_again() {
|
test!(yellow: "do=43", "do" => Style::default().on(Yellow));
|
||||||
let lsc = LSColors::parse("di=");
|
test!(purple: "re=45", "re" => Style::default().on(Purple));
|
||||||
assert_eq!(lsc.get("di"), None);
|
test!(cyan: "mi=46", "mi" => Style::default().on(Cyan));
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
// Bold and underline
|
||||||
fn parse_and_ignore_other() {
|
test!(bold: "fa=1", "fa" => Style::default().bold());
|
||||||
let lsc = LSColors::parse("=id");
|
test!(under: "so=4", "so" => Style::default().underline());
|
||||||
assert_eq!(lsc.get("di"), None);
|
test!(both: "la=1;4", "la" => Style::default().bold().underline());
|
||||||
}
|
|
||||||
|
|
||||||
|
// More and many
|
||||||
|
test!(more_1: "me=43;21;55;34:yu=1;4;1", "me" => Blue.on(Yellow));
|
||||||
|
test!(more_2: "me=43;21;55;34:yu=1;4;1", "yu" => Style::default().bold().underline());
|
||||||
|
|
||||||
#[test]
|
test!(many_1: "red=31:green=32:blue=34", "red" => Red.normal());
|
||||||
fn parse_and_ignore_equals() {
|
test!(many_2: "red=31:green=32:blue=34", "green" => Green.normal());
|
||||||
let lsc = LSColors::parse("=");
|
test!(many_3: "red=31:green=32:blue=34", "blue" => Blue.normal());
|
||||||
assert_eq!(lsc.get("di"), None);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_two() {
|
|
||||||
let lsc = LSColors::parse("di=34:ln=31");
|
|
||||||
assert_eq!(lsc.get("di"), Some(Blue.normal()));
|
|
||||||
assert_eq!(lsc.get("ln"), Some(Red.normal()));
|
|
||||||
assert_eq!(lsc.get("cd"), None);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user