mirror of
https://github.com/jarun/nnn.git
synced 2024-11-28 05:41:31 +00:00
nnn as a file picker
This commit is contained in:
parent
5675edebc7
commit
3cbb7dc5df
21
README.md
21
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).
|
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.*
|
*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">
|
<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)
|
- [get selection manually](#get-selection-manually)
|
||||||
- [cd on quit](#cd-on-quit)
|
- [cd on quit](#cd-on-quit)
|
||||||
- [(neo)vim plugin](#neovim-plugin)
|
- [(neo)vim plugin](#neovim-plugin)
|
||||||
|
- [file picker](#file-picker)
|
||||||
- [run custom scripts](#run-custom-scripts)
|
- [run custom scripts](#run-custom-scripts)
|
||||||
- [sample scripts](#sample-scripts)
|
- [sample scripts](#sample-scripts)
|
||||||
- [launch applications](#launch-applications)
|
- [launch applications](#launch-applications)
|
||||||
|
@ -452,8 +451,6 @@ To get the list in a file:
|
||||||
|
|
||||||
ncp > out.txt
|
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
|
#### cd on quit
|
||||||
|
|
||||||
To quit `nnn` and switch to the directory last opened follow the instructions below.
|
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).
|
`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
|
#### run custom scripts
|
||||||
|
|
||||||
`nnn` can invoke custom scripts with the currently selected file name as argument 1.
|
`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
|
##### sample scripts
|
||||||
|
|
||||||
- Open image files in current dir in **sxiv**:
|
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!
|
||||||
|
|
||||||
#!/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
|
|
||||||
|
|
||||||
#### launch applications
|
#### launch applications
|
||||||
|
|
||||||
|
|
27
scripts/user-scripts/picker.sh
Normal file
27
scripts/user-scripts/picker.sh
Normal 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
|
13
src/nnn.c
13
src/nnn.c
|
@ -780,13 +780,18 @@ static bool showcplist()
|
||||||
/* Initialize curses mode */
|
/* Initialize curses mode */
|
||||||
static bool initcurses(void)
|
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");
|
char *term = getenv("TERM");
|
||||||
|
|
||||||
if (term != NULL)
|
if (term != NULL)
|
||||||
fprintf(stderr, "error opening TERM: %s\n", term);
|
fprintf(stderr, "error opening TERM: %s\n", term);
|
||||||
else
|
else
|
||||||
fprintf(stderr, "initscr() failed\n");
|
fprintf(stderr, "initscr!\n");
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3592,8 +3597,8 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Confirm we are in a terminal */
|
/* Confirm we are in a terminal */
|
||||||
if (!isatty(0) || !isatty(1)) {
|
if (!cfg.picker && !(isatty(0) && isatty(1))) {
|
||||||
fprintf(stderr, "stdin or stdout is not a tty\n");
|
fprintf(stderr, "stdin/stdout !tty\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue