Merge pull request 'extended_cpp' (#2) from extended_cpp into develop
Reviewed-on: Fabel/Tree-Structurer#2
This commit is contained in:
commit
6be8d61a6a
|
@ -0,0 +1,3 @@
|
|||
import tree_structurer
|
||||
|
||||
tree_structurer.get_structure()
|
2
setup.py
2
setup.py
|
@ -20,7 +20,7 @@ tree_structurer_module = Extension(
|
|||
|
||||
setup(
|
||||
name='tree_structurer',
|
||||
version='0.0.2',
|
||||
version='0.0.5',
|
||||
description='A module for analyzing directory structures',
|
||||
ext_modules=[tree_structurer_module],
|
||||
packages=['tree_structurer'],
|
||||
|
|
|
@ -14,12 +14,10 @@ def main():
|
|||
try:
|
||||
if args.verbose:
|
||||
print(f"Analyzing directory: {args.path}")
|
||||
|
||||
# Create tree structurer instance
|
||||
create_tree_structurer()
|
||||
|
||||
# Get and print structure
|
||||
# Get and print structure directly
|
||||
structure = get_structure(args.path)
|
||||
print(f"dir Structure for {args.path}:")
|
||||
for line in structure:
|
||||
print(line)
|
||||
|
||||
|
|
|
@ -5,56 +5,151 @@
|
|||
namespace fs = std::filesystem;
|
||||
|
||||
bool TreeStructurer::should_ignore_dir(const std::string& dirname) {
|
||||
return dirname[0] == '.' ||
|
||||
dirname.find("build") != std::string::npos ||
|
||||
dirname == "venv" ||
|
||||
dirname == "myenv";
|
||||
static const std::vector<std::string> ignore_list = {
|
||||
"build", "venv", "myenv", "dist", "node_modules", "CMakeFiles",
|
||||
".git", ".idea", ".vscode", "__pycache__"
|
||||
};
|
||||
|
||||
if (std::find(ignore_list.begin(), ignore_list.end(), dirname) != ignore_list.end()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!dirname.empty() && (dirname[0] == '.' || dirname[0] == '_')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TreeStructurer::should_ignore_file(const std::string& filename) {
|
||||
return filename[0] == '.' ||
|
||||
filename.find("build") != std::string::npos;
|
||||
static const std::vector<std::string> ignore_extensions = {
|
||||
".pyc", ".pyo", ".pyd", ".so", ".dll", ".dylib",
|
||||
".o", ".obj", ".a", ".lib"
|
||||
};
|
||||
|
||||
if (!filename.empty() && (filename[0] == '.' || filename[0] == '_')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
fs::path path(filename);
|
||||
std::string ext = path.extension().string();
|
||||
if (std::find(ignore_extensions.begin(), ignore_extensions.end(), ext) != ignore_extensions.end()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string TreeStructurer::create_indent(int level) {
|
||||
return std::string(4 * level, ' ');
|
||||
std::vector<fs::path> TreeStructurer::get_filtered_paths(const fs::path& start) {
|
||||
std::vector<fs::path> paths;
|
||||
fs::directory_options options = fs::directory_options::skip_permission_denied;
|
||||
|
||||
try {
|
||||
if (fs::exists(start) && fs::is_directory(start)) {
|
||||
paths.push_back(start);
|
||||
|
||||
for (const auto& entry : fs::recursive_directory_iterator(start, options)) {
|
||||
const auto& path = entry.path();
|
||||
|
||||
// Check if any parent directory should be ignored
|
||||
bool should_skip = false;
|
||||
for (const auto& component : path) {
|
||||
if (should_ignore_dir(component.string())) {
|
||||
should_skip = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (should_skip) continue;
|
||||
|
||||
if (entry.is_directory()) {
|
||||
if (should_ignore_dir(path.filename().string())) {
|
||||
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());
|
||||
return paths;
|
||||
}
|
||||
|
||||
std::string TreeStructurer::get_relative_path(const fs::path& path, const fs::path& base) {
|
||||
const fs::path rel = fs::relative(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 {
|
||||
std::vector<fs::path> paths;
|
||||
for(const auto& entry : fs::recursive_directory_iterator(start)) {
|
||||
paths.push_back(entry.path());
|
||||
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());
|
||||
}
|
||||
|
||||
// Sort paths for consistent output
|
||||
std::sort(paths.begin(), paths.end());
|
||||
|
||||
for(const auto& path : paths) {
|
||||
|
||||
std::vector<bool> is_last_at_level(256, false);
|
||||
std::string last_prefix;
|
||||
|
||||
for (size_t i = 1; 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);
|
||||
|
||||
if(fs::is_directory(path)) {
|
||||
if(!should_ignore_dir(path.filename().string())) {
|
||||
result.push_back(create_indent(level) + path.filename().string() + "/");
|
||||
}
|
||||
} else {
|
||||
if(!should_ignore_file(path.filename().string())) {
|
||||
result.push_back(create_indent(level) + path.filename().string());
|
||||
|
||||
// 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);
|
||||
|
||||
// Check if this is the last item at this level
|
||||
bool is_last = false;
|
||||
for (size_t j = i + 1; j < paths.size(); j++) {
|
||||
const auto& next_path = paths[j];
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,4 +13,5 @@ private:
|
|||
bool should_ignore_file(const std::string& filename);
|
||||
std::string create_indent(int level);
|
||||
std::string get_relative_path(const std::filesystem::path& path, const std::filesystem::path& base);
|
||||
std::vector<std::filesystem::path> get_filtered_paths(const std::filesystem::path& start);
|
||||
};
|
Loading…
Reference in New Issue