diff --git a/NEWS.md b/NEWS.md index bb4f7376..abd566ce 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # cli (development version) +* `cli_progress_bar()` has a new argument `quiet`. If TRUE, then progress bar will be suppressed. + * `keypress()` improvements: - `timeout` argument to wait at most a given number of seconds for a key press. diff --git a/R/progress-client.R b/R/progress-client.R index edae4463..83df8d13 100644 --- a/R/progress-client.R +++ b/R/progress-client.R @@ -282,6 +282,8 @@ #' @param current Whether to use this progress bar as the current progress #' bar of the calling function. See more at 'The current progress bar' #' below. +#' @param quiet if `TRUE` suppresses output from this function. Defaults to the `cli.disable_progress` option, or +#' `TRUE` if unset. #' @param auto_terminate Whether to terminate the progress bar if the #' number of current units reaches the number of total units. #' @param extra Extra data to add to the progress bar. This can be @@ -320,6 +322,7 @@ cli_progress_bar <- function( format_failed = NULL, clear = getOption("cli.progress_clear", TRUE), current = TRUE, + quiet = getOption("cli.disable_progress", FALSE), auto_terminate = type != "download", extra = NULL, .auto_close = TRUE, @@ -359,7 +362,7 @@ cli_progress_bar <- function( bar$extra <- extra clienv$progress[[id]] <- bar if (current) { - if (!is.null(clienv$progress_ids[[envkey]])) { + if (!is.null(clienv$progress_ids[[envkey]]) && !quiet) { cli_progress_done( clienv$progress_ids[[envkey]], .envir = .envir, @@ -369,19 +372,21 @@ cli_progress_bar <- function( clienv$progress_ids[[envkey]] <- id } - if (.auto_close && envkey != clienv$globalenv) { - defer( - cli_progress_done(id = id, .envir = .envir, result = "auto"), - envir = .envir - ) - } + if(!quiet){ + if (.auto_close && envkey != clienv$globalenv) { + defer( + cli_progress_done(id = id, .envir = .envir, result = "auto"), + envir = .envir + ) + } - opt <- options(cli__pb = bar) - on.exit(options(opt), add = TRUE) + opt <- options(cli__pb = bar) + on.exit(options(opt), add = TRUE) - bar$handlers <- cli_progress_select_handlers(bar, .envir) - for (h in bar$handlers) { - if ("create" %in% names(h)) h$create(bar, .envir = .envir) + bar$handlers <- cli_progress_select_handlers(bar, .envir) + for (h in bar$handlers) { + if ("create" %in% names(h)) h$create(bar, .envir = .envir) + } } invisible(id) diff --git a/tests/testthat/test-progress-client.R b/tests/testthat/test-progress-client.R index f14cfaf9..ebb17652 100644 --- a/tests/testthat/test-progress-client.R +++ b/tests/testthat/test-progress-client.R @@ -205,3 +205,21 @@ test_that("cli_progress_bar handles Inf like NA", { capture_cli_messages(fun(total = Inf)) ) }) + +test_that("no progress bar when quiet = TRUE", { + + fun <- function() { + bar <- cli_progress_bar( + name = "name", + quiet = TRUE + ) + for (i in 1:100) { + Sys.sleep(5/100) + cli_progress_update(id = bar) + } + } + + # No messages to display + expect_snapshot(capture_cli_messages(fun())) + }) +