From 386711b26233d170743b78582934f5df93ef6135 Mon Sep 17 00:00:00 2001 From: Maximilian Muecke Date: Sun, 10 Nov 2024 18:10:25 +0100 Subject: [PATCH] feat: support for bytes and num inline formatting --- DESCRIPTION | 1 + R/themes.R | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 16ec2c8a..5745e735 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,6 +33,7 @@ Suggests: htmlwidgets, knitr, methods, + prettyunits, processx, ps (>= 1.3.4.9000), rlang (>= 1.0.2.9003), diff --git a/R/themes.R b/R/themes.R index 70397a02..fd72ff12 100644 --- a/R/themes.R +++ b/R/themes.R @@ -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") + ) ) } @@ -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." + )) + } + type <- match.arg(type) + switch(type, + num = prettyunits::pretty_num(x), + bytes = prettyunits::pretty_bytes(x) + ) +} + theme_create <- function(theme) { mtheme <- theme mtheme[] <- lapply(mtheme, create_formatter)