
# print and color



    function c {
        # color
        local c="\e["
        while [[ "$#" -gt 1 ]]; do
            case "$1" in
                b) c+='1;';;
                r) c+='0;';;
                i) c+='7;';;
                bg) _background=true
            esac
            shift
        done
        case "$1" in
            r) c+='31m';;
            g) c+='32m';;
            y) c+='33m';;
            b) c+='34m';;
            p) c+='35m';;
            c) c+='36m';;
            w) c+='37m';;
            n) c+='38m';;
            R) c+='0m'
        esac
        echo -ne "$c"
    }

    function warn {
        echo -e "[$(c y)warn$(c R)]" "$@"
    }
    function err {
        echo -e "[$(c r)err $(c R)]" "$@"
    }
    function info {
        echo -e "[$(c b)info$(c R)]" "$@"
    }



# menus

    function curpos {
        read -rsdR -p $'\e[6n' curpos
        curpos="${curpos:2}"
        IFS=';' read -r l c <<< "$curpos"
        case "$1" in
            c|col|column) echo -n "$c";;
            l|line) echo -n "$l"
        esac
    }


    function ask {
        while true; do
            if [[ "$2" == '' ]]; then
                echo "$1"
            else
                "$2" "$1"
            fi
            read -rn1 -p "[$(c g)y$(c R)/$(c r)n$(c R) ]? " yn
            echo
            case "$yn" in
                y|Y) return 0;;
                n|N) return 1;;
                *) err "Invalid. Enter y for yes or n for no."
            esac
        done
    }

    function checklist {
        tput civis >/dev/stderr
        stty -echo
        local sel=1
        local cnt=1
        local line=1
        # local q="*"
        # uncheck all
        declare -A checked
        for i in "$@"; do
            checked[$i]='n'
        done
        # free space for checklist
        echo -ne '\n\n\n\n\n\n\n\n' >/dev/stderr
        tput cuu 8 >/dev/stderr
        echo '==========================' >/dev/stderr
        # shellcheck disable=2155
        local cline="$(curpos l)"
        local height=$((LINES-cline-2))
        tput sc >/dev/stderr
        while true; do
            cnt=1
            for i in "$@"; do
                if [[ $cnt -ge $line ]] && [[ $cnt -le $((line+height)) ]]; then
                    if [[ ${checked[$i]} == y ]]
                    then echo -n "[$(c g)x$(c R)] " >/dev/stderr
                    else echo -n "[ ] " >/dev/stderr
                    fi
                    [[ $cnt == "$sel" ]] && c i n >/dev/stderr
                    echo -n "$i$(c R)" >/dev/stderr
                    eval "printf -- ' %.0s' {1..$((COLUMNS-${#i}-4))}" >/dev/stderr
                    echo >/dev/stderr
                fi
                ((cnt++))
            done
            echo -n '==========================' >/dev/stderr
            IFS=$'\t' read -rsn1 in
            case "$in" in
                $'\e')
                    read -rsn1 in
                    case "$in" in
                        '[' )
                            read -rsn1 in
                            case "$in" in
                                A) ((sel--)); [[ $sel -lt 1 ]] && sel=1;;
                                B) ((sel++)); [[ $sel -gt "$#" ]] && sel=$#;;
                            esac
                        ;;
                    esac
                ;;
                ' ')
                    if [[ "${checked[${@:$sel:1}]}" == y ]]; then
                        checked[${@:$sel:1}]=n
                    else
                        checked[${@:$sel:1}]=y
                    fi
                ;;
                '/')
                echo -ne '\r                           \r' >/dev/stderr
                ;;
                q|'') break;;
            esac
            if [[ $sel -lt $line ]]; then
                line=$sel
            elif [[ $sel -gt $((line+height)) ]]; then
                line=$((sel-height))
            fi
            tput rc >/dev/stderr
        done
        tput cnorm >/dev/stderr
        for i in "$@"; do
            if [[ "${checked[$i]}" == y ]]; then
                echo -n "$i "
            fi
        done
        echo ''>/dev/stderr
        stty echo
    }

    function radiolist {
        tput civis >/dev/stderr
        stty -echo
        local sel=1
        local cnt=1
        local line=1
        # local q="*"
        # uncheck all
        menu=false
        if [[ "$1" == '--menu' ]]; then
            menu=true; shift
        fi
        local check=1
        # free space for checklist
        echo -ne '\n\n\n\n\n\n\n\n' >/dev/stderr
        tput cuu 8 >/dev/stderr
        echo '==========================' >/dev/stderr
        # shellcheck disable=2155
        local cline="$(curpos l)"
        local height=$((LINES-cline-2))
        tput sc >/dev/stderr
        while true; do
            cnt=1
            for i in "$@"; do
                if [[ $cnt -ge $line ]] && [[ $cnt -le $((line+height)) ]]; then
                    if "$menu"; then
                        if [[ $cnt == "$check" ]]
                        then echo -n " $(c g)>$(c R) " >/dev/stderr
                        else echo -n "   " >/dev/stderr
                        fi
                    else
                        if [[ $cnt == "$check" ]]
                        then echo -n "[$(c g)x$(c R)] " >/dev/stderr
                        else echo -n "[ ] " >/dev/stderr
                        fi
                    fi
                    [[ $cnt == "$sel" ]] && c i n >/dev/stderr
                    echo -n "$i$(c R)" >/dev/stderr
                    eval "printf -- ' %.0s' {1..$((COLUMNS-${#i}-4))}" >/dev/stderr
                    echo >/dev/stderr
                fi
                ((cnt++))
            done
            echo -n '==========================' >/dev/stderr
            IFS=$'\t' read -rsn1 in
            case "$in" in
                $'\e')
                    read -rsn1 in
                    case "$in" in
                        '[' )
                            read -rsn1 in
                            case "$in" in
                                A) ((sel--)); [[ $sel -lt 1 ]] && sel=1;;
                                B) ((sel++)); [[ $sel -gt "$#" ]] && sel=$#;;
                            esac
                        ;;
                    esac
                ;;
                ' ')
                    check=$sel
                ;;
                '/')
                echo -ne '\r                           \r' >/dev/stderr
                ;;
                q|'') break;;
            esac
            if [[ $sel -lt $line ]]; then
                line=$sel
            elif [[ $sel -gt $((line+height)) ]]; then
                line=$((sel-height))
            fi
            "$menu" && check="$sel"
            tput rc >/dev/stderr
        done
        tput cnorm >/dev/stderr
        eval "echo \"\$$check\""
        echo ''>/dev/stderr
        stty echo
    }

#

