updated folder printing with better loooking ui and removed debu prints

This commit is contained in:
Falko Victor Habel 2025-04-22 17:07:30 +02:00
parent e230128be0
commit 2a034e9c0e
1 changed files with 39 additions and 67 deletions

View File

@ -139,69 +139,49 @@ std::vector<std::string> TreeStructurer::get_directory_structure(const std::stri
throw std::runtime_error("Path is not a directory: " + start.string()); throw std::runtime_error("Path is not a directory: " + start.string());
} }
// Get all paths and sort them std::vector<fs::path> paths = get_filtered_paths(start);
std::vector<fs::path> paths; std::vector<bool> is_last_per_level;
std::map<fs::path, std::vector<fs::path>> dir_contents;
// First, collect all paths // Skip the first path as it's the root
for (const auto& entry : fs::directory_iterator(start)) { for (size_t i = 1; i < paths.size(); ++i) {
fs::path path = entry.path(); fs::path relative = fs::relative(paths[i], start);
if (entry.is_directory()) { std::vector<std::string> components;
if (!should_ignore_dir(path.filename().string())) { for (const auto& comp : relative) {
paths.push_back(path); components.push_back(comp.string());
// Collect contents of this directory }
for (const auto& subentry : fs::directory_iterator(path)) {
if (!should_ignore_file(subentry.path().filename().string()) && // Calculate the current level
!should_ignore_dir(subentry.path().filename().string())) { size_t level = components.size() - 1;
dir_contents[path].push_back(subentry.path());
} // Adjust is_last_per_level vector size
} while (is_last_per_level.size() <= level) {
} is_last_per_level.push_back(false);
} else { }
if (!should_ignore_file(path.filename().string())) {
paths.push_back(path); // Determine if this is the last item at its level
bool is_last = (i == paths.size() - 1) ||
(i + 1 < paths.size() &&
fs::relative(paths[i + 1], start).begin()->string() != components[0]);
is_last_per_level[level] = is_last;
// Build the line prefix
std::string line;
for (size_t j = 0; j < level; ++j) {
if (j == level - 1) {
line += is_last ? "└── " : "├── ";
} else {
line += is_last_per_level[j] ? " " : "";
} }
} }
}
// Sort paths // Add the file/directory name
std::sort(paths.begin(), paths.end()); line += components.back();
for (auto& [dir, contents] : dir_contents) {
std::sort(contents.begin(), contents.end());
}
// Add root directory
std::string root_name = start.filename().string();
result.push_back(root_name + "/");
// Process each path
for (size_t i = 0; i < paths.size(); ++i) {
bool is_last = (i == paths.size() - 1);
std::string line = is_last ? "└── " : "├── ";
std::string name = paths[i].filename().string();
if (fs::is_directory(paths[i])) { if (fs::is_directory(paths[i])) {
name += "/"; line += "/";
} }
line += name;
result.push_back(line); result.push_back(line);
// If it's a directory, process its contents
if (fs::is_directory(paths[i])) {
const auto& contents = dir_contents[paths[i]];
for (size_t j = 0; j < contents.size(); ++j) {
bool is_last_child = (j == contents.size() - 1);
std::string child_line = is_last ? " " : "";
child_line += is_last_child ? "└── " : "├── ";
std::string child_name = contents[j].filename().string();
if (fs::is_directory(contents[j])) {
child_name += "/";
}
child_line += child_name;
result.push_back(child_line);
}
}
} }
} catch (const fs::filesystem_error& e) { } catch (const fs::filesystem_error& e) {
@ -211,7 +191,6 @@ std::vector<std::string> TreeStructurer::get_directory_structure(const std::stri
return result; return result;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Structure Creation from a Tree-like File or String // Structure Creation from a Tree-like File or String
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -453,8 +432,6 @@ std::vector<std::string> TreeStructurer::read_structure_file(const std::string&
line.pop_back(); line.pop_back();
} }
std::cout << "Processing line: " << line << std::endl;
size_t hash_pos = line.find('#'); size_t hash_pos = line.find('#');
size_t single_line_comment_pos = line.find("//"); size_t single_line_comment_pos = line.find("//");
size_t multi_line_comment_start_pos = line.find("/*"); size_t multi_line_comment_start_pos = line.find("/*");
@ -462,15 +439,12 @@ std::vector<std::string> TreeStructurer::read_structure_file(const std::string&
if (hash_pos != std::string::npos) { if (hash_pos != std::string::npos) {
// Trim the line at the hash comment // Trim the line at the hash comment
line = line.substr(0, hash_pos); line = line.substr(0, hash_pos);
std::cout << "Trimmed line at hash comment position: " << hash_pos << std::endl;
} else if (single_line_comment_pos != std::string::npos) { } else if (single_line_comment_pos != std::string::npos) {
// Trim the line at the single-line comment // Trim the line at the single-line comment
line = line.substr(0, single_line_comment_pos); line = line.substr(0, single_line_comment_pos);
std::cout << "Trimmed line at single-line comment position: " << single_line_comment_pos << std::endl;
} else if (multi_line_comment_start_pos != std::string::npos) { } else if (multi_line_comment_start_pos != std::string::npos) {
// Trim the line at the multi-line comment start // Trim the line at the multi-line comment start
line = line.substr(0, multi_line_comment_start_pos); line = line.substr(0, multi_line_comment_start_pos);
std::cout << "Trimmed line at multi-line comment start position: " << multi_line_comment_start_pos << std::endl;
} }
// Remove leading and trailing whitespace // Remove leading and trailing whitespace
@ -479,9 +453,7 @@ std::vector<std::string> TreeStructurer::read_structure_file(const std::string&
if (!line.empty()) { if (!line.empty()) {
lines.push_back(line); lines.push_back(line);
std::cout << "Added non-empty line: " << line << std::endl;
} else { } else {
std::cout << "Skipped empty or commented-out line." << std::endl;
} }
} }