Open marslo opened 6 months ago
hmmm.... I realized that maybe I can use --nth
to prevent call function/scripts to show {1}
in list, and {2}
in previous, and it indeed works :
$ env |
sed -r 's/^([a-zA-Z0-9_-]+)=(.*)$/\1 \2/' |
fzf --delimiter=' ' \
--preview='source ~/.marslo/bin/bash-color.sh; echo -e "$(c Ys)>> {1}$(c)\n$(c Wi).. {2}$(c)"' \
--with-nth=1
https://github.com/junegunn/fzf/assets/5822057/d7f8ec28-40e9-465c-a1e2-35a2cd2f091f
but I'm still curious how to call function instead of scripts, for more complex process to preview
suddenly realized that, I'm struggling for so long is because of "
and '
are different behavior in --preview
!
I was using "
( double quotes ) in the very beginning, and it's not working, so I decided to using function instead of cmd. Accidental I was using '
( single quote ) and it worked !!
here are details:
$ env |
sed -rn 's/^([a-zA-Z0-9]+)=.*$/\1/p' |
fzf --preview-window 'up,30%,wrap,rounded' \
--preview "_i={}; echo ${_i}"
$ env |
sed -rn 's/^([a-zA-Z0-9]+)=.*$/\1/p' |
fzf --preview-window 'up,30%,wrap,rounded' \
--preview '_i={}; echo ${_i}'
details:
https://github.com/junegunn/fzf/assets/5822057/7249e81b-de68-4084-86c6-826b4a4edb7e
my environment:
$ sw_vers
ProductName: macOS
ProductVersion: 14.2.1
BuildVersion: 23C71
$ bash --version
GNU bash, version 5.2.26(1)-release (x86_64-apple-darwin23.2.0)
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ fzf --version
0.45.0 (brew)
I'd like to know whether if any elegant way can handle call a function in --preview instead of call a shell script file
Functions are not visible to child processes unless they are exported with export -f
.
#!/usr/bin/env bash
foo() {
echo foo
}
export -f foo
fzf --preview 'foo'
[!NOTE] Please note, if your login shell isn't
bash
(for example, on macOS the default iszsh
), the function still won't work unless you change theSHELL
environment variable tobash
.SHELL="$(which bash)" fzf --preview 'foo'
fzf starts child processes using
$SHELL -c COMMAND
Alternatives:
foo=$'echo foo'
: | fzf --preview "$foo"
foo() {
echo foo
}
: | fzf --preview 'source /path/to/file/with/function; foo'
I'd like to know whether if any elegant way can handle call a function in --preview instead of call a shell script file
Functions are not visible to child processes unless they are exported with
export -f
.#!/usr/bin/env bash foo() { echo foo } export -f foo fzf --preview 'foo'
export -f foo
work like a charm !
Alternatives:
- make it a string and call the variable
foo=$'echo foo' : | fzf --preview "$foo"
- place the function in a file and source it
foo() { echo foo } : | fzf --preview 'source /path/to/file/with/function'; foo'
for alternatives solution, how to handle fzf
get from pip ( |
) ?
i.e.:
$ foo() { echo ${!1}; }
# fzf from `env | sed`:
$ env | sed -rn 's/^([a-zA-Z0-9]+)=.*$/\1/p' | fzf --preview-window 'up,30%,wrap,rounded' --preview "foo {} "
I've tried:
: | env | sed ... | fzf # < not working
env | sed ... | : | fzf # < not working
I've tried:
: | env | sed ... | fzf # < not working env | sed ... | : | fzf # < not working
I'm not entirely clear on the problem. The colon :
[^1] signifies nothing and can be omitted. It's
only useful if you want to initiate fzf
with an empty list.
foo=$'input={};echo "${!input}"'
command env | sed -rn 's/^([a-zA-Z0-9]+)=.*$/\1/p' | SHELL=bash fzf --preview "$foo"
man fzf
)Info
Problem / Steps to reproduce
The question is inspired from
$ unset **<TAB>
. I'd like to have a function to enhance the format and something else for environment variable.So, theoretical basis to get value of variable value:
I had a function (
getenv
for example ) as below:however, in the fzf preview window, it shows issue:
And the bash works:
I've tried to move
_echo_values()
inside of functiongetenv()
as below, but still no lucky:So, I tried to move the
_echo_values()
to separate files and it works:I'd like to know whether if any elegant way can handle call a function in
--preview
instead of call a shell script file