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
Provenance
- Commit
12bfee0- Tree
36c65f0- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
04892d2
7 files changed+244 added−105 removed
deleted:src/fnd.cpp-63 removed
1-#include <iostream>2-#include <regex>3-#include <boost/filesystem.hpp>4-5-namespace fs = boost::filesystem;6-7-static const std::string ANSI_PURPLE = "\x1b[35;06m";8-static const std::string ANSI_CYAN = "\x1b[36;01m";9-static const std::string ANSI_RESET = "\x1b[0m";10-11-void 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- }17-18- std::cout << path.string();19-20- std::cout << ANSI_RESET << std::endl;21-}22-23-void findFiles(const std::regex& pattern) {24- const fs::path& currentPath = fs::current_path();25-26- for (auto& entry: fs::recursive_directory_iterator(currentPath)) {27- const fs::path& path = entry.path().lexically_relative(currentPath);28-29- if (std::regex_search(path.string(), pattern)) {30- printPath(path);31- }32- }33-}34-35-int main(int argc, char* argv[]) {36- std::string argument;37-38- if (argc == 1) {39- argument = "";40- } else if (argc == 2) {41- argument = argv[1];42- }43-44- if (argc > 2 || argument == "-h" || argument == "--help") {45- std::cerr << "Usage: fnd [PATTERN]" << std::endl;46- return 1;47- }48-49- // 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);54-55- findFiles(re);56- }57- catch (const std::regex_error& e) {58- std::cerr << "Regex error: " << e.what() << std::endl;59- return 1;60- }61-62- return 0;63-}added:src/main.rs+78 added
1+extern crate walkdir;2+extern crate regex;3+extern crate getopts;4+5+use std::env;6+use std::path::Path;7+use std::io::Write;8+use std::process;9+use std::error::Error;10+11+use walkdir::{WalkDir, DirEntry, WalkDirIterator};12+use regex::Regex;13+use getopts::Options;14+15+fn is_hidden(entry: &DirEntry) -> bool {16+ entry.file_name()17+ .to_str()18+ .map(|s| s.starts_with("."))19+ .unwrap_or(false)20+}21+22+fn 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+ }29+30+ 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+}43+44+fn error<T: Error>(err: &T) -> ! {45+ writeln!(&mut std::io::stderr(), "{}", err.description())46+ .expect("Failed writing to stderr");47+ process::exit(1);48+}49+50+fn main() {51+ let args: Vec<String> = env::args().collect();52+53+ 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+ };59+60+ if matches.opt_present("h") {61+ let brief = "Usage: fd [PATTERN]";62+ print!("{}", opts.usage(&brief));63+ process::exit(1);64+ }65+66+ let empty = String::new();67+ let pattern = matches.free.get(0).unwrap_or(&empty);68+69+ let current_dir_buf = env::current_dir()70+ .expect("Could not get current directory!");71+ let current_dir = current_dir_buf.as_path();72+73+ 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
1-CMakeCache.txt2-CMakeFiles3-Makefile4-cmake_install.cmake5-build6-fnd1+target/2+**/*.rs.bkadded:Cargo.lock+143 added
1+[root]2+name = "fd"3+version = "0.1.0"4+dependencies = [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+]9+10+[[package]]11+name = "aho-corasick"12+version = "0.6.3"13+source = "registry+https://github.com/rust-lang/crates.io-index"14+dependencies = [15+ "memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",16+]17+18+[[package]]19+name = "getopts"20+version = "0.2.14"21+source = "registry+https://github.com/rust-lang/crates.io-index"22+23+[[package]]24+name = "kernel32-sys"25+version = "0.2.2"26+source = "registry+https://github.com/rust-lang/crates.io-index"27+dependencies = [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+]31+32+[[package]]33+name = "libc"34+version = "0.2.22"35+source = "registry+https://github.com/rust-lang/crates.io-index"36+37+[[package]]38+name = "memchr"39+version = "1.0.1"40+source = "registry+https://github.com/rust-lang/crates.io-index"41+dependencies = [42+ "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",43+]44+45+[[package]]46+name = "regex"47+version = "0.2.1"48+source = "registry+https://github.com/rust-lang/crates.io-index"49+dependencies = [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+]56+57+[[package]]58+name = "regex-syntax"59+version = "0.4.0"60+source = "registry+https://github.com/rust-lang/crates.io-index"61+62+[[package]]63+name = "same-file"64+version = "0.1.3"65+source = "registry+https://github.com/rust-lang/crates.io-index"66+dependencies = [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+]70+71+[[package]]72+name = "thread-id"73+version = "3.0.0"74+source = "registry+https://github.com/rust-lang/crates.io-index"75+dependencies = [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+]79+80+[[package]]81+name = "thread_local"82+version = "0.3.3"83+source = "registry+https://github.com/rust-lang/crates.io-index"84+dependencies = [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+]88+89+[[package]]90+name = "unreachable"91+version = "0.1.1"92+source = "registry+https://github.com/rust-lang/crates.io-index"93+dependencies = [94+ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",95+]96+97+[[package]]98+name = "utf8-ranges"99+version = "1.0.0"100+source = "registry+https://github.com/rust-lang/crates.io-index"101+102+[[package]]103+name = "void"104+version = "1.0.2"105+source = "registry+https://github.com/rust-lang/crates.io-index"106+107+[[package]]108+name = "walkdir"109+version = "1.0.7"110+source = "registry+https://github.com/rust-lang/crates.io-index"111+dependencies = [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+]116+117+[[package]]118+name = "winapi"119+version = "0.2.8"120+source = "registry+https://github.com/rust-lang/crates.io-index"121+122+[[package]]123+name = "winapi-build"124+version = "0.1.1"125+source = "registry+https://github.com/rust-lang/crates.io-index"126+127+[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
123456789[package]name = "fd"version = "0.1.0"authors = ["David Peter <mail@david-peter.de>"]
[dependencies]getopts = "0.2"regex = "0.2"walkdir = "1"deleted:CMakeLists.txt-18 removed
1-cmake_minimum_required(VERSION 3.1.0)2-3-project(fnd CXX)4-5-find_package(Boost 1.60 REQUIRED COMPONENTS system filesystem)6-include_directories( ${Boost_INCLUDE_DIR} )7-8-file(GLOB SOURCES "src/*.cpp")9-10-set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic")11-12-add_executable(fnd ${SOURCES})13-14-target_compile_features(fnd PRIVATE cxx_range_for)15-16-target_link_libraries(fnd ${Boost_LIBRARIES})17-18-install(TARGETS fnd DESTINATION bin)modified:README.md+12 added-18 removed
112345567889101112131412131415161717181919202021222223232425252628293031272834352930# fnd# fdA modern, convenient and fast replacement for `find`.
**Features:*** Easy syntax. `fnd PATTERN` instead of `find -iname '*PATTERN*'`.* Easy syntax: `fd PATTERN` instead of `find -iname '*PATTERN*'`.* Colored output.* Regular expressions.* The command name is *25%* shorter than `find` :-).* The command name is *50%* shorter than `find` :-).
## Examples``` bash> fndsrcsrc/fnd.cpp> fdREADME.mdsrcsrc/main.rsCargo.tomlLICENSECMakeLists.txtCargo.lock
> fnd cppsrc/fnd.cpp> fd rssrc/main.rs
> fnd '[A-Z].*'README.md> fd '^[A-Z]+$'LICENSECMakeLists.txt```
## Dependencies* g++ `>=4.9`* boost `>=1.60`
## Build```bashcmake .makecargo build```