Colored output, closes #8
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent b1baa5c · Browse files · 3 files·+27 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+27 added−7 removed
- other:+27 added−7 removed
Provenance
- Commit
fba5e02- Tree
bcc4fdf- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
b1baa5c
3 files changed+27 added−7 removed
modified:src/main.rs+19 added-7 removed
1 extern crate walkdir;2 extern crate regex;3 extern crate getopts;4+extern crate ansi_term;5 6 use std::env;7 use std::path::Path;8 use std::io::Write;2 unmodified lines11 12 use walkdir::{WalkDir, DirEntry, WalkDirIterator};13 use regex::{Regex, RegexBuilder};14 use getopts::Options;15+use ansi_term::Colour;16 17+/// Print a search result to the console.18+fn print_entry(entry: &DirEntry, path_str: &str) {19+ let style = match entry {20+ e if e.path_is_symbolic_link() => Colour::Purple,21+ e if e.path().is_dir() => Colour::Cyan,22+ _ => Colour::White23+ };24+ println!("{}", style.paint(path_str));25+}26+27 /// Check if filename of entry starts with a dot.28 fn is_hidden(entry: &DirEntry) -> bool {29 entry.file_name()30 .to_str()10 unmodified lines41 if entry.path() == root {42 continue;43 }44 33- let path_str_r = entry34- .path()35- .strip_prefix(root) // create relative path36- .ok()37- .and_then(Path::to_str);45+ let path_relative =46+ match entry.path().strip_prefix(root) {47+ Ok(r) => r,48+ Err(_) => continue49+ };50 39- let path_str = match path_str_r {51+ let path_str = match path_relative.to_str() {52 Some(p) => p,53 None => continue54 };55 56 pattern.find(path_str)45- .map(|_| println!("{}", path_str));57+ .map(|_| print_entry(&entry, path_str));58 }59 }60 61 /// Print error message to stderr and exit with status `1`.41 unmodified linesmodified:Cargo.lock+7 added
1 [root]2 name = "fd"3 version = "0.1.0"4 dependencies = [5+ "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",6 "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",7 "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",8 "walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",9 ]6 unmodified lines16 "memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",17 ]18 19 [[package]]20+name = "ansi_term"21+version = "0.9.0"22+source = "registry+https://github.com/rust-lang/crates.io-index"23+24+[[package]]25 name = "getopts"26 version = "0.2.14"27 source = "registry+https://github.com/rust-lang/crates.io-index"28 102 unmodified lines131 source = "registry+https://github.com/rust-lang/crates.io-index"132 133 [metadata]134 "checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"135+"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"136 "checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685"137 "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"138 "checksum libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)" = "babb8281da88cba992fa1f4ddec7d63ed96280a1a53ec9b919fd37b53d71e502"139 "checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4"11 unmodified linesmodified:Cargo.toml+1 added
5 unmodified lines6789105 unmodified lines[dependencies]getopts = "0.2"regex = "0.2"walkdir = "1"ansi_term = "0.9"