Skip to content
Myr

fnd.cpp

This file

Path

src/fnd.cpp

62 lines · 1.4 KiB · C++

Last changed by

Add initial codesharkdpbefore adoptionaab0a2d

History of this file

Take 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);5354        findFiles(re);55    }56    catch (const std::regex_error& e) {57        std::cerr << "Regex error: " << e.what() << std::endl;58        return 1;59    }6061    return 0;62}