#!/usr/bin/env bash # prompts the user with a basic dialog command -v tttlog >/dev/null 2>&1 && tttlog "$(basename "$0")" "$@" usage() { echo 'Prompts user with an OS dialog, prints what they typed to STDOUT' exit "${1:-0}" } [[ "$1" == "-h" ]] && usage [[ -z "$1" ]] && { echo -e "Pass the prompt string as the first argument\n" 2>&1 usage 1 } case "${ON_OS:=$(on_machine)}" in linux*) havecmd -v rofi || exit $? # use rofi with no list on linux rofi -dmenu -theme-str 'listview {enabled: false;}' -p "$1" || exit $? ;; mac*) # use applescript on mac OSASCRIPT="$(printf 'set T to text returned of (display dialog "%s" buttons {"Cancel", "OK"} default button "OK" default answer "")' "$1")" RESP="$(osascript -e "$OSASCRIPT" 2>/dev/null)" || exit $? [[ -z "$RESP" ]] && exit 1 printf '%s' "$RESP" ;; android_termux*) havecmd -V 'See https://stedolan.github.io/jq/' jq || exit $? havecmd -V 'install the termux API package: https://github.com/termux/termux-api' termux-dialog || exit $? RESP="$(termux-dialog -t "$1")" CODE="$(jq -r .code <<<"$RESP")" [[ "$CODE" != -1 ]] && exit 1 jq -r .text <<<"$RESP" ;; *) echo 'Not able to prompt for input...' >&2 printf '%s\n' "$1" ;; esac