From 68c6e4b621b5d661b0934c688b7c4f797db08dcd Mon Sep 17 00:00:00 2001 From: stla Date: Wed, 24 Apr 2024 22:46:04 +0200 Subject: [PATCH] moreOptions --- NEWS.md | 10 +++++----- R/findInFiles.R | 16 ++++++++++++---- R/grepInFiles.R | 35 ++++++++++++++--------------------- man/findInFiles.Rd | 17 +++++++++++++---- 4 files changed, 44 insertions(+), 34 deletions(-) diff --git a/NEWS.md b/NEWS.md index 3574633..f743dbc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. @@ -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 diff --git a/R/findInFiles.R b/R/findInFiles.R index b90b728..7ff570d 100644 --- a/R/findInFiles.R +++ b/R/findInFiles.R @@ -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 @@ -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 @@ -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. @@ -71,6 +77,7 @@ findInFiles <- function( wholeWord = FALSE, ignoreCase = FALSE, perl = FALSE, includePattern = NULL, excludePattern = NULL, excludeFoldersPattern = NULL, + moreOptions = NULL, root = ".", output = "viewer" ){ @@ -115,6 +122,7 @@ findInFiles <- function( includePattern = includePattern, excludePattern = excludePattern, excludeFoldersPattern = excludeFoldersPattern, + moreOptions = moreOptions, directory = root, output = output ) diff --git a/R/grepInFiles.R b/R/grepInFiles.R index 928c205..173cee5 100644 --- a/R/grepInFiles.R +++ b/R/grepInFiles.R @@ -18,6 +18,7 @@ grepInFiles <- function( ext, pattern, depth, maxCountPerFile, maxCount, wholeWord, ignoreCase, perl, includePattern, excludePattern, excludeFoldersPattern, + moreOptions, directory, output ){ if(inSolaris()){ @@ -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)) @@ -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 diff --git a/man/findInFiles.Rd b/man/findInFiles.Rd index a3d55e4..2870af0 100644 --- a/man/findInFiles.Rd +++ b/man/findInFiles.Rd @@ -18,6 +18,7 @@ findInFiles( includePattern = NULL, excludePattern = NULL, excludeFoldersPattern = NULL, + moreOptions = NULL, root = ".", output = "viewer" ) @@ -34,6 +35,7 @@ fif( includePattern = NULL, excludePattern = NULL, excludeFoldersPattern = NULL, + moreOptions = NULL, root = ".", output = "viewer" ) @@ -41,8 +43,9 @@ fif( fifR(...) } \arguments{ -\item{extensions}{extension(s) of the files to include in the search, -e.g. \code{"R"} or \code{"js"} or \code{c("R", "Rmd")}} +\item{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} \item{pattern}{pattern to search for, a regular expression, e.g. \code{"function"} or \code{"^function"}} @@ -67,7 +70,9 @@ are found (so there is no gain of efficiency)} \item{ignoreCase}{logical, whether to ignore the case} -\item{perl}{logical, whether \code{pattern} is a Perl regular expression} +\item{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|...)"}} \item{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 @@ -82,12 +87,16 @@ from search} whose name matches this pattern or one of these patterns will be excluded from search} +\item{moreOptions}{additional options passed to the \code{grep} command, +for \code{grep} experts} + \item{root}{path to the root directory to search from} \item{output}{one of \code{"viewer"}, \code{"tibble"} or \code{"viewer+tibble"}; see examples} -\item{...}{arguments other than \code{ext} passed to \code{findInFiles}} +\item{...}{arguments other than \code{extensions} passed to +\code{findInFiles}} } \value{ A tibble if \code{output="tibble"}, otherwise a