fnd.cpp
This file
Path
src/fnd.cpp
63 lines · 1.5 KiB · C++
Last changed by
Case insensitivity, closes #3sharkdpbefore adoption4a36b0e
History of this fileTake it
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;2122}2324void findFiles(const std::regex& pattern) {25 const fs::path& currentPath = fs::current_path();2627 for (auto& entry: fs::recursive_directory_iterator(currentPath)) {28 const fs::path& path = entry.path().lexically_relative(currentPath);2930 if (std::regex_search(path.string(), pattern)) {31 printPath(path);32 }33 }34}3536int main(int argc, char* argv[]) {37 std::string argument;3839 if (argc == 1) {40 argument = "";41 } else if (argc == 2) {42 argument = argv[1];43 }4445 if (argc > 2 || argument == "-h" || argument == "--help") {46 std::cerr << "Usage: fnd [PATTERN]" << std::endl;47 return 1;48 }4950 // try to parse the argument as a regex51 try {52 std::regex re(argument, std::regex_constants::ECMAScript53 | std::regex_constants::icase );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}