Skip to content
Open
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion R/time.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
42 changes: 42 additions & 0 deletions tests/testthat/_snaps/progress-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-progress-variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand All @@ -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", {
Expand Down
Loading