Skip to content

Commit

Permalink
moreOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
stla committed Apr 24, 2024
1 parent 4cedc15 commit 68c6e4b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 34 deletions.
10 changes: 5 additions & 5 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Files without base name (such as `.gitignore`) were excluded from the search.
This is no longer the case.

* Hidden folders were not excluded from the search when running a recursive
search (i.e. `depth` is `NULL` or a negative integer). Now they are always
excluded.
* Hidden folders (those whose name starts with a dot) were not excluded from
the search when running a full recursive search (i.e. `depth` is `NULL` or a
negative integer). Now they are always excluded.

* The argument `ext` of the `findInFiles` function has been renamed to
`extensions` and it can be a vector of file extensions now.
Expand All @@ -30,8 +30,8 @@ user.
* The `findInFiles` function has now an alias function named `fif`.

* Since I most often use the `findInFiles` function to search in R files, I
added the function `fifR` which is the same as `findInFiles` with the `ext`
argument set to `"R"`.
added the function `fifR` which is the same as `findInFiles` with the
`extensions` argument set to `"R"`.


# findInFiles 0.4.0
Expand Down
16 changes: 12 additions & 4 deletions R/findInFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#' @name findInFiles
#' @rdname findInFiles
#'
#' @param extensions extension(s) of the files to include in the search,
#' e.g. \code{"R"} or \code{"js"} or \code{c("R", "Rmd")}
#' @param extensions extension(s) of the files to include in the search
#' (case-sensitive), e.g. \code{"R"} or \code{c("R", "Rmd")}, or \code{"*"}
#' to search in all files
#' @param pattern pattern to search for, a regular expression, e.g.
#' \code{"function"} or \code{"^function"}
#' @param depth depth of the search, \code{NULL} or a negative number for an
Expand All @@ -25,7 +26,9 @@
#' are found (so there is no gain of efficiency)
#' @param wholeWord logical, whether to match the whole pattern
#' @param ignoreCase logical, whether to ignore the case
#' @param perl logical, whether \code{pattern} is a Perl regular expression
#' @param perl logical, whether the pattern given in the \code{pattern}
#' argument is a Perl regular expression; useful to search for multiple
#' patterns: set \code{perl=TRUE} and \code{pattern="(pattern1|pattern2|...)"}
#' @param includePattern this argument is ignored if \code{depth} is not a
#' positive integer; it must be a pattern or a vector of patterns, and only
#' the files whose name matches this pattern or one of these patterns will be
Expand All @@ -36,10 +39,13 @@
#' @param excludeFoldersPattern a pattern or a vector of patterns; folders
#' whose name matches this pattern or one of these patterns will be excluded
#' from search
#' @param moreOptions additional options passed to the \code{grep} command,
#' for \code{grep} experts
#' @param root path to the root directory to search from
#' @param output one of \code{"viewer"}, \code{"tibble"} or
#' \code{"viewer+tibble"}; see examples
#' @param ... arguments other than \code{ext} passed to \code{findInFiles}
#' @param ... arguments other than \code{extensions} passed to
#' \code{findInFiles}
#'
#' @return A tibble if \code{output="tibble"}, otherwise a
#' \code{htmlwidget} object.
Expand Down Expand Up @@ -71,6 +77,7 @@ findInFiles <- function(
wholeWord = FALSE, ignoreCase = FALSE, perl = FALSE,
includePattern = NULL,
excludePattern = NULL, excludeFoldersPattern = NULL,
moreOptions = NULL,
root = ".", output = "viewer"
){

Expand Down Expand Up @@ -115,6 +122,7 @@ findInFiles <- function(
includePattern = includePattern,
excludePattern = excludePattern,
excludeFoldersPattern = excludeFoldersPattern,
moreOptions = moreOptions,
directory = root, output = output
)

Expand Down
35 changes: 14 additions & 21 deletions R/grepInFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ grepInFiles <- function(
ext, pattern, depth, maxCountPerFile, maxCount,
wholeWord, ignoreCase, perl,
includePattern, excludePattern, excludeFoldersPattern,
moreOptions,
directory, output
){
if(inSolaris()){
Expand All @@ -39,10 +40,11 @@ grepInFiles <- function(
stopifnot(isBoolean(perl))
wd <- setwd(directory)
on.exit(setwd(wd))
opts <- c("-n", "-I", "--with-filename", moreOptions)
if(output == "tibble"){
opts <- c("--colour=never", "-n", "--with-filename")
opts <- c(opts, "--colour=never")
}else{
opts <- c("--colour=always", "-n", "--with-filename")
opts <- c(opts, "--colour=always")
}
if(!is.null(maxCountPerFile)) {
stopifnot(isPositiveInteger(maxCountPerFile))
Expand Down Expand Up @@ -78,32 +80,23 @@ grepInFiles <- function(
}, character(1L))
)
}
if(!is.null(excludeFoldersPattern)){
check <- all(vapply(excludeFoldersPattern, isString, logical(1L)))
if(!check) {
stop("Invalid argument `excludeFoldersPattern`.")
}
opts <- c(
opts,
vapply(excludeFoldersPattern, function(pattern) {
paste0("--exclude-dir=", shQuote(pattern))
}, character(1L))
)
excludeFoldersPattern <- c(".*", excludeFoldersPattern) # skip hidden folders
check <- all(vapply(excludeFoldersPattern, isString, logical(1L)))
if(!check) {
stop("Invalid argument `excludeFoldersPattern`.")
}
# return(system2(
# "grep",
# args = c(
# paste0("--include=\\*\\.", ext), opts, "-r", "-e", shQuote(pattern)
# ),
# stdout = TRUE, stderr = TRUE
# ))
opts <- c(
opts,
vapply(excludeFoldersPattern, function(pattern) {
paste0("--exclude-dir=", shQuote(pattern))
}, character(1L))
)
command <- ifelse(inSolaris(), "ggrep", "grep")
results <- if(is.null(depth) || depth < 0){
suppressWarnings(system2(
command,
args = c(
paste0("--include=\\*\\.", ext),
paste0("--exclude-dir=", shQuote(".*")),
opts, "-r", "-e", shQuote(pattern)
),
stdout = TRUE, stderr = TRUE
Expand Down
17 changes: 13 additions & 4 deletions man/findInFiles.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 68c6e4b

Please sign in to comment.