mirror of
https://github.com/Llewellynvdm/exa.git
synced 2025-02-05 20:18:25 +00:00
Start writing ls_colors parser
This commit is contained in:
parent
b2201b72d5
commit
5e0003784d
100
src/output/lsc.rs
Normal file
100
src/output/lsc.rs
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use ansi_term::Style;
|
||||||
|
use ansi_term::Colour::*;
|
||||||
|
|
||||||
|
|
||||||
|
pub struct LSColors<'var> {
|
||||||
|
contents: HashMap<&'var str, &'var str>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'var> LSColors<'var> {
|
||||||
|
fn parse(input: &'var str) -> LSColors<'var> {
|
||||||
|
let contents = input.split(":")
|
||||||
|
.flat_map(|mapping| {
|
||||||
|
|
||||||
|
let bits = mapping.split("=")
|
||||||
|
.take(3)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
if bits.len() == 2 { Some((bits[0], bits[1])) }
|
||||||
|
else { None }
|
||||||
|
}).collect();
|
||||||
|
LSColors { contents }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get(&self, facet_name: &str) -> Option<Style> {
|
||||||
|
self.contents.get(facet_name).and_then(ansi_to_style)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ansi_to_style(ansi: &&str) -> Option<Style> {
|
||||||
|
match *ansi {
|
||||||
|
"31" => Some(Red.normal()),
|
||||||
|
"34" => Some(Blue.normal()),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_empty() {
|
||||||
|
let lsc = LSColors::parse("");
|
||||||
|
assert_eq!(lsc.get("di"), 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_one() {
|
||||||
|
let lsc = LSColors::parse("di=34");
|
||||||
|
assert_eq!(lsc.get("di"), Some(Blue.normal()));
|
||||||
|
assert_eq!(lsc.get("ln"), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_and_ignore_one() {
|
||||||
|
let lsc = LSColors::parse("di=34=56");
|
||||||
|
assert_eq!(lsc.get("di"), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_and_ignore_again() {
|
||||||
|
let lsc = LSColors::parse("di=");
|
||||||
|
assert_eq!(lsc.get("di"), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_and_ignore_other() {
|
||||||
|
let lsc = LSColors::parse("=id");
|
||||||
|
assert_eq!(lsc.get("di"), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_and_ignore_equals() {
|
||||||
|
let lsc = LSColors::parse("=");
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ use output::file_name::FileStyle;
|
|||||||
pub use self::cell::{TextCell, TextCellContents, DisplayWidth};
|
pub use self::cell::{TextCell, TextCellContents, DisplayWidth};
|
||||||
pub use self::colours::Colours;
|
pub use self::colours::Colours;
|
||||||
pub use self::escape::escape;
|
pub use self::escape::escape;
|
||||||
|
pub use self::lsc::LSColors;
|
||||||
|
|
||||||
pub mod details;
|
pub mod details;
|
||||||
pub mod file_name;
|
pub mod file_name;
|
||||||
@ -15,6 +16,7 @@ pub mod time;
|
|||||||
mod cell;
|
mod cell;
|
||||||
mod colours;
|
mod colours;
|
||||||
mod escape;
|
mod escape;
|
||||||
|
mod lsc;
|
||||||
mod render;
|
mod render;
|
||||||
mod tree;
|
mod tree;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user