やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

whiptail bashでTUI作成するコマンド

 GUI環境がなくても使える。

help

$ whiptail
Box options: 
    --msgbox <text> <height> <width>
    --yesno  <text> <height> <width>
    --infobox <text> <height> <width>
    --inputbox <text> <height> <width> [init] 
    --passwordbox <text> <height> <width> [init] 
    --textbox <file> <height> <width>
    --menu <text> <height> <width> <listheight> [tag item] ...
    --checklist <text> <height> <width> <listheight> [tag item status]...
    --radiolist <text> <height> <width> <listheight> [tag item status]...
    --gauge <text> <height> <width> <percent>
Options: (depend on box-option)
    --clear             clear screen on exit
    --defaultno         default no button
    --default-item <string>       set default string
    --fb, --fullbuttons     use full buttons
    --nocancel          no cancel button
    --yes-button <text>       set text of yes button
    --no-button <text>        set text of no button
    --ok-button <text>        set text of ok button
    --cancel-button <text>        set text of cancel button
    --noitem            don't display items
  --notags            don't display tags
    --separate-output       output one line at a time
    --output-fd <fd>      output to fd, not stdout
    --title <title>           display title
    --backtitle <backtitle>       display backtitle
    --scrolltext            force vertical scrollbars
    --topleft           put window in top-left corner
    -h, --help          print this message
    -v, --version           print version information

表示系

--msgbox

whiptail --msgbox "メッセージです。\n\n複数行も表現できます。" 0 0

f:id:ytyaru:20190313093743p:plain

key 説明
Enter or Space 了解

--yesno

whiptail --yesno "質問です。\n\n実行しますか?" 0 0

 直後に$?を参照すると選択した項目に応じた値が入る。

echo $?
選択肢 $?
はい 0
いいえ 1

f:id:ytyaru:20190313094616p:plain

--infobox

TERM=vt100 whiptail --infobox "infoboxですが何か?\n\n標準出力では表示できませんが何か?\n複数行表示できませんが何か?" 0 0

f:id:ytyaru:20190313095117p:plain

 これ何が嬉しいの?

--textbox

/tmp/work/a.txt

aaaaaa
bbbbbbあ
いいいいいいいいいい
うううううううううううううう
whiptail --textbox /tmp/work/a.txt 0 0

f:id:ytyaru:20190313095242p:plain

 全文表示されない……。サイズを大きめに指定すると全文表示された。

whiptail --textbox /tmp/work/a.txt 10 80

f:id:ytyaru:20190313095331p:plain

--gauge

seq -f'echo %g;sleep 0.1' 100 | bash | \
whiptail --gauge "進捗率ですよ。" 0 0 0

f:id:ytyaru:20190313095636p:plain

編集系

--inputbox [init]

whiptail --inputbox "名前を入力してください。" 0 0

 入力値が標準出力として返ってくる。

 変数に代入したいときは以下。

NAME=$(whiptail --inputbox "名前を入力してください。" 0 0  3>&1 1>&2 2>&3)
echo $NAME

 3>&1 1>&2 2>&3は標準出力と標準エラー出力を入れ替えている。

 初期値を設定したいときは以下。

whiptail --inputbox "名前を入力してください。" 0 0 "山田太郎"

f:id:ytyaru:20190313101608p:plain

  • 日本語入力もできる

--passwordbox [init]

whiptail --passwordbox "パスワードを入力してください。" 0 0

f:id:ytyaru:20190313101933p:plain

whiptail --passwordbox "パスワードを入力してください。" 0 0 "初期値"

 <text>を表示するには<height>7以上にする必要がある。ひどい。

whiptail --passwordbox "パスワードを入力してください。" 7 0 "初期値"
  • <text>が表示されない(英字にしても同様。<height>7以上にすると表示された)
  • 初期値だと******になった(1文字2つの*。BackSpace1回で2つの*が消えた)

--menu [tag item] ...

