diff --git a/NEWS.md b/NEWS.md index e1a1cf4f..1013f344 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,9 @@ * New `{.num}` and `{.bytes}` inline styles to format numbers and bytes (@m-muecke, #644, #588, #643). +* `pb_elapsed` rounds to the closest second when the elapsed time exceeds + one minute (@mcol, #751). + # cli 3.6.5 * `code_highlight()` supports long strings and symbols diff --git a/R/time.R b/R/time.R index d56454e6..ece967a2 100644 --- a/R/time.R +++ b/R/time.R @@ -44,12 +44,15 @@ format_time <- local({ return(NA_character_) } + ## round to the closest second if the elapsed time exceeds one minute + digits <- if (sum(pieces[1:3])) 0 else 1 + ## handle non-NAs paste0( if (pieces[1]) paste0(pieces[1], "d "), if (pieces[2]) paste0(pieces[2], "h "), if (pieces[3]) paste0(pieces[3], "m "), - if (pieces[4]) paste0(pieces[4], "s ") + if (pieces[4]) paste0(round(pieces[4], digits), "s ") ) } approx <- trim(apply(parsed, 2, merge_pieces)) diff --git a/tests/testthat/_snaps/progress-variables.md b/tests/testthat/_snaps/progress-variables.md index 9e62b73a..fc192572 100644 --- a/tests/testthat/_snaps/progress-variables.md +++ b/tests/testthat/_snaps/progress-variables.md @@ -112,6 +112,27 @@ Output [1] "1h 5m" +--- + + Code + cli__pb_elapsed() + Output + [1] "1h 4m 13s" + +--- + + Code + cli__pb_elapsed() + Output + [1] "1h 4m 19s" + +--- + + Code + cli__pb_elapsed() + Output + [1] "58.1s" + # pb_elapsed_clock Code @@ -140,6 +161,27 @@ Output [1] "01:05:00" +--- + + Code + cli__pb_elapsed_clock() + Output + [1] "00:00:58.1" + +--- + + Code + cli__pb_elapsed_clock() + Output + [1] "01:04:13.2" + +--- + + Code + cli__pb_elapsed_clock() + Output + [1] "01:04:18.6" + # pb_elapsed_raw Code diff --git a/tests/testthat/test-progress-variables.R b/tests/testthat/test-progress-variables.R index def768bf..2afbc851 100644 --- a/tests/testthat/test-progress-variables.R +++ b/tests/testthat/test-progress-variables.R @@ -58,6 +58,16 @@ test_that("pb_elapsed", { expect_snapshot(cli__pb_elapsed()) local_mocked_bindings(.Call = function(...) 60 * 65) expect_snapshot(cli__pb_elapsed()) + + ## seconds should get rounded + local_mocked_bindings(.Call = function(...) 60 * 64.22) + expect_snapshot(cli__pb_elapsed()) + local_mocked_bindings(.Call = function(...) 60 * 64.31) + expect_snapshot(cli__pb_elapsed()) + + ## seconds should not get rounded + local_mocked_bindings(.Call = function(...) 58.1) + expect_snapshot(cli__pb_elapsed()) }) test_that("pb_elapsed_clock", { @@ -71,6 +81,14 @@ test_that("pb_elapsed_clock", { expect_snapshot(cli__pb_elapsed_clock()) local_mocked_bindings(.Call = function(...) 60 * 65) expect_snapshot(cli__pb_elapsed_clock()) + + ## seconds should not get rounded + local_mocked_bindings(.Call = function(...) 58.1) + expect_snapshot(cli__pb_elapsed_clock()) + local_mocked_bindings(.Call = function(...) 60 * 64.22) + expect_snapshot(cli__pb_elapsed_clock()) + local_mocked_bindings(.Call = function(...) 60 * 64.31) + expect_snapshot(cli__pb_elapsed_clock()) }) test_that("pb_elapsed_raw", {