Skip to content

Commit

Permalink
* Added file_move_temp_auto() to automatically move a Downloads fil…
Browse files Browse the repository at this point in the history
…e to your project directory.

* Added `active_rs_doc_move('dir')` as a shortcut for `rename_files2(fs::path('dir', fs::path_file(active_rs_doc()), active_rs_doc())`.

* Added `file_rename_auto()` as a shortcut for `file.rename(fs::path(fs::path_dir(old_file), new_file, ext = fs::path_ext(old_file)), old_file)`

* Added `file_move_auto()` as a shortcut for `file.rename(fs::path(new_dir, fs::path_file(old_file)), old_file)`
  • Loading branch information
olivroy committed Oct 25, 2024
1 parent 6ef4599 commit 70ebdb1
Show file tree
Hide file tree
Showing 10 changed files with 683 additions and 442 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: reuseme
Title: Collections of Utility Functions to Work Across Projects
Version: 0.0.2.9006
Version: 0.0.2.9007
Authors@R:
person("Olivier", "Roy", , "olivierroy71@hotmail.com", role = c("aut", "cre"))
Description: Allows you to browse current projects, rename files safely,
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ S3method(print,outline_report)
export(active_rs_doc)
export(active_rs_doc_copy)
export(active_rs_doc_delete)
export(active_rs_doc_move)
export(active_rs_doc_nav)
export(arrange_identity)
export(browse_pkg)
Expand All @@ -15,7 +16,10 @@ export(count_pct)
export(dir_outline)
export(distinct_identity)
export(extract_cell_value)
export(file_move_auto)
export(file_move_temp_auto)
export(file_outline)
export(file_rename_auto)
export(filter_detect)
export(filter_identity)
export(filter_if_any)
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ that will passed on to `proj_list()`

* `use_todo("global::todo")` no longer works out of the box. You need to set `options(reuseme.global_todo = fs::path("Documents"))` explicitly (in .Rprofile) for example to make sure
reuseme can write in a directory.

* Added `file_move_temp_auto()` to automatically move a Downloads file to your project directory.

* Added `active_rs_doc_move('dir')` as a shortcut for `rename_files2(fs::path('dir', fs::path_file(active_rs_doc()), active_rs_doc())`.

* Added `file_rename_auto()` as a shortcut for `file.rename(fs::path(fs::path_dir(old_file), new_file, ext = fs::path_ext(old_file)), old_file)`

* Added `file_move_auto()` as a shortcut for `file.rename(fs::path(new_dir, fs::path_file(old_file)), old_file)`

## Fixes

Expand Down
115 changes: 115 additions & 0 deletions R/move.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#' Move temporary file automatically from the R console
#'
#' It works well when you have no API to download a file, but still want a fast R implementation.
#'
#' @param destdir The desired directory to send this to
#' @export
#' @seealso [file_rename_auto()]
file_move_temp_auto <- function(destdir) {
rlang::check_required(destdir)
if (dir.exists(destdir)) {
destdir <- fs::path_expand_r(destdir)
}
if (!fs::is_dir(destdir)) {
cli::cli_abort(c(
"Can't copy file to destdir",
i = "{.path {destdir}} doesn't exist."
))
}
most_recent_file_in_downloads_folder <- fs::dir_info(c("~/Downloads", "~/Desktop"))
source_file <- most_recent_file_in_downloads_folder[[
which.max(most_recent_file_in_downloads_folder$modification_time),
"path"
]]
diff_time <- difftime(Sys.time(), file.mtime(source_file), units = "mins")
if (is.na(diff_time) || diff_time > 60) {
rlang::check_installed("prettyunits")
cli::cli_abort("{.file {source_file}} was created {prettyunits::pretty_dt(diff_time)} ago.")
}
destfile <- fs::path(destdir, fs::path_file(source_file))
fs::file_move(source_file, destfile)
cli::cli_inform(c(
v = "Successfully moved {.file {source_file}} to {.file {destfile}}.",
"Open with {.run fs::file_show('{as.character(destfile)}')}",
"For new name, call `reuseme::file_rename_auto('better-name-sans-ext')` immediately."
))
invisible(destfile)
}

#' Move file automatically between folders
#'
#' @description
#' * `file_rename_auto()` automatically renames your file to a better name while keeping the same folder structure
#'
#'
#' # Advantages
#'
#' Instead of calling `fs::file_move("path/to/dir/file.R", "path/to/dir/new-file.R")`, you can just call
#' `file_rename_auto("new-file", "path/to/dir/file.R")`
#'
#' Instead of calling `fs::file_move("path/to/dir/file.R", "path/to/new-dir/file.R")`, you can just call
#' `file_move_auto("new-dir", "path/to/dir/file.R")`
#'
#' If the functions are used in conjunction with [file_move_temp_auto()],
#'
#' @export
#' @param new_name,new_dir New directory or file name (without extension)
#' @param old_file The old file name
#'
#' @returns The new full path name, invisibly, allowing you to call the functions another time.
file_rename_auto <- function(new_name, old_file = .Last.value) {
if (!fs::is_file(old_name)) {
cli::cli_abort("incorrect context for .rename_temp.")
}
# path_dir() behaves weirdly if not an fs path
# fs::path_dir("~/")
# fs::path_dir(fs::path("~))
# Workaround r-lib/fs#459
old_path <- fs::path_real(old_name)
ext <- fs::path_ext(old_name)
# TRICK {{wf}} path_ext_set replaces or appends extension
new_name <- fs::path_ext_set(new_name, ext)
new_path <- fs::path(
fs::path_dir(old_path),
fs::path_file(new_name)
)
fs::file_move(old_path, new_path)
cli::cli_inform(c(
v = "Successfully moved {.file {old_name}} to {.val {new_name}}.",
"Open with {.run fs::file_show('{as.character(new_path)}')}",
"For new name, call `reuseme::file_rename_auto('better-name-sans-ext')` immediately.",
"For new dir, call `reuseme::file_move_auto('better-name-sans-ext')` immediately."

))
invisible(new_path)
}

#' @export
#' @rdname file_rename_auto
file_move_auto <- function(new_dir, old_file = .Last.value) {
if (!fs::file_exists(old_file) || fs::is_dir(old_fil)) {
cli::cli_abort("Incorrect usage. {.arg old_file} = {.file {old_file}} doesn't exist.")
}

old_file_name <- fs::path_file(old_file)
new_file_name <- fs::path(new_dir, old_file_name)
fs::file_move(
old_file,
new_file_name
)
cli::cli_inform(c(
v = "Successfully moved {.val {old_file_name}} to {.file {new_file_name}}.",
"i" = "Call `reuseme::file_rename_auto('new-file-with-no-ext')` to rename"
))
invisible(new_file_name)
}

# 1. file_move shortcuts
# file_move_auto() is a wrapper of file_move_dir()
# file_rename()
# file_move_dir()
# move dir -> same name
# rename (same dir)



Loading

0 comments on commit 70ebdb1

Please sign in to comment.