Skip to content

Commit

Permalink
WIP: add support for custom trash command
Browse files Browse the repository at this point in the history
this makes it so that if NNN_TRASH is set to a string other than
"1" or "2" then it is accepted as the trash command to run.

this allows us to support arbritary trashing utilities while
also maintaining backwards compatibility for older "1" & "2"
values.

Fixes: #1168
Fixes: #1963
Fixes: #1960
Fixes: #1761
  • Loading branch information
N-R-K committed Jan 4, 2025
1 parent 5522292 commit f260b1a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
4 changes: 3 additions & 1 deletion plugins/.nmv
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ case "$NNN_TRASH" in
RM_UTIL="trash-put" ;;
2)
RM_UTIL="gio trash" ;;
*)
"")
RM_UTIL="rm -ri --" ;;
*)
RM_UTIL="$NNN_TRASH" ;;
esac

exit_status=0
Expand Down
19 changes: 12 additions & 7 deletions src/nnn.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ typedef struct {
uint_t selbm : 1; /* Select a bookmark from bookmarks directory */
uint_t selmode : 1; /* Set when selecting files */
uint_t stayonsel : 1; /* Disable auto-advance on selection */
uint_t trash : 2; /* Trash method 0: rm -rf, 1: trash-cli, 2: gio trash */
uint_t trash : 1; /* 0: rm, 1: trashcmd */
uint_t uidgid : 1; /* Show owner and group info */
uint_t usebsdtar : 1; /* Use bsdtar as default archive utility */
uint_t xprompt : 1; /* Use native prompt instead of readline prompt */
Expand Down Expand Up @@ -462,6 +462,7 @@ static char *listroot;
static char *plgpath;
static char *pnamebuf, *pselbuf, *findselpos;
static char *mark;
static char *trashcmd;
#ifndef NOX11
static char hostname[_POSIX_HOST_NAME_MAX + 1];
#endif
Expand Down Expand Up @@ -2579,7 +2580,7 @@ static bool rmmulstr(char *buf, bool use_trash)
r, selpath);
else
snprintf(buf, CMD_LEN_MAX, "xargs -0 %s < %s",
utils[(g_state.trash == 1) ? UTIL_TRASH_CLI : UTIL_GIO_TRASH], selpath);
trashcmd, selpath);

return TRUE;
}
Expand All @@ -2597,8 +2598,7 @@ static bool xrm(char * const fpath, bool use_trash)
rm_opts[3] = r;
spawn("rm", rm_opts, "--", fpath, F_NORMAL | F_CHKRTN);
} else
spawn(utils[(g_state.trash == 1) ? UTIL_TRASH_CLI : UTIL_GIO_TRASH],
fpath, NULL, NULL, F_NORMAL | F_MULTI);
spawn(trashcmd, fpath, NULL, NULL, F_NORMAL | F_MULTI);

return (access(fpath, F_OK) == -1); /* File is removed */
}
Expand Down Expand Up @@ -9045,9 +9045,14 @@ int main(int argc, char *argv[])
#endif

/* Configure trash preference */
opt = xgetenv_val(env_cfg[NNN_TRASH]);
if (opt && opt <= 2)
g_state.trash = opt;
trashcmd = getenv(env_cfg[NNN_TRASH]);
if (trashcmd) {
g_state.trash = TRUE;
if (strcmp(trashcmd, "1") == 0)
trashcmd = utils[UTIL_TRASH_CLI];
else if (strcmp(trashcmd, "2") == 0)
trashcmd = utils[UTIL_GIO_TRASH];
}

/* Ignore/handle certain signals */
struct sigaction act = {.sa_handler = sigint_handler};
Expand Down

0 comments on commit f260b1a

Please sign in to comment.