Skip to content

Instantly share code, notes, and snippets.

@kidd
Last active October 4, 2023 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidd/12112ffc44f79ea8b44cadf48243c6a4 to your computer and use it in GitHub Desktop.
Save kidd/12112ffc44f79ea8b44cadf48243c6a4 to your computer and use it in GitHub Desktop.
pure shell naive version of a picker like fzf (without the filtering, but only picking by number)
# fzf is either fzf or a naive version of it
# the input is a sed line number, so it can be
# single number: 42
# range: 1,10
# separate lines: 10p;50p
mute command -v fzf ||
fzf() {
local in=$(cat)
for p in "${@}"; do
[ "$p" = "-0" ] && [ "$(echo "$in" | wc -l)" -eq 1 ] && [ "" = "$in" ] && return 1
[ "$p" = "-1" ] && [ "$(echo "$in" | wc -l)" -eq 1 ] && [ "" != "$in" ] && echo "$in" && return
done
# https://superuser.com/questions/1748550/read-from-stdin-while-piping-to-next-command
cat -n <(echo "$in") >/dev/tty
read -n num </dev/tty
echo "$in" | sed -n "${num}p"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment