]> Sergey Matveev's repositories - nnn.git/commitdiff
nnn as a file picker
authorArun Prakash Jana <engineerarun@gmail.com>
Thu, 3 Jan 2019 18:13:04 +0000 (23:43 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Thu, 3 Jan 2019 18:13:35 +0000 (23:43 +0530)
README.md
scripts/user-scripts/picker.sh [new file with mode: 0644]
src/nnn.c

index cde37d06c1e8558c5085dd1b2c8b9641396f157f..4768222c3705c936e3c6bfbf672e26108366789f 100644 (file)
--- 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 (file)
index 0000000..1968c96
--- /dev/null
@@ -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
index c018c46e94b97e14b084dd7839f185d538144ccc..45f6c23f4fea879c5926b27407ad2aa2d2e8964c 100644 (file)
--- 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;
        }