Skip to content

Commit

Permalink
Drop prettyunits dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Nov 20, 2023
1 parent 6332428 commit 1b4cc0f
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 30 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ Imports:
lpSolve,
pkgbuild (>= 1.0.2),
pkgcache (>= 2.2.0),
prettyunits (>= 1.1.1),
processx (>= 3.4.2),
ps,
R6,
Expand Down
3 changes: 0 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ importFrom(jsonlite,unbox)
importFrom(lpSolve,lp)
importFrom(pkgbuild,build)
importFrom(pkgbuild,pkgbuild_process)
importFrom(prettyunits,pretty_bytes)
importFrom(prettyunits,pretty_dt)
importFrom(prettyunits,pretty_sec)
importFrom(stats,na.omit)
importFrom(utils,modifyList)
importFrom(utils,untar)
Expand Down
18 changes: 8 additions & 10 deletions R/download-progress-bar.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,17 @@ pkgplan__initial_pb_message <- function(bar) {
cli_alert_info(c(
"No downloads are needed",
if (nch > 0) ", {nch} pkg{?s} ",
if (cbt > 0) "{.size ({pretty_bytes(cbt)})} ",
if (cbt > 0) "{.size ({format_bytes$pretty_bytes(cbt)})} ",
if (nch > 0) "{qty(nch)}{?is/are} cached"
))
} else {
cli_alert_info(c(
"Getting",
if (bts > 0) " {num-unk} pkg{?s} {.size ({pretty_bytes(bts)})}",
if (bts > 0) " {num-unk} pkg{?s} {.size ({format_bytes$pretty_bytes(bts)})}",
if (bts > 0 && unk > 0) " and",
if (unk > 0) " {unk} pkg{?s} with unknown size{?s}",
if (nch > 0) ", {nch} ",
if (cbt > 0) "{.size ({pretty_bytes(cbt)})} ",
if (cbt > 0) "{.size ({format_bytes$pretty_bytes(cbt)})} ",
if (nch > 0) "cached"
))
}
Expand Down Expand Up @@ -192,7 +192,7 @@ pkgplan__update_progress_bar <- function(bar, idx, event, data) {
cli_alert_success(c(
"Got {.pkg {data$package}} ",
"{.version {data$version}} ({data$platform})",
if (!is.na(sz) && bar$show_size) " {.size ({pretty_bytes(sz)})}"
if (!is.na(sz) && bar$show_size) " {.size ({format_bytes$pretty_bytes(sz)})}"
))
if (!is.na(bar$what$filesize[idx])) {
bar$chunks[[sec]] <- (bar$chunks[[sec]] %||% 0) -
Expand Down Expand Up @@ -256,8 +256,6 @@ pkgplan__update_progress_bar <- function(bar, idx, event, data) {
#'
#' @noRd

#' @importFrom prettyunits pretty_bytes pretty_dt

pkgplan__show_progress_bar <- function(bar) {
if (is.null(bar$status)) return()

Expand Down Expand Up @@ -290,7 +288,7 @@ calculate_rate <- function(start, now, chunks) {
if (rate == 0 && time_at < 4) {
rstr <- strrep(" ", 8)
} else {
rstr <- paste0(pretty_bytes(rate, style = "6"), "/s")
rstr <- paste0(format_bytes$pretty_bytes(rate, style = "6"), "/s")
}
list(rate = rate, rstr = rstr)
}
Expand All @@ -305,7 +303,7 @@ calculate_eta <- function(total, current, rate) {
if (etas < 1) {
estr <- "<1s "
} else {
estr <- format(pretty_dt(etas, compact = TRUE), width = 6)
estr <- format(format_time$pretty_dt(etas, compact = TRUE), width = 6)
}
}
list(etas = etas, estr = estr)
Expand Down Expand Up @@ -368,12 +366,12 @@ pkgplan__done_progress_bar <- function(bar) {
if (is.null(bar$status)) return()

end_at <- Sys.time()
dt <- pretty_dt(Sys.time() - bar$start_at)
dt <- format_time$pretty_dt(Sys.time() - bar$start_at)

cli_status_clear(bar$status)
bar$status <- NULL

bts <- pretty_bytes(sum(bar$what$current))
bts <- format_bytes$pretty_bytes(sum(bar$what$current))
dld <- sum(bar$what$status == "got")
cch <- sum(bar$what$status == "had")
err <- sum(bar$what$status == "error")
Expand Down
1 change: 0 additions & 1 deletion R/download.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#' @aliases pkg_download_result
NULL

#' @importFrom prettyunits pretty_bytes
#' @importFrom cli ansi_hide_cursor ansi_show_cursor

pkgplan_download_resolution <- function(self, private) {
Expand Down
15 changes: 5 additions & 10 deletions R/install-plan.R
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ stop_task_package_uncompress <- function(state, worker) {
pkg <- state$plan$package[pkgidx]
version <- state$plan$version[pkgidx]
time <- Sys.time() - state$plan$package_time[[pkgidx]]
ptime <- pretty_sec(as.numeric(time, units = "secs"))
ptime <- format_time$pretty_sec(as.numeric(time, units = "secs"))
alert("danger", "Failed to uncompress {.pkg {pkg}} {.version {version}}")
update_progress_bar(state, 1L)

Expand Down Expand Up @@ -616,7 +616,7 @@ stop_task_package_build <- function(state, worker) {
pkg <- state$plan$package[pkgidx]
version <- state$plan$version[pkgidx]
time <- Sys.time() - state$plan$package_time[[pkgidx]]
ptime <- pretty_sec(as.numeric(time, units = "secs"))
ptime <- format_time$pretty_sec(as.numeric(time, units = "secs"))

if (success) {
alert("success", paste0(
Expand Down Expand Up @@ -675,8 +675,6 @@ stop_task_package_build <- function(state, worker) {
state
}

#' @importFrom prettyunits pretty_sec

stop_task_build <- function(state, worker) {

## TODO: make sure exit status is non-zero on build error!
Expand All @@ -686,7 +684,7 @@ stop_task_build <- function(state, worker) {
pkg <- state$plan$package[pkgidx]
version <- state$plan$version[pkgidx]
time <- Sys.time() - state$plan$build_time[[pkgidx]]
ptime <- pretty_sec(as.numeric(time, units = "secs"))
ptime <- format_time$pretty_sec(as.numeric(time, units = "secs"))

if (success) {
alert("success", paste0(
Expand Down Expand Up @@ -779,8 +777,6 @@ installed_note <- function(pkg) {
)
}

#' @importFrom prettyunits pretty_sec

stop_task_install <- function(state, worker) {

## TODO: make sure the install status is non-zero on exit
Expand All @@ -790,7 +786,7 @@ stop_task_install <- function(state, worker) {
pkg <- state$plan$package[pkgidx]
version <- state$plan$version[pkgidx]
time <- Sys.time() - state$plan$install_time[[pkgidx]]
ptime <- pretty_sec(as.numeric(time, units = "secs"))
ptime <- format_time$pretty_sec(as.numeric(time, units = "secs"))
note <- installed_note(state$plan[pkgidx,])

if (success) {
Expand Down Expand Up @@ -829,7 +825,6 @@ create_install_result <- function(state) {
}

#' @export
#' @importFrom prettyunits pretty_sec

print.pkginstall_result <- function(x, ...) {
newly <- sum(x$lib_status == "new" & x$type != "deps")
Expand All @@ -846,7 +841,7 @@ print.pkginstall_result <- function(x, ...) {
if (upd) paste0(emoji("rocket", ""), " ", upd, " updated"),
if (noupd + curr) paste0(emoji("hand", ""), " ", noupd + curr, " kept"),
if (! tolower(Sys.getenv("PKG_OMIT_TIMES")) == "true") {
paste0("in ", pretty_sec(build_time + inst_time))
paste0("in ", format_time$pretty_sec(build_time + inst_time))
}
)

Expand Down
2 changes: 0 additions & 2 deletions R/resolution.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@
#' @aliases pkg_resolution_result
NULL

#' @importFrom prettyunits pretty_dt

pkgplan_resolve <- function(self, private) {
"!DEBUG pkgplan_resolve (sync)"
synchronise(self$async_resolve())
Expand Down
114 changes: 114 additions & 0 deletions R/sizes.R
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")
)
})
86 changes: 86 additions & 0 deletions R/time.R
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")
)
})
4 changes: 1 addition & 3 deletions R/tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@ annotate_tree <- function(sol, ann, builder = NULL) {
))
}

#' @importFrom prettyunits pretty_bytes

format_file_size <- function(x) {
bts <- str_trim(pretty_bytes(x))
bts <- str_trim(format_bytes$pretty_bytes(x))
cli::col_silver(
ifelse(is.na(x), "(unknown size)", paste0("(", bts, ")"))
)
Expand Down

0 comments on commit 1b4cc0f

Please sign in to comment.