Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for bytes and num inline formatting #740

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
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 @@
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_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