Skip to content

Commit

Permalink
Make dir_tree() work even when glob or regexp is used
Browse files Browse the repository at this point in the history
This PR addresses the issue described in r-lib#398. `dir_tree()` didn't work when a pattern was supplied to `regexp=` or `glob=` arguments that get passed on to `dir_ls()`. That's because the list of file paths returned by those calls to `dir_ls()` did not include paths of the parent (and grandparent etc.) directories that `dir_tree()` uses to construct the tree.

The code I've added uses the file paths returned by `dir_ls()` to recreate the directory paths that'll be needed to build the tree.

To see that this now works, try for example:

```r
dir_tree(system.file(package = "fs"), regexp = "[.]rds$")
dir_tree(system.file(package = "fs"), type = "directory")
```
  • Loading branch information
JoshOBrien committed Feb 5, 2024
1 parent 03435bc commit 3dc99c0
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions R/tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
#'
#' @export
dir_tree <- function(path = ".", recurse = TRUE, ...) {
find_ancestor_dirs <- function(path) {
parts <- path_split(path)[[1]]
Reduce(function(x, y) path_join(list(c(x, y))),
parts, accumulate = TRUE)
}

files <- dir_ls(path, recurse = recurse, ...)
dd <- unlist(sapply(path_dir(files), find_ancestor_dirs))
dd <- setdiff(dd, path)
files <- unique(c(dd, files))
by_dir <- split(files, path_dir(files))

ch <- box_chars()
Expand Down

0 comments on commit 3dc99c0

Please sign in to comment.