Skip to content

[question] elegant way to call function in an previous instead of call script file #3599

Closed
@marslo

Description

@marslo
  • I have read through the manual page (man fzf)
  • I have the latest version of fzf
  • I have searched through the existing issues

Info

  • OS
    • Linux
    • Mac OS X
    • Windows
    • Etc.
  • Shell
    • bash
    • zsh
    • fish

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:

$ foo='SHELL'      # `SHELL` is the env var name

$ echo "${foo} - $(eval echo \$$foo)"
SHELL - /usr/local/bin/bash                  # i.e.: this is what I want to show in preview
# or
$ echo "${foo} - ${!foo}"
SHELL - /usr/local/bin/bash

I had a function ( getenv for example ) as below:

$ cat ~/ee.sh
#!/usr/bin/env bash
# shellcheck disable=SC2086

function _echo_values() { echo -e ">> $1\n.. $(eval echo \$$1)"; }
function getenv() {
  local option
  local -a array

  option='-1 --exit-0 --sort --multi --cycle'

  while read -r _env; do
    _echo_values $_env                                           # expected: show in terminal
    array+=( "${_env}=$(eval echo \$${_env})" )
  done < <( env |
            sed -r 's/^([a-zA-Z0-9_-]+)=.*$/\1/' |
            fzf ${option} \
                --prompt 'env> ' \
                --preview-window 'right,70%,wrap,rounded' \
                --preview "_echo_values {}" \                    # expected: show in preview
                --header 'TAB/SHIFT-TAB to select multiple items, CTRL-D to deselect-all, CTRL-S to select-all'
          )
  pbcopy < <( printf '%s\n' "${array[@]}" | head -c-1 )
}

# source the function and verify
$ source ~/ee.sh
$ _echo_values SHELL                                           # verify
>> SHELL
.. /usr/local/bin/bash                                         # works

however, in the fzf preview window, it shows issue:

/usr/local/bin/bash: line 1: _echo_values: command not found
getenv-1

And the bash works:
image

I've tried to move _echo_values() inside of function getenv() as below, but still no lucky:

function getenv() {
  local option
  local -a array

  option='-1 --exit-0 --sort --multi --cycle'
  _echo_values() { echo -e ">> $1\n.. $(eval echo \$$1)"; }     # moved inside of `getenv`, cannot be call in terminal, but can be identified inside the `getenv()`

  while read -r _env; do
    _echo_values $_env                                          # works here
    array+=( "${_env}=$(eval echo \$${_env})" )
  done < <( env |
            sed -r 's/^([a-zA-Z0-9_-]+)=.*$/\1/' |
            fzf ${option} \
                --prompt 'env> ' \
                --preview-window 'right,70%,wrap,rounded' \
                --preview "_echo_values {}" \                    # issue here: `/usr/local/bin/bash : line 1: _echo_values: command not found`
                --header 'TAB/SHIFT-TAB to select multiple items, CTRL-D to deselect-all, CTRL-S to select-all'
          )
  pbcopy < <( printf '%s\n' "${array[@]}" | head -c-1 )
}

So, I tried to move the _echo_values() to separate files and it works:
image

I'd like to know whether if any elegant way can handle call a function in --preview instead of call a shell script file

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions