diff --git a/Cargo.lock b/Cargo.lock index 6013483..0d86bb0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,7 @@ name = "fd" version = "0.1.0" 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)", "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)", @@ -15,6 +16,11 @@ dependencies = [ "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]] name = "getopts" version = "0.2.14" @@ -126,6 +132,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "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 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" diff --git a/Cargo.toml b/Cargo.toml index 0eab6db..8703b9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ authors = ["David Peter "] getopts = "0.2" regex = "0.2" walkdir = "1" +ansi_term = "0.9" diff --git a/src/main.rs b/src/main.rs index 294bce0..9f3f14e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ extern crate walkdir; extern crate regex; extern crate getopts; +extern crate ansi_term; use std::env; use std::path::Path; @@ -11,6 +12,17 @@ use std::error::Error; use walkdir::{WalkDir, DirEntry, WalkDirIterator}; use regex::{Regex, RegexBuilder}; 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. fn is_hidden(entry: &DirEntry) -> bool { @@ -30,19 +42,19 @@ fn scan(root: &Path, pattern: &Regex) { continue; } - let path_str_r = entry - .path() - .strip_prefix(root) // create relative path - .ok() - .and_then(Path::to_str); + let path_relative = + match entry.path().strip_prefix(root) { + Ok(r) => r, + Err(_) => continue + }; - let path_str = match path_str_r { + let path_str = match path_relative.to_str() { Some(p) => p, None => continue }; pattern.find(path_str) - .map(|_| println!("{}", path_str)); + .map(|_| print_entry(&entry, path_str)); } }