denisidoro / navi

An interactive cheatsheet tool for the command-line
Apache License 2.0
15.21k stars 507 forks source link

[feature request] Make `navi` variables to `fzf`'s `header` option #899

Open tapyu opened 6 months ago

tapyu commented 6 months ago

Is your feature request related to a problem? Please describe. Let us consider that I want to create a navi cheat sheet to print a line of a file using sed.

% sed

# Print a specific line from a file
sed -n <number>p <file>

$ file: find . -maxdepth 1 -type f | cut -c3-
$ number: export aux=$(wc -l <file> | cut -d ' ' -f 1) --- --header "This file contains $aux pages"

Describe the solution you'd like My idea is that the aux variable becomes available to fzf so that it prints out a header like

This file contains 618 pages

Describe alternatives you've considered I tried some syntax such as <file>, $file, etc. But it seems that fzf is insensitive to any variable declared by navi.

chiva commented 4 months ago

You can try a workaround for the same result

% sed

# Print a specific line from a file
sed -n <number>p <file>

$ file: find . -maxdepth 1 -type f | cut -c3-
$ number: aux=$(wc -l <file> | cut -f 1) && echo "This file contains $aux pages" --- --header-lines 1
tapyu commented 4 months ago

Small correction, it should be aux=$(wc -l <file> | cut -d ' ' -f 1), we need to define the delimiter.

Anyway, that is a good workaround, but it is a workaround. I would be great to make fzf option aware of both shell-defined variables (e.g., $aux) and navi-defined variables (e.g., <file>), so that one can write something like

% sed

# Print a specific line from a file
sed -n <number>p <file>

$ file: find . -maxdepth 1 -type f | cut -c3-
$ number: export aux=$(wc -l <file> | cut -d ' ' -f 1) --- --header "The file <file> contains $aux pages "

No workarounds, no tricks, no complications.

tapyu commented 4 months ago

Consider another example

# add submodule
git submodule add <is_branch>

$ is_branch: echo "" --- --map "grep -Pq '.+' && echo 'branch=<is_branch>'" --header "are you adding submodule from a specific branch? Type the branch name if yes, or leave empty if no."

I want to create an optional example. If I write something, I must append --branch= to the written argument. Your workaround doesn't work here.

The solution would be

# add submodule
git submodule add <prefix><is_branch>

$ is_branch: echo "" --- --header "are you adding submodule from a specific branch? Type the branch name if yes, or leave empty if no."
$ prefix: [[ -n $(echo <is_branch>) ]] && echo '--branch=' || echo '' --- --fzf-overrides '-1'

Again, a functional but ugly workaround. It would be much better to be able to write former solution.