aboutsummaryrefslogtreecommitdiff
path: root/helpers/bbask
blob: ad446ecc97ba9366121c4286f67012ddf248e5a1 (plain)
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
#!/bin/sh
# A simple user-input asker. Result is output to stdout.
#    Usage: ask [-1] [prompt [initial query]]
if [ "$1" = '-1' ]; then
    # Get one character of input
    tput civis >/dev/tty;
    printf '\033[1m%s\033[0m' "$2" >/dev/tty;
    stty -icanon -echo >/dev/tty 2>/dev/tty;
    if [ "$(uname)" = "Darwin" ]; then
        read -n 1 REPLY </dev/tty >/dev/tty;
        echo $REPLY
    else
        dd bs=1 count=1 2>/dev/null </dev/tty
    fi
    stty icanon echo >/dev/tty 2>/dev/tty
    tput cvvis >/dev/tty
else
    # Get a line of input
    if command -v ask >/dev/null; then
        ask --history=bb.hist --prompt="$(printf '%s\033[?25h' "$1")" --query="$2"
    else
        printf "\033[1m%s\033[0m" "$1" >/dev/tty
        tput cvvis >/dev/tty
        if [ "$(uname)" = "Darwin" ]; then
            read -e REPLY </dev/tty >/dev/tty
        else
            read REPLY </dev/tty >/dev/tty
        fi
        echo $REPLY
    fi
fi