diff --git a/NEWS.md b/NEWS.md index 3885d88a..5561d0b4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # cli (development version) +* `cli_progress_bar()` now accepts `total` = Inf or -Inf which mimics the behavior of when `total` is NA. + * `num_ansi_colors()` now does not warn in Emacs if the `INSIDE_EMACS` environment variable is not a proper version number (@rundel, #689). diff --git a/R/progress-client.R b/R/progress-client.R index b153b5f2..6e654139 100644 --- a/R/progress-client.R +++ b/R/progress-client.R @@ -325,6 +325,11 @@ cli_progress_bar <- function(name = NULL, stop("Need to specify format if `type == \"custom\"") } + ## If `total` is infinite, use behavior seen when `total` is NA + if (is.infinite(total)) { + total <- NA + } + ## If changes, synchronize with C API in progress.c bar <- new.env(parent = emptyenv()) bar$id <- id diff --git a/tests/testthat/test-progress-client.R b/tests/testthat/test-progress-client.R index 0e860ba1..210d5b74 100644 --- a/tests/testthat/test-progress-client.R +++ b/tests/testthat/test-progress-client.R @@ -50,7 +50,7 @@ test_that("update errors if no progress bar", { cli_progress_output("boo") } expect_error(fun(), "Cannot find current progress bar") - + envkey <- NULL fun <- function() { envkey <<- format(environment()) @@ -164,5 +164,19 @@ test_that("cli_progress_output", { expect_snapshot(capture_cli_messages(fun())) withr::local_options(cli.dynamic = TRUE, cli.ansi = FALSE) - expect_snapshot(capture_cli_messages(fun())) + expect_snapshot(capture_cli_messages(fun())) +}) + +test_that("cli_progress_bar handles Inf like NA", { + withr::local_options(cli.dynamic = FALSE, cli.ansi = FALSE) + fun <- function(total) { + bar <- cli_progress_bar( + name = "name", + format = "{cli::pb_name}{cli::pb_current}", + total = total + ) + cli_progress_update(force = TRUE) + cli_progress_done(id = bar) + } + expect_equal(capture_cli_messages(fun(total = NA)), capture_cli_messages(fun(total = Inf))) })