Skip to content

Commit

Permalink
Merge pull request #722 from LouisMPenrod/handle-progress-type-inf
Browse files Browse the repository at this point in the history
Update behavior when `total` = Inf to be same as NA
  • Loading branch information
gaborcsardi committed Aug 28, 2024
2 parents 69150b1 + a57b201 commit e55ae2c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).

Expand Down
5 changes: 5 additions & 0 deletions R/progress-client.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions tests/testthat/test-progress-client.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)))
})

0 comments on commit e55ae2c

Please sign in to comment.