Skip to content

Commit

Permalink
feat: support for bytes and num inline formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muecke committed Nov 10, 2024
1 parent 18e2858 commit 386711b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Suggests:
htmlwidgets,
knitr,
methods,
prettyunits,
processx,
ps (>= 1.3.4.9000),
rlang (>= 1.0.2.9003),
Expand Down
21 changes: 20 additions & 1 deletion R/themes.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,13 @@ builtin_theme <- function(dark = getOption("cli.theme_dark", "auto")) {
transform = function(x) format_inline(typename(x))
),
span.or = list("vec-sep2" = " or ", "vec-last" = ", or "),
span.timestamp = list(before = "[", after = "]", color = "grey")
span.timestamp = list(before = "[", after = "]", color = "grey"),
span.bytes = list(
transform = function(x) format_pretty(x, "bytes")
),
span.num = list(
transform = function(x) format_pretty(x, "num")
)
)
}

Expand Down Expand Up @@ -380,6 +386,19 @@ format_code <- function(dark) {
}
}

format_pretty <- function(x, type = c("num", "bytes")) {
if (!requireNamespace("prettyunits", quietly = TRUE)) {
throw(cli_error(
"{.pgk prettyunits} is required for formatting numbers and bytes."
))

Check warning on line 393 in R/themes.R

View check run for this annotation

Codecov / codecov/patch

R/themes.R#L390-L393

Added lines #L390 - L393 were not covered by tests
}
type <- match.arg(type)
switch(type,
num = prettyunits::pretty_num(x),
bytes = prettyunits::pretty_bytes(x)

Check warning on line 398 in R/themes.R

View check run for this annotation

Codecov / codecov/patch

R/themes.R#L395-L398

Added lines #L395 - L398 were not covered by tests
)
}

theme_create <- function(theme) {
mtheme <- theme
mtheme[] <- lapply(mtheme, create_formatter)
Expand Down

0 comments on commit 386711b

Please sign in to comment.