122 lines
4.0 KiB
C++
122 lines
4.0 KiB
C++
#include "tree_structurer.hpp"
|
|
#include <algorithm>
|
|
#include <filesystem>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
bool TreeStructurer::should_ignore_dir(const std::string& dirname) {
|
|
static const std::vector<std::string> 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();
|
|
}
|
|
|
|
bool TreeStructurer::should_ignore_file(const std::string& filename) {
|
|
static const std::vector<std::string> ignore_extensions = {
|
|
".pyc", ".pyo", ".pyd", ".so", ".dll", ".dylib",
|
|
".o", ".obj", ".a", ".lib"
|
|
};
|
|
|
|
if (filename[0] == '.') return true;
|
|
|
|
fs::path path(filename);
|
|
std::string ext = path.extension().string();
|
|
return std::find(ignore_extensions.begin(), ignore_extensions.end(), ext) != ignore_extensions.end();
|
|
}
|
|
|
|
std::vector<fs::path> TreeStructurer::get_filtered_paths(const fs::path& start) {
|
|
std::vector<fs::path> 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;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(should_include) {
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
std::sort(paths.begin(), paths.end());
|
|
return paths;
|
|
}
|
|
|
|
std::string TreeStructurer::create_indent(int level) {
|
|
if (level == 0) return "";
|
|
|
|
std::string indent;
|
|
for (int i = 0; i < level - 1; ++i) {
|
|
indent += "│ ";
|
|
}
|
|
indent += "├─ ";
|
|
return indent;
|
|
}
|
|
|
|
|
|
std::string TreeStructurer::get_relative_path(const fs::path& path, const fs::path& base) {
|
|
fs::path rel = fs::relative(path, base);
|
|
return rel.string();
|
|
}
|
|
|
|
std::vector<std::string> TreeStructurer::get_directory_structure(const std::string& startpath) {
|
|
std::vector<std::string> result;
|
|
fs::path start = startpath.empty() ? fs::current_path() : fs::path(startpath);
|
|
|
|
try {
|
|
result.push_back(start.filename().string() + "/");
|
|
|
|
auto paths = get_filtered_paths(start);
|
|
std::vector<bool> is_last_at_level(256, false);
|
|
|
|
for (size_t i = 0; i < paths.size(); ++i) {
|
|
const auto& path = paths[i];
|
|
std::string rel_path = get_relative_path(path, start);
|
|
int level = std::count(rel_path.begin(), rel_path.end(), fs::path::preferred_separator);
|
|
|
|
bool is_last = (i == paths.size() - 1) ||
|
|
(i < paths.size() - 1 &&
|
|
std::count(get_relative_path(paths[i + 1], start).begin(),
|
|
get_relative_path(paths[i + 1], start).end(),
|
|
fs::path::preferred_separator) <= level);
|
|
|
|
is_last_at_level[level] = is_last;
|
|
|
|
std::string line;
|
|
for (int j = 0; j < level; ++j) {
|
|
if (j == level - 1) {
|
|
line += is_last ? "└── " : "├── ";
|
|
} else {
|
|
line += is_last_at_level[j] ? " " : "│ ";
|
|
}
|
|
}
|
|
|
|
line += path.filename().string();
|
|
if(fs::is_directory(path)) {
|
|
line += "/";
|
|
}
|
|
result.push_back(line);
|
|
}
|
|
} catch (const fs::filesystem_error& e) {
|
|
throw std::runtime_error("Failed to access directory: " + std::string(e.what()));
|
|
}
|
|
|
|
return result;
|
|
} |