Created
April 28, 2014 09:01
-
-
Save YukiYamashina/11366144 to your computer and use it in GitHub Desktop.
move cursor on terminal (up: k, down: j, left: h, right: l, quit: q)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
function show_coordinate() { | |
tput civis | |
tput sc | |
tput cup 0 0 ; printf "[%3d,%3d] up:k, down:j, left:h, right:l, quit:q" ${1} ${2} | |
tput rc | |
tput cnorm | |
} | |
### keyboard assignment ### | |
assign_key_up=k | |
assign_key_down=j | |
assign_key_left=h | |
assign_key_right=l | |
### set maximum size of the window ### | |
declare -i x_position_max=`tput lines` | |
declare -i y_position_max=`tput cols` | |
### initial cursor position ### | |
declare -i x_position=5 | |
declare -i y_position=10 | |
### clear terminal ### | |
clear | |
### move cursor and show the coordinate ### | |
tput cup ${x_position} ${y_position} | |
show_coordinate ${x_position} ${y_position} | |
### set terminal in raw mode ### | |
stty raw -echo | |
while : ; do | |
### real time input => ${char} ### | |
char=`dd bs=1 count=1 2>/dev/null` | |
### set position of the cursor with checking if the cursor is at the edge of the window ### | |
if [ "${char}" == ${assign_key_up} ] ; then | |
if [ ${x_position} -eq 0 ] ; then | |
x_position=${x_position_max} | |
else | |
x_position=`expr ${x_position} - 1` | |
fi | |
elif [ "${char}" == ${assign_key_down} ] ; then | |
if [ ${x_position} -eq ${x_position_max} ] ; then | |
x_position=0 | |
else | |
x_position=`expr ${x_position} + 1` | |
fi | |
elif [ "${char}" == ${assign_key_left} ] ; then | |
if [ ${y_position} -eq 0 ] ; then | |
y_position=${y_position_max} | |
else | |
y_position=`expr ${y_position} - 1` | |
fi | |
elif [ "${char}" == ${assign_key_right} ] ; then | |
if [ ${y_position} -eq ${y_position_max} ] ; then | |
y_position=0 | |
else | |
y_position=`expr ${y_position} + 1` | |
fi | |
fi | |
### move cursor ### | |
tput cup ${x_position} ${y_position} | |
### update cursor coordinate ### | |
show_coordinate ${x_position} ${y_position} | |
### "q" is defined as an escape key ### | |
if [ "${char}" == "q" ] ; then | |
clear | |
stty -raw echo | |
exit | |
fi | |
done | |
### unset terminal in raw mode ### | |
stty -raw echo | |
### clear terminal ### | |
clear |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment