nnn as a file picker

This commit is contained in:
Arun Prakash Jana 2019-01-03 23:43:04 +05:30
parent 5675edebc7
commit 3cbb7dc5df
No known key found for this signature in database
GPG Key ID: A75979F35C080412
3 changed files with 42 additions and 19 deletions

View File

@ -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

View File

@ -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

View File

@ -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;
}