aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Arun Prakash Jana <engineerarun@gmail.com>2019-01-03 23:43:04 +0530
committerGravatar Arun Prakash Jana <engineerarun@gmail.com>2019-01-03 23:43:35 +0530
commit3cbb7dc5dfcbd79b672859959f3e6c6f264df0ec (patch)
tree7aff023519073c8e7a37295c9c0fafba74867df9
parent5675edebc709c2b3f5f7cabd5c66810997551e28 (diff)
downloadnnn-3cbb7dc5dfcbd79b672859959f3e6c6f264df0ec.tar.gz
nnn as a file picker
-rw-r--r--README.md21
-rw-r--r--scripts/user-scripts/picker.sh27
-rw-r--r--src/nnn.c13
3 files changed, 42 insertions, 19 deletions
diff --git a/README.md b/README.md
index cde37d0..4768222 100644
--- a/README.md
+++ b/README.md
@@ -31,8 +31,6 @@ It runs on Linux, macOS, Raspberry Pi, BSD, Cygwin, Linux subsystem for Windows
We need contributors. Please visit the [ToDo list](https://github.com/jarun/nnn/issues/137).
-If you are using custom scripts which you would like to share with others, please raise a PR to add them under [user-scripts](https://github.com/jarun/nnn/tree/master/scripts/user-scripts).
-
*Love smart and efficient utilities? Explore [my repositories](https://github.com/jarun?tab=repositories). Buy me a cup of coffee if they help you.*
<p align="center">
@@ -69,6 +67,7 @@ If you are using custom scripts which you would like to share with others, pleas
- [get selection manually](#get-selection-manually)
- [cd on quit](#cd-on-quit)
- [(neo)vim plugin](#neovim-plugin)
+ - [file picker](#file-picker)
- [run custom scripts](#run-custom-scripts)
- [sample scripts](#sample-scripts)
- [launch applications](#launch-applications)
@@ -452,8 +451,6 @@ To get the list in a file:
ncp > out.txt
-To use `nnn` as a file picker and redirect the output to other programs, please see [issue #183](https://github.com/jarun/nnn/issues/183).
-
#### cd on quit
To quit `nnn` and switch to the directory last opened follow the instructions below.
@@ -466,6 +463,10 @@ As you might notice, `nnn` uses the environment variable `NNN_TMPFILE` to write
`nnn` can be used as a file picker/chooser within vim or neovim. Find the plugin [here](https://github.com/mcchrish/nnn.vim).
+#### file picker
+
+To use `nnn` as a file picker and redirect the output to other programs, use [picker.sh](https://github.com/jarun/nnn/blob/master/scripts/user-scripts/picker.sh).
+
#### run custom scripts
`nnn` can invoke custom scripts with the currently selected file name as argument 1.
@@ -480,17 +481,7 @@ Press <kbd>R</kbd> to run the script in the current directory. You can also use
##### sample scripts
-- Open image files in current dir in **sxiv**:
-
- #!/usr/bin/env sh
-
- sxiv -q * >/dev/null 2>&1
-
-- Fuzzy find files in **fzy** and open with xdg-open:
-
- #!/usr/bin/env sh
-
- xdg-open $(find -type f | fzy) >/dev/null 2>&1
+Sample scripts for use cases like sxiv or fzy integration are under [user-scripts](https://github.com/jarun/nnn/tree/master/scripts/user-scripts). Feel free to contribute yours!
#### launch applications
diff --git a/scripts/user-scripts/picker.sh b/scripts/user-scripts/picker.sh
new file mode 100644
index 0000000..1968c96
--- /dev/null
+++ b/scripts/user-scripts/picker.sh
@@ -0,0 +1,27 @@
+#!/bin/bash
+
+# Description: Pick files and pipe the line-separated list to another utility
+#
+# Shell: bash
+# Author: Arun Prakash Jana
+#
+# Usage:
+# Copy this file in your $PATH, make it executable and preferably name it to picker.
+# Run commands like:
+# ls -l `picker`
+# cd `picker`
+# or, in fish shell:
+# ls -l (picker)
+# cd (picker)
+#
+# NOTE: This use case is limited to picking files, other functionality may not work as expected.
+
+nnn -p /tmp/pickerout
+> /tmp/picked
+while read -d $'\0' line ; do
+ echo $line >> /tmp/picked
+done < /tmp/pickerout
+echo $line >> /tmp/picked
+cat /tmp/picked
+
+rm /tmp/pickerout /tmp/picked
diff --git a/src/nnn.c b/src/nnn.c
index c018c46..45f6c23 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -780,13 +780,18 @@ static bool showcplist()
/* Initialize curses mode */
static bool initcurses(void)
{
- if (initscr() == NULL) {
+ if (cfg.picker) {
+ if (!newterm(NULL, stderr, stdin)) {
+ fprintf(stderr, "newterm!\n");
+ return FALSE;
+ }
+ } else if (!initscr()) {
char *term = getenv("TERM");
if (term != NULL)
fprintf(stderr, "error opening TERM: %s\n", term);
else
- fprintf(stderr, "initscr() failed\n");
+ fprintf(stderr, "initscr!\n");
return FALSE;
}
@@ -3592,8 +3597,8 @@ int main(int argc, char *argv[])
}
/* Confirm we are in a terminal */
- if (!isatty(0) || !isatty(1)) {
- fprintf(stderr, "stdin or stdout is not a tty\n");
+ if (!cfg.picker && !(isatty(0) && isatty(1))) {
+ fprintf(stderr, "stdin/stdout !tty\n");
return 1;
}