diff --git a/tree_structurer/cpp/tree_structurer.cpp b/tree_structurer/cpp/tree_structurer.cpp index 028cdb0..a2af3cb 100644 --- a/tree_structurer/cpp/tree_structurer.cpp +++ b/tree_structurer/cpp/tree_structurer.cpp @@ -3,16 +3,23 @@ #include namespace fs = std::filesystem; - bool TreeStructurer::should_ignore_dir(const std::string& dirname) { static const std::vector ignore_list = { "build", "venv", "myenv", "dist", "node_modules", "CMakeFiles", ".git", ".idea", ".vscode", "__pycache__" }; - - return dirname[0] == '.' || - dirname[0] == '_' || // This will catch __pycache__ and _directories - std::find(ignore_list.begin(), ignore_list.end(), dirname) != ignore_list.end(); + + // Ignore directories in the ignore list + if (std::find(ignore_list.begin(), ignore_list.end(), dirname) != ignore_list.end()) { + return true; + } + + // Ignore directories starting with '.' or '_' + if (!dirname.empty() && (dirname[0] == '.' || dirname[0] == '_')) { + return true; + } + + return false; } bool TreeStructurer::should_ignore_file(const std::string& filename) { @@ -20,45 +27,61 @@ bool TreeStructurer::should_ignore_file(const std::string& filename) { ".pyc", ".pyo", ".pyd", ".so", ".dll", ".dylib", ".o", ".obj", ".a", ".lib" }; - - if (filename[0] == '.') return true; - + + // Ignore files starting with '.' or '_' + if (!filename.empty() && (filename[0] == '.' || filename[0] == '_')) { + return true; + } + + // Ignore files with specific extensions fs::path path(filename); std::string ext = path.extension().string(); - return std::find(ignore_extensions.begin(), ignore_extensions.end(), ext) != ignore_extensions.end(); + if (std::find(ignore_extensions.begin(), ignore_extensions.end(), ext) != ignore_extensions.end()) { + return true; + } + + return false; } std::vector TreeStructurer::get_filtered_paths(const fs::path& start) { - std::vector paths; - - for(const auto& entry : fs::recursive_directory_iterator(start)) { + std::vector paths; + + for (const auto& entry : fs::recursive_directory_iterator(start)) { const auto& path = entry.path(); - bool should_include = true; - - for(auto it = path.begin(); it != path.end(); ++it) { - if(should_ignore_dir(it->string())) { - should_include = false; + + // Skip any path that contains an ignored directory + bool contains_ignored_dir = false; + for (auto it = path.begin(); it != path.end(); ++it) { + if (should_ignore_dir(it->string())) { + contains_ignored_dir = true; break; + } } + + if (contains_ignored_dir) { + continue; } - - if(should_include) { - if(fs::is_directory(path)) { - if(!should_ignore_dir(path.filename().string())) { - paths.push_back(path); + + // Handle directories + if (fs::is_directory(path)) { + if (!should_ignore_dir(path.filename().string())) { + paths.push_back(path); + } } - } else { - if(!should_ignore_file(path.filename().string())) { - paths.push_back(path); - } -} + // Handle files + else { + if (!should_ignore_file(path.filename().string())) { + paths.push_back(path); + } } } - + std::sort(paths.begin(), paths.end()); return paths; } + + std::string TreeStructurer::create_indent(int level) { if (level == 0) return "";