R/utils.R

Defines functions cli_format_path is.wd clean_up_file_path clean_file_path

#' @title Construct (Clean) Path to File
#' 
#' @description Like [file.path], but also removes all duplicated slashes (/).
#' 
#' @noRd
clean_file_path <- function(...) {
  res <- file.path(...)
  res <- gsub("/+", "/", res)
  res
}

#' @title Clean up a file path
#' @description Given a vector of file paths, removes all leading and trailing
#'   slashes (/) and duplicated slashes inside.
#' @examples
#' clean_up_file_path("///adasd///asdasd//fg/asd///")
#' #> [1] "adasd/asdasd/fg/asd"
#' @noRd
clean_up_file_path <- function(path) {
  stopifnot(is.character(path))
  path <- gsub("^/+", "", path)
  path <- gsub("/+$", "", path)
  path <- gsub("/+", "/", path)
  path
}

#' @description Test if `path` corresponds to the current wd.
#' @noRd
is.wd <- function(path) {
  normalized_path <- normalizePath(path, mustWork = FALSE)
  normalized_wd <- normalizePath(getwd())
  return(normalized_path == normalized_wd)
}

cli_format_path <- function(path) {
  if (is.wd(path)) return("the working directory")
  glue::glue("{{.path {path}}}")
}

Try the cloudfs package in your browser

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

cloudfs documentation built on May 29, 2024, 11:08 a.m.