Skip to content
Myr

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

1 file+35 added−6 removed

modified:src/main.rs+35 added-6 removed
11 unmodified lines
12use std::io::{Write, BufReader,BufRead};13use std::path::{Path, Component};14use std::process;1516use ansi_term::Colour;16use ansi_term::{Style, Colour};17use getopts::Options;18use isatty::stdout_isatty;19use regex::{Regex, RegexBuilder};20use walkdir::{WalkDir, DirEntry, WalkDirIterator};
51 unmodified lines
72 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 };8283 print!("{}", style.paint(comp_str.to_str().unwrap()));
58 unmodified lines
142fn parse_dircolors(path: &Path) -> std::io::Result<ExtensionStyles> {143 let file = File::open(path)?;144 let mut ext_styles = HashMap::new();145146 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();149150 let pattern_ansi =151 Regex::new(r"^\.([A-Za-z0-9]+)\s*([0-9][0-9]);([0-9][0-9])\b")152 .unwrap();153154 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