-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6332428
commit 1b4cc0f
Showing
9 changed files
with
214 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
|
||
format_bytes <- local({ | ||
|
||
pretty_bytes <- function(bytes, style = c("default", "nopad", "6")) { | ||
|
||
style <- switch( | ||
match.arg(style), | ||
"default" = pretty_bytes_default, | ||
"nopad" = pretty_bytes_nopad, | ||
"6" = pretty_bytes_6 | ||
) | ||
|
||
style(bytes) | ||
} | ||
|
||
compute_bytes <- function(bytes, smallest_unit = "B") { | ||
units0 <- c("B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") | ||
|
||
stopifnot( | ||
is.numeric(bytes), | ||
is.character(smallest_unit), | ||
length(smallest_unit) == 1, | ||
!is.na(smallest_unit), | ||
smallest_unit %in% units0 | ||
) | ||
|
||
limits <- c(1000, 999950 * 1000 ^ (seq_len(length(units0) - 2) - 1)) | ||
low <- match(smallest_unit, units0) | ||
units <- units0[low:length(units0)] | ||
limits <- limits[low:length(limits)] | ||
|
||
neg <- bytes < 0 & !is.na(bytes) | ||
bytes <- abs(bytes) | ||
|
||
mat <- matrix( | ||
rep(bytes, each = length(limits)), | ||
nrow = length(limits), | ||
ncol = length(bytes) | ||
) | ||
mat2 <- matrix(mat < limits, nrow = length(limits), ncol = length(bytes)) | ||
exponent <- length(limits) - colSums(mat2) + low - 1L | ||
res <- bytes / 1000 ^ exponent | ||
unit <- units[exponent - low + 2L] | ||
|
||
## Zero bytes | ||
res[bytes == 0] <- 0 | ||
unit[bytes == 0] <- units[1] | ||
|
||
## NA and NaN bytes | ||
res[is.na(bytes)] <- NA_real_ | ||
res[is.nan(bytes)] <- NaN | ||
unit[is.na(bytes)] <- units0[low] # Includes NaN as well | ||
|
||
data.frame( | ||
stringsAsFactors = FALSE, | ||
amount = res, | ||
unit = unit, | ||
negative = neg | ||
) | ||
} | ||
|
||
pretty_bytes_default <- function(bytes) { | ||
szs <- compute_bytes(bytes) | ||
amt <- szs$amount | ||
|
||
## String. For fractions we always show two fraction digits | ||
res <- character(length(amt)) | ||
int <- is.na(amt) | amt == as.integer(amt) | ||
res[int] <- format( | ||
ifelse(szs$negative[int], -1, 1) * amt[int], | ||
scientific = FALSE | ||
) | ||
res[!int] <- sprintf("%.2f", ifelse(szs$negative[!int], -1, 1) * amt[!int]) | ||
|
||
format(paste(res, szs$unit), justify = "right") | ||
} | ||
|
||
pretty_bytes_nopad <- function(bytes) { | ||
sub("^\\s+", "", pretty_bytes_default(bytes)) | ||
} | ||
|
||
pretty_bytes_6 <- function(bytes) { | ||
szs <- compute_bytes(bytes, smallest_unit = "kB") | ||
amt <- szs$amount | ||
|
||
na <- is.na(amt) | ||
nan <- is.nan(amt) | ||
neg <- !na & !nan & szs$negative | ||
l10 <- !na & !nan & !neg & amt < 10 | ||
l100 <- !na & !nan & !neg & amt >= 10 & amt < 100 | ||
b100 <- !na & !nan & !neg & amt >= 100 | ||
|
||
szs$unit[neg] <- "kB" | ||
|
||
famt <- character(length(amt)) | ||
famt[na] <- " NA" | ||
famt[nan] <- "NaN" | ||
famt[neg] <- "< 0" | ||
famt[l10] <- sprintf("%.1f", amt[l10]) | ||
famt[l100] <- sprintf(" %.0f", amt[l100]) | ||
famt[b100] <- sprintf("%.0f", amt[b100]) | ||
|
||
paste0(famt, " ", szs$unit) | ||
} | ||
|
||
structure( | ||
list( | ||
.internal = environment(), | ||
pretty_bytes = pretty_bytes, | ||
compute_bytes = compute_bytes | ||
), | ||
class = c("standalone_bytes", "standalone") | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
format_time <- local({ | ||
assert_diff_time <- function(x) { | ||
stopifnot(inherits(x, "difftime")) | ||
} | ||
|
||
parse_ms <- function(ms) { | ||
stopifnot(is.numeric(ms)) | ||
|
||
data.frame( | ||
days = floor(ms / 86400000), | ||
hours = floor((ms / 3600000) %% 24), | ||
minutes = floor((ms / 60000) %% 60), | ||
seconds = round((ms / 1000) %% 60, 1) | ||
) | ||
} | ||
|
||
first_positive <- function(x) which(x > 0)[1] | ||
|
||
trim <- function(x) gsub("^\\s+|\\s+$", "", x) | ||
|
||
pretty_ms <- function(ms, compact = FALSE) { | ||
stopifnot(is.numeric(ms)) | ||
|
||
parsed <- t(parse_ms(ms)) | ||
|
||
if (compact) { | ||
units <- c("d", "h", "m", "s") | ||
parsed2 <- parsed | ||
parsed2[] <- paste0(parsed, units) | ||
idx <- cbind( | ||
apply(parsed, 2, first_positive), | ||
seq_len(length(ms)) | ||
) | ||
tmp <- paste0("~", parsed2[idx]) | ||
|
||
# handle NAs | ||
tmp[is.na(parsed2[idx])] <- NA_character_ | ||
tmp | ||
} else { | ||
## Exact for small ones | ||
exact <- paste0(ceiling(ms), "ms") | ||
exact[is.na(ms)] <- NA_character_ | ||
|
||
## Approximate for others, in seconds | ||
merge_pieces <- function(pieces) { | ||
## handle NAs | ||
if (all(is.na(pieces))) { | ||
return(NA_character_) | ||
} | ||
|
||
## 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 ") | ||
) | ||
} | ||
approx <- trim(apply(parsed, 2, merge_pieces)) | ||
|
||
ifelse(ms < 1000, exact, approx) | ||
} | ||
} | ||
|
||
pretty_sec <- function(sec, compact = FALSE) { | ||
pretty_ms(sec * 1000, compact = compact) | ||
} | ||
|
||
pretty_dt <- function(dt, compact = FALSE) { | ||
assert_diff_time(dt) | ||
|
||
units(dt) <- "secs" | ||
|
||
pretty_sec(as.vector(dt), compact = compact) | ||
} | ||
|
||
structure( | ||
list( | ||
.internal = environment(), | ||
pretty_ms = pretty_ms, | ||
pretty_sec = pretty_sec, | ||
pretty_dt = pretty_dt | ||
), | ||
class = c("standalone_time", "standalone") | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters