man console_codes
and there we find that the escape sequence ESC [ ? 9 h
ESC [ ? 9 h
includes mouse tracking mode,ESC [ ? 9 l
ESC [ ? 9 l
turns this mode off.ESC [ M bxy
, where in b
will be information about the pressed button and modifier keys, and in x
and y
- information about the coordinates of the mouse. The symbol b
is not interesting to us. And to get the coordinates of the mouse, you need to subtract 32
from both x
and y
. LC_ALL=C printf -v code '%d' "'$data"
LC_ALL=C
requires that a character with a code greater than 127 is interpreted by itself, and not as part of a multibyte character. #!/bin/bash declare -i mouseX declare -i mouseY declare -i mouseButton declare -r ESC_CODE=$'\e' declare -r EXIT_CODE='x' printMouseInfo() { echo button=$mouseButton column=$mouseX row=$mouseY } readMouse() { local mouseButtonData local mouseXData local mouseYData read -r -s -n 1 -t 1 mouseButtonData read -r -s -n 1 -t 1 mouseXData read -r -s -n 1 -t 1 mouseYData local -i mouseButtonCode local -i mouseXCode local -i mouseYCode LC_ALL=C printf -v mouseButtonCode '%d' "'$mouseButtonData" LC_ALL=C printf -v mouseXCode '%d' "'$mouseXData" LC_ALL=C printf -v mouseYCode '%d' "'$mouseYData" ((mouseButton = mouseButtonCode)) ((mouseX = mouseXCode - 32)) ((mouseY = mouseYCode - 32)) } declare key echo -ne "\e[?9h" while true; do key="" read -r -s -t 1 -n 1 key case "$key" in $EXIT_CODE) break;; $ESC_CODE) read -r -s -t 1 -n 1 key if [[ "$key" == '[' ]]; then read -r -s -t 1 -n 1 key if [[ "$key" == "M" ]]; then readMouse printMouseInfo fi fi;; esac done echo -ne "\e[?9l"
$ ./mouse.sh button=0 column=46 row=17 button=0 column=61 row=19 button=0 column=64 row=15 button=0 column=59 row=11 button=0 column=43 row=9 button=0 column=36 row=10 button=0 column=42 row=17 button=0 column=63 row=23 button=0 column=75 row=22 button=0 column=91 row=19 $
ESC [ ? 1000 h
ESC [ ? 1000 h
, then you can get information about clicking and releasing the mouse buttons.reset
command, if you press Ctrl + C while the script is running. This is very convenient when developing, since we use colors in the game and suppress user input, and if the script is interrupted, until you randomly reset
, the terminal will be in a state completely unsuitable for work.trap
command is used: trap
parameter can be found by typing trap -l
. If the
parameter is not present, the default action will be set. If you specify EXIT
as the
parameter, the specified command will be executed upon completion of the script, and this is what we need. We write: function initApplication() { stty -echo echo -ne $HIDE_CURSOR_CODE trap finishApplication EXIT ... } function finishApplication() { trap EXIT reset } initApplication runApplication finishApplication
SIGWINCH
signal: function repaint() { LINES=`tput lines` COLUMNS=`tput cols` mapXPosition=$(((COLUMNS - CELL_WIDTH * MAP_WIDTH) / 2 + 1)) mapYPosition=$(((LINES - CELL_HEIGHT * MAP_HEIGHT) / 2 + 1)) timerXPosition=$((MAP_WIDTH * CELL_WIDTH + mapXPosition + 6)) timerYPosition=$((mapYPosition)) echo -ne "\e[0m" clear drawMap drawHeader drawFooter ((isInvalidated = 0)) } function initApplication() { ... trap "((isInvalidated = 1))" SIGWINCH } function runGame() { local key ... while true; do if ((isInvalidated)); then repaint fi ... key="" ... case "$key" in $NEW_GAME_CODE) continue 2;; $EXIT_CODE) break 2;; $ESC_CODE) ... esac ... done }
DEBUG
as a
, then the specified command will be executed after each script command, sometimes it is useful when debugging.man bash
, man console_codes
and ABS can be read endlessly, and each time discovering newer and newer facets of bash programming.Source: https://habr.com/ru/post/131921/
All Articles