metal3d / bashsimplecurses

A simple curses library made in bash to draw terminal interfaces
BSD 3-Clause "New" or "Revised" License
902 stars 117 forks source link

[question] how do i do anything with choices? #56

Closed ghost closed 1 year ago

ghost commented 2 years ago

im not a big bash scripter, i barely know anything, yet id still like to know how to actually do anything with the choice you can pick in the choices example. ive changed it a little like you see here now:

#i ran: (commandname) list add remove

source /usr/local/lib/simple_curses.sh

declare -a SELECTABLES
while [[ $# -gt 0 ]]; do
  # shellcheck disable=SC2034
  case "$1" in
  -- ) shift; break ;;
  * ) SELECTABLES+=("$1"); shift ;;
  esac
done
# SELECTABLES=( "$@" )
arraylen=${#SELECTABLES[@]}
selected=0
lastselected=$selected

main(){
    window "Would you like to list/add/remove services?" "red" "50%"

    local count
    count=0
    for i in ${SELECTABLES[@]};do
        bgcolor="black"
        color="blue"
        [ "$selected" == "$count" ] && {
            bgcolor="blue"
            color="red"
        }
        append "$i" $color $bgcolor
        count=$(( count + 1 ))
    done
    endwin
    lastselected=$selected
}
update(){
    local ret
    local success

    read -r -n 1 -s -t 1 ret
    success=$?

    if [ "$success" -lt 128 ]; then
        if [ "$ret" == $'\e' ];then
            read -sN2 -r k1
            success=$?
            ret+=${k1}
        fi

        case $ret in
            [0-9])
                selected=$(( $ret - 1 ))
            ;;
            $'\e[A'|$'\e[D')
                selected=$(( selected - 1 ))
                [ "$selected" -lt "0" ] && selected=$(( arraylen - 1))
            ;;
            $'\e[B'|$'\e[C')
                selected=$(( selected + 1 ))
                [ "$selected" == "$arraylen" ] && selected="0"
            ;;
            $'\e')
                return 255
            ;;
            *)
                return $(( $lastselected + 1 ))
            ;;
        esac
    fi

    return 0
}

main_loop "$@"

i dont understand the update function but i can figure the rest, i just want to know how i can, for example, make a new menu popup when i pick one of the choices?

hugocm93 commented 2 years ago

When you pick a choice (selected is modified), you could call a script that displays a menu for example.

ghost commented 2 years ago

thats what im trying to do, i just dont know how to since im not sure how the choices work in themselves. could you maybe give an example? thank you.

hugocm93 commented 2 years ago
case $ret in
    $'0')
        # 0 was picked
        command menu0.sh
    ;;
    $'1')
        # 1 was picked
       command menu1.sh
    ;;
    *)
        # do nothing
    ;;
esac
ghost commented 2 years ago

thank you so much! will use this