From f09d5c046e4e403b4d71978d94d177fe97d59619 Mon Sep 17 00:00:00 2001 From: Louismp1223 <louismpenrod@gmail.com> Date: Thu, 15 Aug 2024 14:24:17 -0700 Subject: [PATCH] Use same behavior in cli_progress_bar when total = Inf as when total = NA Add test to check same behavior Update NEWS Fixes #630 --- NEWS.md | 2 ++ R/progress-client.R | 5 +++++ tests/testthat/test-progress-client.R | 18 ++++++++++++++++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 1d7f4ca47..b33639b16 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. + # cli 3.6.3 * cli now builds on ARM Windows. diff --git a/R/progress-client.R b/R/progress-client.R index b153b5f24..6e654139b 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 0e860ba10..210d5b746 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))) })