Skip to content
Myr

Re-write in rust

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by sharkdp · committed by sharkdp · parent 04892d2 · Browse files · 7 files+244 added−105 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+244 added−105 removed

  • other:+244 added−105 removed

7 files+244 added−105 removed

deleted:src/fnd.cpp-63 removed
1#include <iostream>2#include <regex>3#include <boost/filesystem.hpp>45namespace fs = boost::filesystem;67static const std::string ANSI_PURPLE = "\x1b[35;06m";8static const std::string ANSI_CYAN = "\x1b[36;01m";9static const std::string ANSI_RESET = "\x1b[0m";1011void printPath(const fs::path& path) {12 if (fs::is_symlink(path)) {13 std::cout << ANSI_PURPLE;14 } else if (fs::is_directory(path)) {15 std::cout << ANSI_CYAN;16 }1718 std::cout << path.string();1920 std::cout << ANSI_RESET << std::endl;21}2223void findFiles(const std::regex& pattern) {24 const fs::path& currentPath = fs::current_path();2526 for (auto& entry: fs::recursive_directory_iterator(currentPath)) {27 const fs::path& path = entry.path().lexically_relative(currentPath);2829 if (std::regex_search(path.string(), pattern)) {30 printPath(path);31 }32 }33}3435int main(int argc, char* argv[]) {36 std::string argument;3738 if (argc == 1) {39 argument = "";40 } else if (argc == 2) {41 argument = argv[1];42 }4344 if (argc > 2 || argument == "-h" || argument == "--help") {45 std::cerr << "Usage: fnd [PATTERN]" << std::endl;46 return 1;47 }4849 // try to parse the argument as a regex50 try {51 std::regex::flag_type flags = std::regex_constants::ECMAScript52 | std::regex_constants::icase;53 std::regex re(argument, flags);5455 findFiles(re);56 }57 catch (const std::regex_error& e) {58 std::cerr << "Regex error: " << e.what() << std::endl;59 return 1;60 }6162 return 0;63}
added:src/main.rs+78 added
1extern crate walkdir;2extern crate regex;3extern crate getopts;45use std::env;6use std::path::Path;7use std::io::Write;8use std::process;9use std::error::Error;1011use walkdir::{WalkDir, DirEntry, WalkDirIterator};12use regex::Regex;13use getopts::Options;1415fn is_hidden(entry: &DirEntry) -> bool {16 entry.file_name()17 .to_str()18 .map(|s| s.starts_with("."))19 .unwrap_or(false)20}2122fn scan(root: &Path, pattern: &Regex) {23 let walker = WalkDir::new(root).into_iter();24 for entry in walker.filter_entry(|e| !is_hidden(e))25 .filter_map(|e| e.ok()) {26 if entry.path() == root {27 continue;28 }2930 let path_relative = entry.path().strip_prefix(root).unwrap();31 let path_str = match path_relative.to_str() {32 Some(s) => s,33 None => continue34 };35 match pattern.find(path_str) {36 Some(_) =>37 println!("{}", path_str),38 None =>39 continue40 }41 }42}4344fn error<T: Error>(err: &T) -> ! {45 writeln!(&mut std::io::stderr(), "{}", err.description())46 .expect("Failed writing to stderr");47 process::exit(1);48}4950fn main() {51 let args: Vec<String> = env::args().collect();5253 let mut opts = Options::new();54 opts.optflag("h", "help", "print this help message");55 let matches = match opts.parse(&args[1..]) {56 Ok(m) => { m }57 Err(f) => { error(&f) }58 };5960 if matches.opt_present("h") {61 let brief = "Usage: fd [PATTERN]";62 print!("{}", opts.usage(&brief));63 process::exit(1);64 }6566 let empty = String::new();67 let pattern = matches.free.get(0).unwrap_or(&empty);6869 let current_dir_buf = env::current_dir()70 .expect("Could not get current directory!");71 let current_dir = current_dir_buf.as_path();7273 match Regex::new(pattern) {74 Ok(re) =>75 scan(current_dir, &re),76 Err(err) => error(&err)77 }78}
modified:.gitignore+2 added-6 removed
1CMakeCache.txt2CMakeFiles3Makefile4cmake_install.cmake5build6fnd1target/2**/*.rs.bk
added:Cargo.lock+143 added
1[root]2name = "fd"3version = "0.1.0"4dependencies = [5 "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",6 "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",7 "walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",8]910[[package]]11name = "aho-corasick"12version = "0.6.3"13source = "registry+https://github.com/rust-lang/crates.io-index"14dependencies = [15 "memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",16]1718[[package]]19name = "getopts"20version = "0.2.14"21source = "registry+https://github.com/rust-lang/crates.io-index"2223[[package]]24name = "kernel32-sys"25version = "0.2.2"26source = "registry+https://github.com/rust-lang/crates.io-index"27dependencies = [28 "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",29 "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",30]3132[[package]]33name = "libc"34version = "0.2.22"35source = "registry+https://github.com/rust-lang/crates.io-index"3637[[package]]38name = "memchr"39version = "1.0.1"40source = "registry+https://github.com/rust-lang/crates.io-index"41dependencies = [42 "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",43]4445[[package]]46name = "regex"47version = "0.2.1"48source = "registry+https://github.com/rust-lang/crates.io-index"49dependencies = [50 "aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",51 "memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",52 "regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",53 "thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",54 "utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",55]5657[[package]]58name = "regex-syntax"59version = "0.4.0"60source = "registry+https://github.com/rust-lang/crates.io-index"6162[[package]]63name = "same-file"64version = "0.1.3"65source = "registry+https://github.com/rust-lang/crates.io-index"66dependencies = [67 "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",68 "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",69]7071[[package]]72name = "thread-id"73version = "3.0.0"74source = "registry+https://github.com/rust-lang/crates.io-index"75dependencies = [76 "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",77 "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",78]7980[[package]]81name = "thread_local"82version = "0.3.3"83source = "registry+https://github.com/rust-lang/crates.io-index"84dependencies = [85 "thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)",86 "unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",87]8889[[package]]90name = "unreachable"91version = "0.1.1"92source = "registry+https://github.com/rust-lang/crates.io-index"93dependencies = [94 "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",95]9697[[package]]98name = "utf8-ranges"99version = "1.0.0"100source = "registry+https://github.com/rust-lang/crates.io-index"101102[[package]]103name = "void"104version = "1.0.2"105source = "registry+https://github.com/rust-lang/crates.io-index"106107[[package]]108name = "walkdir"109version = "1.0.7"110source = "registry+https://github.com/rust-lang/crates.io-index"111dependencies = [112 "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",113 "same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",114 "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",115]116117[[package]]118name = "winapi"119version = "0.2.8"120source = "registry+https://github.com/rust-lang/crates.io-index"121122[[package]]123name = "winapi-build"124version = "0.1.1"125source = "registry+https://github.com/rust-lang/crates.io-index"126127[metadata]128"checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"129"checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685"130"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"131"checksum libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)" = "babb8281da88cba992fa1f4ddec7d63ed96280a1a53ec9b919fd37b53d71e502"132"checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4"133"checksum regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4278c17d0f6d62dfef0ab00028feb45bd7d2102843f80763474eeb1be8a10c01"134"checksum regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9191b1f57603095f105d317e375d19b1c9c5c3185ea9633a99a6dcbed04457"135"checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7"136"checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a"137"checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7"138"checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91"139"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"140"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"141"checksum walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bb08f9e670fab86099470b97cd2b252d6527f0b3cc1401acdb595ffc9dd288ff"142"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"143"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
added:Cargo.toml+9 added
deleted:CMakeLists.txt-18 removed
1cmake_minimum_required(VERSION 3.1.0)23project(fnd CXX)45find_package(Boost 1.60 REQUIRED COMPONENTS system filesystem)6include_directories( ${Boost_INCLUDE_DIR} )78file(GLOB SOURCES "src/*.cpp")910set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic")1112add_executable(fnd ${SOURCES})1314target_compile_features(fnd PRIVATE cxx_range_for)1516target_link_libraries(fnd ${Boost_LIBRARIES})1718install(TARGETS fnd DESTINATION bin)
modified:README.md+12 added-18 removed