R/zzz.R

Defines functions get_remote_file_size null_to_na msgWarning msgDownload msgSuccess msgError msgInfo

#' @importFrom crayon blue red green yellow
#' @importFrom cli style_bold style_underline cat_rule cat_line cat_bullet
#' @import utils

msgInfo <- function(..., appendLF = TRUE) {
  txt <- paste(cli::symbol$info, ...)
  message(blue(txt), appendLF = appendLF)
  invisible(txt)
}

msgError <- function(..., appendLF = TRUE) {
  txt <- paste(cli::symbol$cross, ...)
  message(red(txt), appendLF = appendLF)
  invisible(txt)
}

msgSuccess <- function(..., appendLF = TRUE) {
  txt <- paste(cli::symbol$tick, ...)
  message(green(txt), appendLF = appendLF)
  invisible(txt)
}

# x is a resource
msgDownload <- function(url, fmt, name) {
  sz <- ifelse(fmt != "other", get_remote_file_size(url), "unknown")
  msgInfo(name, paste0("(format: ", fmt, " - size: ", sz, ") "),
    appendLF = FALSE)
}

msgWarning <- function(..., appendLF = TRUE) {
  txt <- paste(cli::symbol$warning, ...)
  message(yellow(txt), appendLF = appendLF)
  invisible(txt)
}

# convert null to na recursively
null_to_na <- function(x) {
    if (is.list(x)) {
        return(lapply(x, null_to_na))
    } else {
        return(ifelse(is.null(x), NA, x))
    }
}

#
get_remote_file_size <- function(url) {
  hdr <- curlGetHeaders(url)
  tmp <- as.numeric(
    gsub("\\D", "", hdr[grepl("^Content-Length:", hdr)])
  )
  # browser()
  if (length(tmp) == 1) {
    format(structure(tmp, class = "object_size"), units = "auto")
  } else if (length(tmp) == 2) {
    format(structure(tmp[2], class = "object_size"), units = "auto")
  } else "unknown"
}
VLucet/rgovcan documentation built on Nov. 24, 2022, 6:52 p.m.