-
Notifications
You must be signed in to change notification settings - Fork 0
/
chooser
executable file
·50 lines (46 loc) · 1.27 KB
/
chooser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
#set -x
: ${GUI_CHOOSER:=true}
: ${NUM_LINES:=25}
: ${CHOOSER_PROMPT:=Choose an entry}
: ${CHOOSER_SEARCH:=}
joinArray() {
IFS=$'\n'; echo "$*"
}
if [[ -z ${CHOOSER} ]]
then
if [[ -z ${SSH_CONNECTION} && "${GUI_CHOOSER}" == "true" ]]
then
# GUI can be used
if [[ "$(uname)" == "Darwin" ]]
then
type -a choose &>/dev/null && CHOOSER=choose
else
type -a bemenu &>/dev/null && CHOOSER=bemenu
fi
fi
[[ -z ${CHOOSER} ]] && type -a fzf &>/dev/null && CHOOSER=fzf
[[ -z ${CHOOSER} ]] && type -a bemenu &>/dev/null && CHOOSER=bemenu && export BEMENU_BACKEND=curses
fi
case "${CHOOSER}" in
choose)
joinArray "${@}" | choose -n ${NUM_LINES}
;;
bemenu)
joinArray "${@}" | bemenu -l ${NUM_LINES} --fn "DejaVu Sans Mono 20" --ifne -p "${CHOOSER_PROMPT}: " -P ">" -b -i
;;
fzf)
joinArray "${@}" | fzf --header="${CHOOSER_PROMPT}" +m --cycle -1 -0 --height=${NUM_LINES} -i -q "${CHOOSER_SEARCH}"
;;
*)
# Default
PS3="${CHOOSER_PROMPT}: "
select entry in "${@}"; do
if [[ " ${*} " == *" ${entry} "* ]]
then
echo "${entry}"
break
fi
done
;;
esac