Skip to content

Commit fffdd9f

Browse files
committed
- Inline prettyunits.
1 parent 1c523e3 commit fffdd9f

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

DESCRIPTION

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@ URL: https://github.com/tidyverse/blob
2121
BugReports: https://github.com/tidyverse/blob/issues
2222
Imports:
2323
methods,
24-
prettyunits (>= 1.1.0.9000),
2524
rlang,
2625
vctrs (>= 0.2.1)
2726
Suggests:
2827
covr,
2928
crayon,
3029
pillar (>= 1.2.1),
3130
testthat
32-
Remotes:
33-
r-lib/prettyunits#24
3431
Encoding: UTF-8
3532
LazyData: true
3633
Roxygen: list(markdown = TRUE, roclets = c("collate", "namespace", "rd", "pkgapi::api_roclet"))

R/format.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ is_vector_s3.blob <- function(x) TRUE
3232

3333
blob_size <- function(x, digits = 3, trim = TRUE, ...) {
3434
x <- vapply(x, length, numeric(1))
35-
prettyunits::pretty_bytes(x, if (isTRUE(trim)) "nopad" else "default")
35+
if (isTRUE(trim)) {
36+
pretty_bytes_nopad(x)
37+
} else {
38+
pretty_bytes_default(x)
39+
}
3640
}
3741

3842
# Dynamically exported, see zzz.R

R/prettyunits.R

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
compute_bytes <- function(bytes, smallest_unit = "B") {
2+
units0 <- c("B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
3+
4+
stopifnot(
5+
is.numeric(bytes),
6+
is.character(smallest_unit),
7+
length(smallest_unit) == 1,
8+
!is.na(smallest_unit),
9+
smallest_unit %in% units0
10+
)
11+
12+
negative <- (bytes < 0)
13+
bytes <- abs(bytes)
14+
smallest_idx <- match(smallest_unit, units0)
15+
16+
limits <- c(1000, 999950 * 1000 ^ (seq_len(length(units0) - 2) - 1))
17+
idx <- cut(bytes, c(0, limits, Inf), labels = FALSE, right = FALSE)
18+
idx <- pmax(idx, smallest_idx)
19+
20+
amount <- bytes / signif(c(1, limits), 1)[idx]
21+
idx[is.na(idx)] <- smallest_idx
22+
unit <- units0[idx]
23+
24+
data.frame(
25+
stringsAsFactors = FALSE,
26+
amount,
27+
unit,
28+
negative
29+
)
30+
}
31+
32+
pretty_bytes_default <- function(bytes) {
33+
szs <- compute_bytes(bytes)
34+
amt <- szs$amount * ifelse(szs$negative, -1, 1)
35+
36+
is_int <- is.na(amt) | amt == as.integer(amt)
37+
38+
## String. For fractions we always show two fraction digits
39+
res <- ifelse(
40+
is_int,
41+
sprintf("%.0f%s", amt, ifelse(all(is_int) | (szs$unit == "B"), "", " ")),
42+
sprintf("%.2f", amt)
43+
)
44+
45+
format(paste(res, szs$unit), justify = "right")
46+
}
47+
48+
pretty_bytes_nopad <- function(bytes) {
49+
sub("^\\s+", "", pretty_bytes_default(bytes))
50+
}

0 commit comments

Comments
 (0)