R/file-system.R

Defines functions path_norm_copypaste cli_inform_write nav

Documented in nav

#' Navigate to a file
#'
#' When used interactively, opens the file/folder.
#'
#' @param x A path.
#'
#' @return Invisibly returns `x`.
#' @export
#'
#' @examples
#' \dontrun{
#' if (interactive()) {
#'   nav(getwd())
#'  }
#' }
nav <- function(x) {
  path <- fs::path_tidy(x)
  if (rlang::is_interactive()) {
    fs::file_show(path)
  }
  invisible(path)
}

cli_inform_write <- function(x, start = ".") {
  path <- fs::path_tidy(x)
  relpath <- as.character(fs::path_rel(path, start = start))
  nav_expr <- sprintf('penngradlings::nav("%s")', path)
  cli::cli_inform('Saved to {.run [{relpath}]({nav_expr})}')
}

path_norm_copypaste <- function(x) {
  path <- fs::path_norm(utils::readClipboard())
  utils::writeClipboard(path)
}
yjunechoe/penngradlings documentation built on Sept. 6, 2024, 8:13 p.m.