#!/usr/bin/env bash # Wrapper to prompt user to select an item from a list. # Uses rofi on linux, and choose-gui on mac # https://github.com/davatorium/rofi # https://github.com/chipsenkbeil/choose declare -a linux_options linux_options=() usage() { echo "Usage: command | picker [-p PROMPT] Accepts [-p PROMPT] to pass to rofi. Ignored on mac. Reads lines from STDIN and uses rofi/choose-gui to pick one" exit "${1:-0}" } if [[ "$1" == '-h' ]]; then usage fi # get user input from STDIN STDIN="$(cat)" || exit $? if [[ -z "$STDIN" ]]; then echo -e 'picker: No input received from STDIN\n' >&2 usage 1 fi ## read options while getopts "p:" OPT; do case "$OPT" in p) linux_options+=("-${OPT}" "$OPTARG") ;; *) usage 1 ;; esac done shift $((OPTIND - 1)) # if this succeeds, it exits the script. Otherwise, uses fzf os_specific() { case "${ON_OS:=$(on_machine)}" in linux*) havecmd -V 'linux picker uses rofi: https://github.com/davatorium/rofi' rofi || return 0 declare LINES LINES="$(wc -l <<<"$STDIN")" # if STDIN is less than 15 lines, limit the list view to that number ((LINES < 15)) && linux_options+=(-lines "$LINES") printf '%s' "$STDIN" | rofi -i -dmenu "${linux_options[@]}" || exit $? exit 0 ;; mac*) havecmd -V 'mac picker uses choose: https://github.com/chipsenkbeil/choose' choose || return 0 printf '%s' "$STDIN" | choose -c 9400D3 -b 1C1C1C -u || exit $? exit 0 ;; esac } os_specific || true if havecmd fzf; then fzf <<<"$STDIN" || exit $? else echo 'fzf not found, not sure how to prompt' >&2 exit 1 fi