R/utils.R

Defines functions maybe_message discard_end_while keep_while discard_start_while dl_and_read read_url

Documented in discard_end_while discard_start_while dl_and_read keep_while read_url

#' Read a file from a URL
#'
#' Quietly download, read, and delete file
#'
#' @param url URL to a file
#' @keywords internal
read_url <- function(url) {
  return(suppressWarnings(purrr::possibly(dl_and_read)(url)))
}


#' Download and read a file
#'
#' @inheritParams read_url
#'
#' @return A character vector with one element for each line.
#' @keywords internal
dl_and_read <- function(url) { # nocov start
  mode <- ifelse(.Platform$OS.type == "windows", "wb", "w")
  tmp <- tempfile()
  on.exit(unlink(tmp))
  utils::download.file(url, tmp, mode = mode, quiet = TRUE)
  readr::read_lines(tmp)
} # nocov end

#' Discard all values at the start of .x while .p is true
#'
#' @param .x Vector
#' @param .p Logical vector
#' @keywords internal
discard_start_while <- function(.x, .p) {
  if (.p[1] && any(!.p)) {
    .x <- utils::tail(.x, -(min(which(!.p)) - 1))
  }
  .x
}


#' Keep values at the start of .x while .p is true
#'
#' @param .x Vector
#' @param .p Logical vector
#' @keywords internal
keep_while <- function(.x, .p) {
  if (.p[1] && any(!.p)) {
    .x <- utils::head(.x, min(which(!.p)) - 1)
  }
  .x
}


#' Discard all values at the start of .x while .p is true
#'
#' @param .x Vector
#' @param .p Logical vector
#' @keywords internal
discard_end_while <- function(.x, .p) {
  rev(discard_start_while(rev(.x), rev(.p)))
}

maybe_message <- function(verbose,
                          message,
                          class = NULL,
                          ...,
                          call = rlang::caller_env()) {
  if (verbose) {
    if (length(class)) {
      class <- paste0("gutenbergr-msg-", class)
    } else {
      class <- "gutenbergr-msg"
    }
    cli::cli_inform(message, class = class, ..., .envir = call)
  }
}
dgrtwo/gutenbergr documentation built on June 12, 2025, 2:33 a.m.