Support for normal ANSI colors
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent 8ad71c1 · Browse files · 1 file·+35 added−6 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+35 added−6 removed
- other:+35 added−6 removed
Provenance
- Commit
f25c73c- Tree
aba2374- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
8ad71c1
1 file changed+35 added−6 removed
modified:src/main.rs+35 added-6 removed
11 unmodified lines12 use std::io::{Write, BufReader,BufRead};13 use std::path::{Path, Component};14 use std::process;15 16-use ansi_term::Colour;16+use ansi_term::{Style, Colour};17 use getopts::Options;18 use isatty::stdout_isatty;19 use regex::{Regex, RegexBuilder};20 use walkdir::{WalkDir, DirEntry, WalkDirIterator};51 unmodified lines72 component_path.extension()73 .and_then(|e| e.to_str())74 .and_then(|e| ext_styles.get(e))75 .map(|r| r.clone())76- .unwrap_or(Colour::White.normal())76+ .unwrap_or(Style::new())77 }78 else {79- Colour::White.normal()79+ Style::new()80 }81 };82 83 print!("{}", style.paint(comp_str.to_str().unwrap()));58 unmodified lines142 fn parse_dircolors(path: &Path) -> std::io::Result<ExtensionStyles> {143 let file = File::open(path)?;144 let mut ext_styles = HashMap::new();145 146- let pattern =147- Regex::new(r"^\.([A-Za-z0-9]+)\s*38;5;([0-9]+)\b").unwrap();146+ let pattern_ansi_256 =147+ Regex::new(r"^\.([A-Za-z0-9]+)\s*(?:00;)?38;5;([0-9]+)\b")148+ .unwrap();149 150+ let pattern_ansi =151+ Regex::new(r"^\.([A-Za-z0-9]+)\s*([0-9][0-9]);([0-9][0-9])\b")152+ .unwrap();153+154 for line in BufReader::new(file).lines() {155+ let line_s = line.unwrap();150- if let Some(caps) = pattern.captures(line.unwrap().as_str()) {156+ if let Some(caps) = pattern_ansi_256.captures(line_s.as_str()) {157 if let Some(ext) = caps.get(1).map(|m| m.as_str()) {158 let fg = caps.get(2)159 .map(|m| m.as_str())160 .and_then(|n| u8::from_str_radix(n, 10).ok())161 .unwrap_or(7); // white162 ext_styles.insert(String::from(ext),163 Colour::Fixed(fg).normal());164 }165+ } else if let Some(caps) = pattern_ansi.captures(line_s.as_str()) {166+ if let Some(ext) = caps.get(1).map(|m| m.as_str()) {167+ let color_s = caps.get(3)168+ .map_or("", |m| m.as_str());169+ let color = match color_s {170+ "31" => Colour::Red,171+ "32" => Colour::Green,172+ "33" => Colour::Yellow,173+ "34" => Colour::Blue,174+ "35" => Colour::Purple,175+ "36" => Colour::Cyan,176+ _ => Colour::White177+ };178+ let style_s = caps.get(2)179+ .map_or("", |m| m.as_str());180+ let style = match style_s {181+ "01" => color.bold(),182+ "03" => color.italic(),183+ "04" => color.underline(),184+ _ => color.normal()185+ };186+ ext_styles.insert(String::from(ext), style);187+ }188 }189 }190 Ok(ext_styles)191 }67 unmodified lines