Domanda

Sto cercando di fare un semplice script bash che emula il comportamento delle caselle di controllo in apparenza! Lo voglio mostrare alcune opzioni e spostare il cursore sulla casella di controllo accanto secondo la stampa chiave dei tasti freccia sinistra o destra. Ho già riuscito a farlo usando READ e ANSII sequenze di escape per rilevare i tasti freccia e io uso tput per spostare il cursore.

Il mio problema con questo è che ho bisogno di leggere una certa lettera (x per esempio) per essere premuti e poi prendere un'altra azione. Ma come posso rilevare questo premere il tasto e al tempo stesso rilevare se un tasto freccia è premuto o no ??

Per rilevare i codici ANSII ho bisogno di leggere 3 caratteri e con il carattere X (la chiave per "selezionare") ho bisogno di leggere una sola, come posso leggere 3 caratteri e allo stesso tempo leggere uno solo?

Anche io ho cercato di fare qualcosa in modo che l'utente può solo premere i tasti freccia sinistra o destra o la chiave x, ma se preme un tasto qualsiasi, non dovrebbe succedere niente!

L'ho fatto fino a questo punto:

#!/bin/bash
## Here I just print in the screen the "form" with the "checkboxes"
function screen_info(){
clear
cat <<EOF

    /\_/\_/\_/\_/\_/\_/\_/\_/\_/\_
    ||
    ||      1[ ]    2[ ]    3[ ]
    ||
    #############################



EOF
}

## This function detects the arrow keys and moves the cursor
function arrows(){

## I use ANSII escape sequences to detect the arrow keys
left_arrow=$'\x1b\x5b\x44'  #leftuierda
right_arrow=$'\x1b\x5b\x43' #rightecha
just_x_key=""

## With tput I move the cursor accordingly
cursor_x=14
cursor_y=3
tput cup $cursor_y $cursor_x

while [ -z "$just_x_key" -o "$just_x_key" != "$just_x_key" ]; do
    read -s -n3 key
    while [ `expr length "$key"` -ne 3 ]; do
        key=""
        read -s -n3 key
        break
    done
    case "$key" in
    $left_arrow)
        if [ $cursor_x -gt 14 ]; then
            cursor_x=`expr $cursor_x - 8`
        fi
        tput cup $cursor_y $cursor_x
        #This is supposed to be a simple condition detecting the x key pressed wich I want to trigger something... But how to read a single character to this condition and 3 characters at the same time if the user presses the arrow key ???? =/
        #read -s just_x_key
        #if [ $just_x_key == x ]; then
        #   echo X
        #   tput cup 7 15
        #   echo "YOU PRESSED THE RIGHT KEY!!! =D"
        #fi 
        ;;
    $right_arrow)
        if [ $cursor_x -lt 28 ]; then
            cursor_x=`expr $cursor_x + 8`
        fi
        tput cup $cursor_y $cursor_x
        #read -s just_x_key
        #if [ $just_x_key == x ]; then
        #   echo X
        #   tput cup 7 15
        #   echo "YOU PRESSED THE RIGHT KEY!!! =D"
        #fi 
        ;;
    esac
done

exit $?
}


#EXECUTION
#=========

## I just call the functions!
screen_info
arrows

Sì, lo so, non è il codice più perfetto mai ma sto cercando di imparare. Suggerimenti saranno molto apprezzati.

È stato utile?

Soluzione

Se si desidera solo le caselle di controllo in uno script, è possibile utilizzare gli strumenti whiptail(1) o dialog(1) per creare le caselle di controllo:

$ whiptail --checklist "Please pick one" 10 60 5 one one off two two off\
  three three off four four off  five five off


                 ┌──────────────────────────────────────────────────────────┐
                 │    [ ] one    one                                        │ 
                 │    [*] two    two                                        │ 
                 │    [ ] three  three                                      │ 
                 │    [*] four   four                                       │ 
                 │    [ ] five   five                                       │ 
                 │                                                          │ 
                 │              <Ok>                  <Cancel>              │ 
                 │                                                          │ 
                 └──────────────────────────────────────────────────────────┘ 

"two" "four"$ 

Il "two" "four" finale viene restituito le voci selezionate dal programma whiptail(1).

Se state programmando questo per il gusto di farlo da soli, fatemelo sapere e mi limiterò a convertire questo a un commento nella speranza che qualcun altro troverà il suggerimento utile in futuro.

Altri suggerimenti

In alternativa, è possibile utilizzare la mia biblioteca di https://sites.google.com/site/easybashgui ??

Si rileva automaticamente se finestra di dialogo o whiptail è installato. (Normalmente, almeno uno dei due è sempre presente anche nei sistemi nudi ...)

Così i vostri script diventa:

source easybashgui; menu 1 2 3 ; clean_temp

Perché reinventare la ruota? ; P

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top