diff --git a/NEWS.md b/NEWS.md index 2731f85b..f80bbe9d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -43,6 +43,10 @@ * `ansi_collapse(style = "head")` no longer exceeds `width` when not even the first element fits (#570, @taekop). +* `cli_progress_update(set = )` no longer fails with a deferred `on.exit()` + error when the progress bar is drawn with a value below zero or above + the total (#580, @taekop). + # cli 3.6.6 * New `{.num}` and `{.bytes}` inline styles to format numbers diff --git a/R/progress-bar.R b/R/progress-bar.R index 616d6a61..e4ede296 100644 --- a/R/progress-bar.R +++ b/R/progress-bar.R @@ -1,5 +1,5 @@ make_progress_bar <- function(percent, width = 30, style = list()) { - complete_len <- round(width * percent) + complete_len <- round(width * min(max(percent, 0), 1)) def <- default_progress_style() chr_complete <- style[["progress-complete"]] %||% def[["complete"]] diff --git a/tests/testthat/test-progress-client.R b/tests/testthat/test-progress-client.R index f14cfaf9..f2420cf4 100644 --- a/tests/testthat/test-progress-client.R +++ b/tests/testthat/test-progress-client.R @@ -127,6 +127,17 @@ test_that("update set", { expect_snapshot(capture_cli_messages(fun())) }) +test_that("update set outside of 0..total", { + withr::local_options(cli.dynamic = FALSE, cli.ansi = FALSE) + fun <- function(set) { + bar <- cli_progress_bar(total = 10, format = "{cli::pb_bar}") + cli_progress_update(force = TRUE, set = set) + cli_progress_done(id = bar) + } + expect_no_error(capture_cli_messages(fun(-1))) + expect_no_error(capture_cli_messages(fun(11))) +}) + test_that("format changes if we (un)learn total", { withr::local_options(cli.dynamic = FALSE, cli.ansi = FALSE) fun <- function() {