mirror of
https://github.com/sharkdp/fd
synced 2024-10-02 22:13:31 +00:00
Colored output, closes #8
This commit is contained in:
parent
b1baa5cb76
commit
fba5e02c25
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -2,6 +2,7 @@
|
||||||
name = "fd"
|
name = "fd"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -15,6 +16,11 @@ dependencies = [
|
||||||
"memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ansi_term"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "getopts"
|
name = "getopts"
|
||||||
version = "0.2.14"
|
version = "0.2.14"
|
||||||
|
@ -126,6 +132,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
[metadata]
|
[metadata]
|
||||||
"checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"
|
"checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"
|
||||||
|
"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"
|
||||||
"checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685"
|
"checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685"
|
||||||
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
|
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
|
||||||
"checksum libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)" = "babb8281da88cba992fa1f4ddec7d63ed96280a1a53ec9b919fd37b53d71e502"
|
"checksum libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)" = "babb8281da88cba992fa1f4ddec7d63ed96280a1a53ec9b919fd37b53d71e502"
|
||||||
|
|
|
@ -7,3 +7,4 @@ authors = ["David Peter <mail@david-peter.de>"]
|
||||||
getopts = "0.2"
|
getopts = "0.2"
|
||||||
regex = "0.2"
|
regex = "0.2"
|
||||||
walkdir = "1"
|
walkdir = "1"
|
||||||
|
ansi_term = "0.9"
|
||||||
|
|
26
src/main.rs
26
src/main.rs
|
@ -1,6 +1,7 @@
|
||||||
extern crate walkdir;
|
extern crate walkdir;
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
|
extern crate ansi_term;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
@ -11,6 +12,17 @@ use std::error::Error;
|
||||||
use walkdir::{WalkDir, DirEntry, WalkDirIterator};
|
use walkdir::{WalkDir, DirEntry, WalkDirIterator};
|
||||||
use regex::{Regex, RegexBuilder};
|
use regex::{Regex, RegexBuilder};
|
||||||
use getopts::Options;
|
use getopts::Options;
|
||||||
|
use ansi_term::Colour;
|
||||||
|
|
||||||
|
/// Print a search result to the console.
|
||||||
|
fn print_entry(entry: &DirEntry, path_str: &str) {
|
||||||
|
let style = match entry {
|
||||||
|
e if e.path_is_symbolic_link() => Colour::Purple,
|
||||||
|
e if e.path().is_dir() => Colour::Cyan,
|
||||||
|
_ => Colour::White
|
||||||
|
};
|
||||||
|
println!("{}", style.paint(path_str));
|
||||||
|
}
|
||||||
|
|
||||||
/// Check if filename of entry starts with a dot.
|
/// Check if filename of entry starts with a dot.
|
||||||
fn is_hidden(entry: &DirEntry) -> bool {
|
fn is_hidden(entry: &DirEntry) -> bool {
|
||||||
|
@ -30,19 +42,19 @@ fn scan(root: &Path, pattern: &Regex) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let path_str_r = entry
|
let path_relative =
|
||||||
.path()
|
match entry.path().strip_prefix(root) {
|
||||||
.strip_prefix(root) // create relative path
|
Ok(r) => r,
|
||||||
.ok()
|
Err(_) => continue
|
||||||
.and_then(Path::to_str);
|
};
|
||||||
|
|
||||||
let path_str = match path_str_r {
|
let path_str = match path_relative.to_str() {
|
||||||
Some(p) => p,
|
Some(p) => p,
|
||||||
None => continue
|
None => continue
|
||||||
};
|
};
|
||||||
|
|
||||||
pattern.find(path_str)
|
pattern.find(path_str)
|
||||||
.map(|_| println!("{}", path_str));
|
.map(|_| print_entry(&entry, path_str));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue