Add option -p for file picker

This commit is contained in:
Arun Prakash Jana 2018-11-23 22:41:47 +05:30
parent 435790325e
commit 9e974be3dd
No known key found for this signature in database
GPG key ID: A75979F35C080412
3 changed files with 36 additions and 9 deletions

View file

@ -184,7 +184,7 @@ Search keyword and option completion scripts for Bash, Fish and Zsh can be found
``` ```
usage: nnn [-b key] [-c N] [-e] [-i] [-l] usage: nnn [-b key] [-c N] [-e] [-i] [-l]
[-S] [-v] [-h] [PATH] [-p file] [-S] [-v] [-h] [PATH]
The missing terminal file manager for X. The missing terminal file manager for X.
@ -197,6 +197,7 @@ optional args:
-e use exiftool instead of mediainfo -e use exiftool instead of mediainfo
-i start in navigate-as-you-type mode -i start in navigate-as-you-type mode
-l start in light mode -l start in light mode
-p file copy selection to file (stdout if '-')
-S start in disk usage analyser mode -S start in disk usage analyser mode
-v show program version -v show program version
-h show this help -h show this help

4
nnn.1
View file

@ -11,6 +11,7 @@
.Op Ar -e .Op Ar -e
.Op Ar -i .Op Ar -i
.Op Ar -l .Op Ar -l
.Op Ar -p file
.Op Ar -S .Op Ar -S
.Op Ar -v .Op Ar -v
.Op Ar -h .Op Ar -h
@ -178,6 +179,9 @@ supports the following options:
.Fl l .Fl l
start in light mode (fewer details) start in light mode (fewer details)
.Pp .Pp
.Fl "p file"
copy (or \fIpick\fR) selection to file, or stdout if file='-'
.Pp
.Fl S .Fl S
start in disk usage analyzer mode start in disk usage analyzer mode
.Pp .Pp

View file

@ -261,9 +261,11 @@ typedef struct {
uint quote : 1; /* Copy paths within quotes */ uint quote : 1; /* Copy paths within quotes */
uint color : 3; /* Color code for directories */ uint color : 3; /* Color code for directories */
uint ctxactive : 1; /* Context active or not */ uint ctxactive : 1; /* Context active or not */
uint reserved : 13; uint reserved : 11;
/* The following settings are global */ /* The following settings are global */
uint curctx : 2; /* Current context number */ uint curctx : 2; /* Current context number */
uint picker : 1; /* Write selection to user-specified file */
uint pickraw : 1; /* Write selection to sdtout before exit */
} settings; } settings;
/* Contexts or workspaces */ /* Contexts or workspaces */
@ -279,7 +281,7 @@ typedef struct {
/* GLOBALS */ /* GLOBALS */
/* Configuration, contexts */ /* Configuration, contexts */
static settings cfg = {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 4, 1, 0, 0}; static settings cfg = {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 4, 1, 0, 0, 0, 0};
static context g_ctx[MAX_CTX] __attribute__ ((aligned)); static context g_ctx[MAX_CTX] __attribute__ ((aligned));
static struct entry *dents; static struct entry *dents;
@ -305,7 +307,7 @@ static uchar crc8table[CRC8_TABLE_LEN] __attribute__ ((aligned));
static char g_buf[MAX_CMD_LEN] __attribute__ ((aligned)); static char g_buf[MAX_CMD_LEN] __attribute__ ((aligned));
/* Buffer for file path copy file */ /* Buffer for file path copy file */
static char g_cppath[MAX_HOME_LEN] __attribute__ ((aligned)); static char g_cppath[PATH_MAX] __attribute__ ((aligned));
/* Buffer to store tmp file path */ /* Buffer to store tmp file path */
static char g_tmpfpath[MAX_HOME_LEN] __attribute__ ((aligned)); static char g_tmpfpath[MAX_HOME_LEN] __attribute__ ((aligned));
@ -432,7 +434,7 @@ static void printerr(int linenum)
{ {
exitcurses(); exitcurses();
fprintf(stderr, "line %d: (%d) %s\n", linenum, errno, strerror(errno)); fprintf(stderr, "line %d: (%d) %s\n", linenum, errno, strerror(errno));
if (g_cppath[0]) if (!cfg.picker && g_cppath[0])
unlink(g_cppath); unlink(g_cppath);
exit(1); exit(1);
} }
@ -651,6 +653,10 @@ static char *xbasename(char *path)
/* Writes buflen char(s) from buf to a file */ /* Writes buflen char(s) from buf to a file */
static void writecp(const char *buf, const size_t buflen) static void writecp(const char *buf, const size_t buflen)
{ {
if (cfg.pickraw) {
return;
}
if (!g_cppath[0]) { if (!g_cppath[0]) {
printmsg(messages[STR_COPY_ID]); printmsg(messages[STR_COPY_ID]);
return; return;
@ -3393,7 +3399,7 @@ static void usage(void)
{ {
fprintf(stdout, fprintf(stdout,
"usage: nnn [-b key] [-c N] [-e] [-i] [-l]\n" "usage: nnn [-b key] [-c N] [-e] [-i] [-l]\n"
" [-S] [-v] [-h] [PATH]\n\n" " [-p file] [-S] [-v] [-h] [PATH]\n\n"
"The missing terminal file manager for X.\n\n" "The missing terminal file manager for X.\n\n"
"positional args:\n" "positional args:\n"
" PATH start dir [default: current dir]\n\n" " PATH start dir [default: current dir]\n\n"
@ -3403,6 +3409,7 @@ static void usage(void)
" -e use exiftool instead of mediainfo\n" " -e use exiftool instead of mediainfo\n"
" -i start in navigate-as-you-type mode\n" " -i start in navigate-as-you-type mode\n"
" -l start in light mode\n" " -l start in light mode\n"
" -p file copy selection to file (stdout if '-')\n"
" -S start in disk usage analyser mode\n" " -S start in disk usage analyser mode\n"
" -v show program version\n" " -v show program version\n"
" -h show this help\n\n" " -h show this help\n\n"
@ -3422,7 +3429,7 @@ int main(int argc, char *argv[])
exit(1); exit(1);
} }
while ((opt = getopt(argc, argv, "Slib:c:evh")) != -1) { while ((opt = getopt(argc, argv, "Slib:c:ep:vh")) != -1) {
switch (opt) { switch (opt) {
case 'S': case 'S':
cfg.blkorder = 1; cfg.blkorder = 1;
@ -3448,6 +3455,19 @@ int main(int argc, char *argv[])
case 'e': case 'e':
cfg.metaviewer = EXIFTOOL; cfg.metaviewer = EXIFTOOL;
break; break;
case 'p':
cfg.picker = 1;
if (optarg[0] == '-' && optarg[1] == '\0')
cfg.pickraw = 1;
else {
/* copier used as tmp var */
copier = realpath(optarg, g_cppath);
if (!g_cppath[0]) {
fprintf(stderr, "%s\n", strerror(errno));
exit(1);
}
}
break;
case 'v': case 'v':
fprintf(stdout, "%s\n", VERSION); fprintf(stdout, "%s\n", VERSION);
return 0; return 0;
@ -3532,7 +3552,7 @@ int main(int argc, char *argv[])
else if (xdiraccess("/tmp")) else if (xdiraccess("/tmp"))
g_tmpfplen = xstrlcpy(g_tmpfpath, "/tmp", MAX_HOME_LEN); g_tmpfplen = xstrlcpy(g_tmpfpath, "/tmp", MAX_HOME_LEN);
if (g_tmpfplen) { if (!cfg.picker && g_tmpfplen) {
xstrlcpy(g_cppath, g_tmpfpath, MAX_HOME_LEN); xstrlcpy(g_cppath, g_tmpfpath, MAX_HOME_LEN);
xstrlcpy(g_cppath + g_tmpfplen - 1, "/.nnncp", MAX_HOME_LEN - g_tmpfplen); xstrlcpy(g_cppath + g_tmpfplen - 1, "/.nnncp", MAX_HOME_LEN - g_tmpfplen);
} }
@ -3561,7 +3581,9 @@ int main(int argc, char *argv[])
browse(ipath); browse(ipath);
exitcurses(); exitcurses();
if (g_cppath[0]) if (cfg.pickraw)
opt = write(1, pcopybuf, copybufpos - 1);
else if (!cfg.picker && g_cppath[0])
unlink(g_cppath); unlink(g_cppath);
#ifdef LINUX_INOTIFY #ifdef LINUX_INOTIFY