Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update behavior when total = Inf to be same as NA #722

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)))
})
Loading