Code Monkey home page Code Monkey logo

Comments (5)

marslo avatar marslo commented on June 15, 2024

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
getenv-preview.mov

but I'm still curious how to call function instead of scripts, for more complex process to preview

from fzf.

marslo avatar marslo commented on June 15, 2024

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:

  • double quotes
    $ env |
      sed -rn 's/^([a-zA-Z0-9]+)=.*$/\1/p' |
      fzf --preview-window 'up,30%,wrap,rounded' \
          --preview "_i={}; echo ${_i}"
  • single quote
    $ env |
      sed -rn 's/^([a-zA-Z0-9]+)=.*$/\1/p' |
      fzf --preview-window 'up,30%,wrap,rounded' \
          --preview '_i={}; echo ${_i}'

details:

fzf-preview-single-double-quotes.mov

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)

from fzf.

LangLangBart avatar LangLangBart commented on June 15, 2024

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 is zsh), the
function still won't work unless you change the SHELL environment variable to bash.

SHELL="$(which bash)" fzf --preview 'foo'

fzf starts child processes using $SHELL -c COMMAND


Alternatives:

  1. make it a string and call the variable
foo=$'echo foo'
: | fzf --preview "$foo"
  1. place the function in a file and source it
foo() {
     echo foo
}
: | fzf --preview 'source /path/to/file/with/function; foo'

from fzf.

marslo avatar marslo commented on June 15, 2024

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:

  1. make it a string and call the variable
foo=$'echo foo'
: | fzf --preview "$foo"
  1. 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

from fzf.

LangLangBart avatar LangLangBart commented on June 15, 2024

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"

Footnotes

  1. Bourne Shell Builtins (Bash Reference Manual)

from fzf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.