diff --git a/src/prodir/cpp/tree_structurer.cpp b/src/prodir/cpp/tree_structurer.cpp index 6a961d9..ad33960 100644 --- a/src/prodir/cpp/tree_structurer.cpp +++ b/src/prodir/cpp/tree_structurer.cpp @@ -138,70 +138,50 @@ std::vector TreeStructurer::get_directory_structure(const std::stri if (!fs::is_directory(start)) { throw std::runtime_error("Path is not a directory: " + start.string()); } + + std::vector paths = get_filtered_paths(start); + std::vector is_last_per_level; - // Get all paths and sort them - std::vector paths; - std::map> dir_contents; - - // First, collect all paths - for (const auto& entry : fs::directory_iterator(start)) { - fs::path path = entry.path(); - if (entry.is_directory()) { - if (!should_ignore_dir(path.filename().string())) { - paths.push_back(path); - // Collect contents of this directory - for (const auto& subentry : fs::directory_iterator(path)) { - if (!should_ignore_file(subentry.path().filename().string()) && - !should_ignore_dir(subentry.path().filename().string())) { - dir_contents[path].push_back(subentry.path()); - } - } - } - } else { - if (!should_ignore_file(path.filename().string())) { - paths.push_back(path); - } + // Skip the first path as it's the root + for (size_t i = 1; i < paths.size(); ++i) { + fs::path relative = fs::relative(paths[i], start); + std::vector components; + for (const auto& comp : relative) { + components.push_back(comp.string()); } - } - - // Sort paths - std::sort(paths.begin(), paths.end()); - 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])) { - name += "/"; + // Calculate the current level + size_t level = components.size() - 1; + + // Adjust is_last_per_level vector size + while (is_last_per_level.size() <= level) { + is_last_per_level.push_back(false); } - line += name; + + // 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] ? " " : "│ "; + } + } + + // Add the file/directory name + line += components.back(); + if (fs::is_directory(paths[i])) { + 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) { @@ -211,7 +191,6 @@ std::vector TreeStructurer::get_directory_structure(const std::stri return result; } - // ----------------------------------------------------------------------------- // Structure Creation from a Tree-like File or String // ----------------------------------------------------------------------------- @@ -453,8 +432,6 @@ std::vector TreeStructurer::read_structure_file(const std::string& line.pop_back(); } - std::cout << "Processing line: " << line << std::endl; - size_t hash_pos = line.find('#'); size_t single_line_comment_pos = line.find("//"); size_t multi_line_comment_start_pos = line.find("/*"); @@ -462,15 +439,12 @@ std::vector TreeStructurer::read_structure_file(const std::string& if (hash_pos != std::string::npos) { // Trim the line at the hash comment 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) { // Trim the line at the single-line comment 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) { // Trim the line at the multi-line comment start 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 @@ -479,9 +453,7 @@ std::vector TreeStructurer::read_structure_file(const std::string& if (!line.empty()) { lines.push_back(line); - std::cout << "Added non-empty line: " << line << std::endl; } else { - std::cout << "Skipped empty or commented-out line." << std::endl; } }