tried improvement

This commit is contained in:
Falko Victor Habel 2025-01-19 20:04:40 +01:00
parent 6d5610c05c
commit 06bed02427
3 changed files with 72 additions and 61 deletions

View File

@ -20,7 +20,7 @@ tree_structurer_module = Extension(
setup( setup(
name='tree_structurer', name='tree_structurer',
version='0.0.4', version='0.0.5',
description='A module for analyzing directory structures', description='A module for analyzing directory structures',
ext_modules=[tree_structurer_module], ext_modules=[tree_structurer_module],
packages=['tree_structurer'], packages=['tree_structurer'],

View File

@ -17,6 +17,7 @@ def main():
create_tree_structurer() create_tree_structurer()
# Get and print structure directly # Get and print structure directly
structure = get_structure(args.path) structure = get_structure(args.path)
print(f"dir Structure for {args.path}:")
for line in structure: for line in structure:
print(line) print(line)

View File

@ -3,18 +3,17 @@
#include <filesystem> #include <filesystem>
namespace fs = std::filesystem; namespace fs = std::filesystem;
bool TreeStructurer::should_ignore_dir(const std::string& dirname) { bool TreeStructurer::should_ignore_dir(const std::string& dirname) {
static const std::vector<std::string> ignore_list = { static const std::vector<std::string> ignore_list = {
"build", "venv", "myenv", "dist", "node_modules", "CMakeFiles", "build", "venv", "myenv", "dist", "node_modules", "CMakeFiles",
".git", ".idea", ".vscode", "__pycache__" ".git", ".idea", ".vscode", "__pycache__"
}; };
// Ignore directories in the ignore list
if (std::find(ignore_list.begin(), ignore_list.end(), dirname) != ignore_list.end()) { if (std::find(ignore_list.begin(), ignore_list.end(), dirname) != ignore_list.end()) {
return true; return true;
} }
// Ignore directories starting with '.' or '_'
if (!dirname.empty() && (dirname[0] == '.' || dirname[0] == '_')) { if (!dirname.empty() && (dirname[0] == '.' || dirname[0] == '_')) {
return true; return true;
} }
@ -28,12 +27,10 @@ bool TreeStructurer::should_ignore_file(const std::string& filename) {
".o", ".obj", ".a", ".lib" ".o", ".obj", ".a", ".lib"
}; };
// Ignore files starting with '.' or '_'
if (!filename.empty() && (filename[0] == '.' || filename[0] == '_')) { if (!filename.empty() && (filename[0] == '.' || filename[0] == '_')) {
return true; return true;
} }
// Ignore files with specific extensions
fs::path path(filename); fs::path path(filename);
std::string ext = path.extension().string(); std::string ext = path.extension().string();
if (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()) {
@ -45,55 +42,44 @@ bool TreeStructurer::should_ignore_file(const std::string& filename) {
std::vector<fs::path> TreeStructurer::get_filtered_paths(const fs::path& start) { std::vector<fs::path> TreeStructurer::get_filtered_paths(const fs::path& start) {
std::vector<fs::path> paths; std::vector<fs::path> paths;
fs::directory_options options = fs::directory_options::skip_permission_denied;
for (const auto& entry : fs::recursive_directory_iterator(start)) { try {
const auto& path = entry.path(); if (fs::exists(start) && fs::is_directory(start)) {
paths.push_back(start);
// Skip any path that contains an ignored directory for (const auto& entry : fs::recursive_directory_iterator(start, options)) {
bool contains_ignored_dir = false; const auto& path = entry.path();
for (auto it = path.begin(); it != path.end(); ++it) {
if (should_ignore_dir(it->string())) { // Check if any parent directory should be ignored
contains_ignored_dir = true; bool should_skip = false;
break; for (const auto& component : path) {
} if (should_ignore_dir(component.string())) {
} should_skip = true;
break;
if (contains_ignored_dir) { }
continue; }
} if (should_skip) continue;
// Handle directories if (entry.is_directory()) {
if (fs::is_directory(path)) { if (should_ignore_dir(path.filename().string())) {
if (!should_ignore_dir(path.filename().string())) { paths.push_back(path);
paths.push_back(path); }
} } else {
} if (should_ignore_file(path.filename().string())) {
// Handle files paths.push_back(path);
else { }
if (!should_ignore_file(path.filename().string())) { }
paths.push_back(path);
} }
} }
} catch (const fs::filesystem_error& e) {
throw std::runtime_error("Error accessing path: " + std::string(e.what()));
} }
std::sort(paths.begin(), paths.end()); std::sort(paths.begin(), paths.end());
return paths; 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) { std::string TreeStructurer::get_relative_path(const fs::path& path, const fs::path& base) {
fs::path rel = fs::relative(path, base); fs::path rel = fs::relative(path, base);
return rel.string(); return rel.string();
@ -102,26 +88,50 @@ std::string TreeStructurer::get_relative_path(const fs::path& path, const fs::pa
std::vector<std::string> TreeStructurer::get_directory_structure(const std::string& startpath) { std::vector<std::string> TreeStructurer::get_directory_structure(const std::string& startpath) {
std::vector<std::string> result; std::vector<std::string> result;
fs::path start = startpath.empty() ? fs::current_path() : fs::path(startpath); fs::path start = startpath.empty() ? fs::current_path() : fs::path(startpath);
try { try {
result.push_back(start.filename().string() + "/");
auto paths = get_filtered_paths(start); auto paths = get_filtered_paths(start);
if (paths.empty()) return result;
// Don't add the root directory name if we're in the current directory
if (start != fs::current_path()) {
result.push_back(start.filename().string());
}
std::vector<bool> is_last_at_level(256, false); std::vector<bool> is_last_at_level(256, false);
std::string last_prefix;
for (size_t i = 0; i < paths.size(); ++i) {
for (size_t i = 1; i < paths.size(); ++i) {
const auto& path = paths[i]; const auto& path = paths[i];
std::string rel_path = get_relative_path(path, start); std::string rel_path = get_relative_path(path, start);
// Split the path into components and check each component
bool should_skip = false;
for (const auto& component : fs::path(rel_path)) {
if (should_ignore_dir(component.string())) {
should_skip = true;
break;
}
}
if (should_skip) continue;
int level = std::count(rel_path.begin(), rel_path.end(), fs::path::preferred_separator); int level = std::count(rel_path.begin(), rel_path.end(), fs::path::preferred_separator);
bool is_last = (i == paths.size() - 1) || // Check if this is the last item at this level
(i < paths.size() - 1 && bool is_last = false;
std::count(get_relative_path(paths[i + 1], start).begin(), for (size_t j = i + 1; j < paths.size(); j++) {
get_relative_path(paths[i + 1], start).end(), const auto& next_path = paths[j];
fs::path::preferred_separator) <= level); std::string next_rel_path = get_relative_path(next_path, start);
int next_level = std::count(next_rel_path.begin(), next_rel_path.end(), fs::path::preferred_separator);
if (next_level <= level) {
is_last = (j == paths.size() - 1) || (next_level < level);
break;
}
}
if (i == paths.size() - 1) is_last = true;
is_last_at_level[level] = is_last; is_last_at_level[level] = is_last;
std::string line; std::string line;
for (int j = 0; j < level; ++j) { for (int j = 0; j < level; ++j) {
if (j == level - 1) { if (j == level - 1) {
@ -130,9 +140,9 @@ std::vector<std::string> TreeStructurer::get_directory_structure(const std::stri
line += is_last_at_level[j] ? " " : ""; line += is_last_at_level[j] ? " " : "";
} }
} }
line += path.filename().string(); line += path.filename().string();
if(fs::is_directory(path)) { if (fs::is_directory(path)) {
line += "/"; line += "/";
} }
result.push_back(line); result.push_back(line);
@ -140,6 +150,6 @@ std::vector<std::string> TreeStructurer::get_directory_structure(const std::stri
} catch (const fs::filesystem_error& e) { } catch (const fs::filesystem_error& e) {
throw std::runtime_error("Failed to access directory: " + std::string(e.what())); throw std::runtime_error("Failed to access directory: " + std::string(e.what()));
} }
return result; return result;
} }