R/usethis.R

Defines functions read_utf8 write_utf8 write_union

write_union <- function(path, lines) {
  stopifnot(is.character(lines))
  if (file.exists(path)) {
    existing_lines <- read_utf8(path)
  } else {
    existing_lines <- character()
  }
  new <- setdiff(lines, existing_lines)
  if (length(new) == 0) {
    return(invisible(FALSE))
  }
  cli::cli_alert_success("Adding {new} to {path}.")
  all <- c(existing_lines, new)
  write_utf8(path, all)
}


write_utf8 <- function(path, text) {
  opts <- options(encoding = "native.enc")
  withr::defer(options(opts))
  writeLines(enc2utf8(text), path, useBytes = TRUE)
}

read_utf8 <- function(path) {
  opts <- options(encoding = "native.enc")
  withr::defer(options(opts))
  readLines(path, encoding = "UTF-8", warn = FALSE)
}

Try the precommit package in your browser

Any scripts or data that you put into this service are public.

precommit documentation built on July 2, 2022, 1:06 a.m.