whiptail --menu "メニューです。選べよ。" 0 0 0 0 "白米" 1 "ご飯" 2 "白飯" 3 "日の丸弁当" 4 "白にぎり飯"

f:id:ytyaru:20190313103317p:plain

key 説明
, フォーカス遷移
Space チェック切替
Enter 決定(了解)
ESC キャンセル(取消)

 引数[tag item]の書式は以下。

戻り値1 表示値1
戻り値2 表示値2
戻り値3 表示値3
...

 改行をいれると少しわかりやすい。

whiptail --menu "メニューです。選べよ。" 0 0 0 \
0 "白米" \
1 "ご飯" \
2 "白飯" \
3 "日の丸弁当" \
4 "白にぎり飯"

 選択した項目を変数に代入するなら以下。

selected=$(whiptail --menu "メニューです。選べよ。" 0 0 0 \
0 "白米" \
1 "ご飯" \
2 "白飯" \
3 "日の丸弁当" \
4 "白にぎり飯" \
3>&1 1>&2 2>&3) 
echo $selected

--checklist [tag item status]...

whiptail --checklist "チェックリスト" 0 0 0 0 作業0 ON 1 作業1 OFF 2 作業2 ON

f:id:ytyaru:20190313111503p:plain

key 説明
, フォーカス遷移
Space チェック切替
Enter 決定(了解)
ESC キャンセル(取消)

 了解するとONの項目のtagが返される。

"0" "2"

 スペース区切りで""囲み。

 改行でわかりやすくする。

whiptail --checklist "チェックリスト" 0 0 0 \
0 作業0 ON \
1 作業1 OFF \
2 作業2 ON

 戻り値を変数に代入する。

selected=$(whiptail --checklist "チェックリスト" 0 0 0 \
0 作業0 ON \
1 作業1 OFF \
2 作業2 ON \
3>&1 1>&2 2>&3) 
echo $selected

--radiolist [tag item status]...

whiptail --radiolist "ラジオリスト" 0 0 0 0 選択肢0 ON 1 選択肢1 OFF 2 選択肢2 OFF

f:id:ytyaru:20190313111522p:plain

key 説明
, フォーカス遷移
Space チェック切替
Enter 決定(了解)
ESC キャンセル(取消)

 了解するとONの項目のtagが返される。""で囲まれない。

0

 戻り値を変数に代入する。

selected=$(whiptail --radiolist "ラジオリスト" 0 0 0 \
0 選択肢0 ON \
1 選択肢1 OFF \
2 選択肢2 OFF \
3>&1 1>&2 2>&3) 
echo $selected

 &&でつなげてワンライナーにしたら以下。

selected=$(whiptail --radiolist "ラジオリスト" 0 0 0 \
0 選択肢0 ON \
1 選択肢1 OFF \
2 選択肢2 OFF \
3>&1 1>&2 2>&3) && echo $selected

おまけ

dialog

 dialogコマンドをインストールするとカレンダーなどのTUIが使えるらしい。

 whiptailに無くてdialogに有るのは以下。

  --calendar     <text> <height> <width> <day> <month> <year>
  --dselect      <directory> <height> <width>
  --editbox      <file> <height> <width>
  --fselect      <filepath> <height> <width>
  --inputmenu    <text> <height> <width> <menu height> <tag1> <item1>...
  --pause        <text> <height> <width> <seconds>
  --tailbox      <file> <height> <width>
  --tailboxbg    <file> <height> <width>
  --timebox      <text> <height> <width> <hour> <minute> <second>

zenity

 GUI作成するコマンド。

対象環境

  • Raspbierry pi 3 Model B+
  • Raspbian stretch 9.0 2018-11-13
  • bash 4.4.12
    • zenity 3.28.1
    • whiptail 0.52.19
$ uname -a
Linux raspberrypi 4.14.98-v7+ #1200 SMP Tue Feb 12 20:27:48 GMT 2019 armv7l GNU/Linux