-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Directories are shown even if they don’t contain files with the specified extension #14
Comments
Hey @zyadtaha Yep, I know! There's a bigger update coming, and you can work on this and add an option for filtering directories based on file extension presence. |
Just to confirm, do you mean adding an automatic exclusion where directories without any files of the specified extension are filtered out by default? |
I think your approach here is correct |
After some thought, here is my approach: I will initialize a FIFO queue, and as I traverse the directory structure, I will add the file names to it. For directories, I will also add them to the queue and then check if the number of files (with the specified extension) added before and after traversing the directory is the same. If so, I will remove the directory from the queue and proceed. At the end, all the elements of the queue will be printed. The advantage of this approach is that the tree structure is printed in a single pass. However, determining the last valid entry (to ensure proper tree prefix and indentation) becomes tricky (see the picture below) because we can't be sure whether it's the last valid entry when it's added to the queue. I thought of this workaround: creating a function to calculate the number of entries within each directory to determine if an entry is the last valid one. This will ensure that the correct tree prefix and indentation are used. The downside is that it will require traversing the structure twice (once to gather the data and once to print the tree). What do you think? @Ahmedhossamdev |
Hey @zyadtaha Your FIFO queue idea is cool, and I like how it handles traversal in one pass. Checking file counts to filter empty directories is smart. The challenge with indentation is real, though. Your two-pass workaround makes sense—it ensures the tree looks right. If performance is a concern, maybe cache data during the first pass to speed up the second one? Another thought: have you considered using a stack? It could track depth and last entries dynamically, avoiding a second pass. Just an idea! We can try both approaches and optimize them later. Let me know what you think! |
Hey @Ahmedhossamdev, sorry for the late reply. I was busy the past few days.
By caching data, do you mean caching whether a directory is valid (has at least one file with the specified extension)? Regarding the stack, I tried to think about how to traverse the directory only once using a stack while also handling the printing of the structure and indentation, but I couldn't figure out how to implement it. Any help would be appreciated! |
@zyadtaha I'm thinking about it |
I think we can start by implementing the two-pass solution, and if we notice any performance bottlenecks, we can brainstorm optimizations later. What do you think? @Ahmedhossamdev |
Sorry for replyinging late, You can try it, of course, Don't worry about performance for now |
When using the --ext flag to filter files by extension, the tool displays directories even if they don’t contain any files with the specified extension.
For example, when running go run ./cmd/main.go --ext .md, the entire directory structure is being shown, including directories like assets/ and cmd/, which don’t contain any .md files.
Wouldn't it be better to only display directories that contain at least one file with the specified extension?
If that makes sense to you, let me know to start working on it.
The text was updated successfully, but these errors were encountered: