diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..915bef4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +CMakeCache.txt +CMakeFiles +Makefile +cmake_install.cmake +build +fnd diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d342209 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.1.0) + +project(fnd CXX) + +find_package(Boost 1.56 REQUIRED COMPONENTS filesystem) +include_directories( ${Boost_INCLUDE_DIR} ) + +file(GLOB SOURCES "src/*.cpp") + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic") + +add_executable(fnd ${SOURCES}) + +target_link_libraries(fnd ${Boost_LIBRARIES}) diff --git a/src/fnd.cpp b/src/fnd.cpp new file mode 100644 index 0000000..5c3a622 --- /dev/null +++ b/src/fnd.cpp @@ -0,0 +1,62 @@ +#include +#include +#include + +namespace fs = boost::filesystem; + +static const std::string ANSI_PURPLE = "\x1b[35;06m"; +static const std::string ANSI_CYAN = "\x1b[36;01m"; +static const std::string ANSI_RESET = "\x1b[0m"; + +void printPath(const fs::path& path) { + if (fs::is_symlink(path)) { + std::cout << ANSI_PURPLE; + } else if (fs::is_directory(path)) { + std::cout << ANSI_CYAN; + } + + std::cout << path.string(); + + std::cout << ANSI_RESET << std::endl; + +} + +void findFiles(const std::regex& pattern) { + const fs::path& currentPath = fs::current_path(); + + for (auto& entry: fs::recursive_directory_iterator(currentPath)) { + const fs::path& path = entry.path().lexically_relative(currentPath); + + if (std::regex_search(path.string(), pattern)) { + printPath(path); + } + } +} + +int main(int argc, char* argv[]) { + std::string argument; + + if (argc == 1) { + argument = ""; + } else if (argc == 2) { + argument = argv[1]; + } + + if (argc > 2 || argument == "-h" || argument == "--help") { + std::cerr << "Usage: fnd [PATTERN]" << std::endl; + return 1; + } + + // try to parse the argument as a regex + try { + std::regex re(argument); + + findFiles(re); + } + catch (const std::regex_error& e) { + std::cerr << "Regex error: " << e.what() << std::endl; + return 1; + } + + return 0; +}