@superstep, rieccomi, avevo bisogno di un terminale!
Codice: Seleziona tutto
_stty_save=""
function con_async(){
if [[ $1 -eq 0 ]]; then
stty $_stty_save
else
_stty_save=`stty -g`
stty -echo -icanon -ixon min 1 time 0
fi
}
function con_kbhit(){
read -t0
return $?
}
function con_getch(){
read -n 1 ret
eval "$1=$ret"
}
ora che hai queste tre funzioni prima di tutto abiliti la modalità senza echo:
poi puoi ad esempio attendere che venga premuto un tasto con
infine lo puoi leggere e visualizzare
infine ripristini tutto
l'esempio completo:
Codice: Seleziona tutto
#!/bin/bash
_stty_save=""
function con_async(){
if [[ $1 -eq 0 ]]; then
stty $_stty_save
else
_stty_save=`stty -g`
stty -echo -icanon -ixon min 1 time 0
fi
}
function con_kbhit(){
read -t0
return $?
}
function con_getch(){
read -n 1 ret
eval "$1=$ret"
}
con_async 1
until con_kbhit; do
sleep 0.02
done
con_getch ef
echo "read $ef"
con_async 0
se ti servono piu funzioni per il terminale
easyconsole
Codice: Seleziona tutto
#!/bin/bash
##############
#easy console#
##############
_stty_save=""
function con_async(){
if [[ $1 -eq 0 ]]; then
stty $_stty_save
else
_stty_save=`stty -g`
stty -echo -icanon -ixon min 1 time 0
fi
}
function con_kbhit(){
read -t0
return $?
}
function con_getch(){
read -n 1 ret
eval "$1=$ret"
}
function con_delay(){
#1 millisecond
t="$(echo "$1.0/1000.0" | bc -l)";
sleep $t
}
function con_gotorc(){
#$1 row, $2 col
printf "\033[$1;$2""f"
}
_row=0
_col=0
function con_getrc(){
#return val in _row _col
exec < /dev/tty
oldstty=$(stty -g)
stty raw -echo min 0
echo -en "\033[6n" > /dev/tty
IFS=';' read -r -d R -a pos
stty $oldstty
_row=$((${pos[0]:2} - 1))
_col=$((${pos[1]} - 1))
}
function con_cls(){
printf "\033[H\033[J"
}
function con_clsline(){
#$1 mode
if [[ $1 == "right" ]]; then
md="0K"
elif [[ $1 == "left" ]]; then
md="1K"
elif [[ $1 == "all" ]]; then
md="2K"
elif [[ $1 == "down" ]]; then
md="J"
elif [[ $1 == "up" ]]; then
md="1J"
fi
printf "\033[$md"
}
function con_setcolor(){
#$1 foreground, $2 background
cf=""
cb=""
if [[ $1 == 'black' ]]; then
cf="30m"
elif [[ $1 == 'red' ]]; then
cf="31m"
elif [[ $1 == 'green' ]]; then
cf="32m"
elif [[ $1 == 'yellow' ]]; then
cf="33m"
elif [[ $1 == 'blue' ]]; then
cf="34m"
elif [[ $1 == 'magenta' ]]; then
cf="35m"
elif [[ $1 == 'cyan' ]]; then
cf="36m"
elif [[ $1 == 'lgray' ]]; then
cf="37m"
elif [[ $1 == 'dgray' ]]; then
cf="90m"
elif [[ $1 == 'lred' ]]; then
cf="91m"
elif [[ $1 == 'lgreen' ]]; then
cf="92m"
elif [[ $1 == 'lyellow' ]]; then
cf="93m"
elif [[ $1 == 'lblue' ]]; then
cf="94m"
elif [[ $1 == 'lmagenta' ]]; then
cf="95m"
elif [[ $1 == 'lcyan' ]]; then
cf="96m"
elif [[ $1 == 'white' ]]; then
cf="97m"
elif [[ $1 == "reset" ]]; then
cf="m"
fi
if [[ $2 == 'black' ]]; then
cb="40m"
elif [[ $2 == 'red' ]]; then
cb="41m"
elif [[ $2 == 'green' ]]; then
cb="42m"
elif [[ $2 == 'yellow' ]]; then
cb="43m"
elif [[ $2 == 'blue' ]]; then
cb="44m"
elif [[ $2 == 'magenta' ]]; then
cb="45m"
elif [[ $2 == 'cyan' ]]; then
cb="46m"
elif [[ $2 == 'lgray' ]]; then
cb="47m"
elif [[ $2 == 'dgray' ]]; then
cb="100m"
elif [[ $2 == 'lred' ]]; then
cb="101m"
elif [[ $2 == 'lgreen' ]]; then
cb="102m"
elif [[ $2 == 'lyellow' ]]; then
cb="103m"
elif [[ $2 == 'lblue' ]]; then
cb="104m"
elif [[ $2 == 'lmagenta' ]]; then
cb="105m"
elif [[ $2 == 'lcyan' ]]; then
cb="106m"
elif [[ $2 == 'white' ]]; then
cb="107m"
fi
if [[ $cf ]]; then
printf "\033[$cf"
fi
if [[ $cb ]]; then
printf "\033[$cb"
fi
}
function con_setcolor256(){
#$1 foreground, $2 background
if [[ $1 == "reset" ]]; then
printf "\033[m"
else
fr="$1"
bk="$2"
if [[ $bk != "" ]]; then
printf "\033[48;5;$bk""m"
fi
if [[ $fr != "" ]]; then
printf "\033[38;5;$fr""m"
fi
fi
}
function con_showcursor(){
#$1 bool
if [[ $1 == 0 ]]; then
en="l"
else
en="h"
fi
printf "\033[?25$en"
}
function con_special(){
#1 specialchar
if [[ $1 == "horizontal" ]]; then
sc='q'
elif [[ $1 == "vertical" ]]; then
sc='x'
elif [[ $1 == "upleft" ]]; then
sc='l'
elif [[ $1 == "upright" ]]; then
sc='k'
elif [[ $1 == "downleft" ]]; then
sc='m'
elif [[ $1 == "downright" ]]; then
sc='j'
elif [[ $1 == "center" ]]; then
sc='n'
elif [[ $1 == "interceptup" ]]; then
sc='w'
elif [[ $1 == "interceptleft" ]]; then
sc='t'
elif [[ $1 == "interceptright" ]]; then
sc='u'
elif [[ $1 == "interceptdown" ]]; then
sc='v'
else
sc="$1"
fi
printf "\033(0$sc\033(B"
}
function con_line(){
#$1 row1, $2 col1, $3 row2, $4 col2, $5 char
stepx=0
stepy=0
fraction=0
x0="$2"
x1="$4"
y0="$1"
y1="$3"
ch="$5"
if [[ $x0 != $x1 ]]; then
if [[ $x1 < $x0 ]]; then
((x0 += 1))
else
((x0 -= 1))
fi
fi
if [[ $y0 != $y1 ]]; then
if [[ $y1 < $y0 ]]; then
((y0 += 1))
else
((y0 -= 1))
fi
fi
dx=$((x1 - x0))
dy=$((y1 - y0))
if [[ $dx < 0 ]]; then
((dx = -dx))
stepx=-1
else
stepx=1
fi
if [[ $dy < 0 ]]; then
((dy = -dy))
stepy=-1
else
stepy=1
fi
((dx *= 2))
((dy *= 2))
if [[ $dx > $dy ]]; then
fraction=$((dy - dx / 2))
while [[ $x0 != $x1 ]]; do
((x0 += stepx))
if (( $fraction >= 0 )); then
((y0 += stepy))
((fraction -= dx))
fi
((fraction += dy))
con_gotorc $y0 $x0
printf "$ch"
done
else
fraction=$((dx - dy / 2))
while [[ y0 != y1 ]]; do
((y0 += stepy))
if (( fraction >= 0 )); then
((x0 += stepx))
((fraction -= dy))
fi
((fraction += dx))
con_gotorc $y0 $x0
printf "$ch"
done
fi
}
function con_rect(){
#$1 y, $2 x, $3 height, $4 width
y="$1"
x="$2"
h="$3"
w="$4"
nw=$((w - 2))
con_gotorc $y $x
con_special upleft
i=0
while [[ $i -lt $nw ]]; do
con_special horizontal
(( i += 1 ))
done
con_special upright;
ly=$((y + h - 1))
con_gotorc $ly $x
con_special downleft
i=0
while [[ $i -lt $nw ]]; do
con_special horizontal
(( i += 1 ))
done
con_special downright
(( y += 1 ))
(( h -= 1 ))
rx=$(( x + w - 1))
i=$((y))
nh=$((y + h - 1))
while [[ $i -lt $nh ]]; do
con_gotorc $i $x
con_special vertical
con_gotorc $i $rx
con_special vertical
(( i += 1 ))
done
}
function con_fillrect(){
#$1 sty, $2 stx, $3 height, $4 width
y=$(($1 + 1))
x=$(($2 + 1))
h=$(($3 - 2))
w=$(($4 - 2))
ch="$5"
ir=0
while [[ $ir -lt $h ]]; do
ic=0
con_gotorc $y $x
while [[ $ic -lt $w ]]; do
printf $ch
((ic += 1))
done
((y += 1))
((ir += 1))
done
}
function con_circle(){
#$1 row, $2 col, $3 raggio, $4 char
r=$1
c=$2
x=$3;
y=0;
xc=$(( 1 - 2 * $3));
yc=1;
re=0;
while (( x>= y )); do
con_gotorc $((r + y)) $((c + x))
printf $4
con_gotorc $((r + y)) $((c - x))
printf $4
con_gotorc $((r - y)) $((c - x))
printf $4
con_gotorc $((r - y)) $((c + x))
printf $4
con_gotorc $((r + x)) $((c + y))
printf $4
con_gotorc $((r + x)) $((c - y))
printf $4
con_gotorc $((r - x)) $((c -y ))
printf $4
con_gotorc $((r - x)) $((c + y))
printf $4
((y += 1))
((re += yc))
((yc += 2));
if (( 2 * re + xc > 0 )); then
((x -= 1))
((re += xc))
((xc += 2))
fi
done
}
function con_ellipse(){
#$1 cr, $2 cc, $3 sr, $4 sc, $5 char
cr=$1
cc=$2
sr=$3
sc=$4
ch=$5
rx_2=$((sc * sc));
ry_2=$((sr * sr));
p=$((ry_2 - rx_2 * sr + ry_2 / 2))
x=0;
y=sr;
two_ry_2_x=0;
two_rx_2_y=$((rx_2 * 2 * y));
con_gotorc $((cr + y)) $((cc + x))
printf $ch
con_gotorc $((cr + y)) $((cc - x))
printf $ch
con_gotorc $((cr - y)) $((cc - x))
printf $ch
con_gotorc $((cr - y)) $((cc + x))
printf $ch
while ((two_rx_2_y >= two_ry_2_x)); do
((x += 1))
((two_ry_2_x += ry_2 * 2))
((p += two_ry_2_x + ry_2))
if ((p >= 0)); then
((y -= 1))
((two_rx_2_y -= rx_2 * 2))
((p -= two_rx_2_y))
fi
con_gotorc $((cr + y)) $((cc + x))
printf $ch
con_gotorc $((cr + y)) $((cc - x))
printf $ch
con_gotorc $((cr - y)) $((cc - x))
printf $ch
con_gotorc $((cr - y)) $((cc + x))
printf $ch
done
p=$((ry_2 * (x + 1) * (x + 1) + rx_2 * (y - 1) * ( y - 1 ) - rx_2 * ry_2))
while (( y >= 0 )); do
((p += rx_2))
((y -= 1))
((two_rx_2_y -= rx_2 * 2))
((p -= two_rx_2_y))
if (( p <= 0 )); then
((x += 1))
((two_ry_2_x += ry_2 * 2))
((p += two_ry_2_x))
fi
con_gotorc $((cr + y)) $((cc + x))
printf $ch
con_gotorc $((cr + y)) $((cc - x))
printf $ch
con_gotorc $((cr - y)) $((cc - x))
printf $ch
con_gotorc $((cr - y)) $((cc + x))
printf $ch
done
}
function con_getmaxrc(){
#return val in _row _col
_row=$LINES
_col=$COLUMNS
}
function con_resize(){
#$1 height, $2 width
printf "\033[8;$1;$2""t"
}
_msg_row=0
_msg_col=0
function con_msg(){
#$1 status, $2 message
status=$1
message=$2
colstat="reset"
en=0
if (( $status >= 100 )); then
status='OK'
colstat="lgreen"
en=1
elif (( $status < 0 )); then
status='ER'
colstat="lred"
en=1
elif (( $status == 0 )); then
status='..'
colstat="white"
else
colstat="white"
if (( $status < 10 )); then
status="0$status"
fi
fi
if (( $# < 2 )); then
con_gotorc $_msg_row 2
con_setcolor $colstat
printf '..'
con_gotorc $_msg_row 2
con_setcolor $colstat
printf $status
con_setcolor "reset"
con_gotorc $_msg_row $_msg_col
else
printf "["
con_setcolor $colstat
printf $status
con_setcolor "reset"
printf "] $message"
con_getrc
_msg_row=$((_row + 1))
_msg_col=$_col
fi
if (( en == 1 )); then
echo ""
fi
}
function con_carret_up(){
#$1 nup
printf "\033[$1""A"
}
function con_carret_down(){
#$1 ndown
printf "\033[$1""B"
}
function con_carret_next(){
#$1 nnext
printf "\033[$1""C"
}
function con_carret_prev(){
#$1 nprev
printf "\033[$1""D"
}
function con_carret_home(){
printf "\033[H"
}
function con_carret_end(){
printf "\033[H"
}
function con_carret_save(){
printf "\033[s"
}
function con_carret_restore(){
printf "\033[8"
}
function con_scrool_up(){
printf "\033[M"
}
function con_scrool_down(){
printf "\033[D"
}
function con_carret_delete(){
#$1 ndelete
printf "\033[$1""P"
}
function con_mode_ins(){
#$1 mode
if [[ $1 == 'i' ]]; then
printf "\033[4h"
else
printf "\033[4l"
fi
}
function con_font_attribute(){
#$1 attribute
a=0
if [[ $1 == "reset" ]]; then
a=0
elif [[ $1 == "bold" ]]; then
a=1
elif [[ $1 == "half" ]]; then
a=2
elif [[ $1 == "italic" ]]; then
a=3
elif [[ $1 == "underline" ]]; then
a=4
elif [[ $1 == "crossed" ]]; then
a=9
fi
printf "\033[$a""m"
}
function con_bip(){
if [[ $1 == "" ]]; then
paplay /usr/share/sounds/freedesktop/stereo/bell.oga
else
paplay $1
fi
}
ad esempio(stupido):
Codice: Seleziona tutto
#!/bin/bash
source ./easyconsole
con_font_attribute bold
con_setcolor white
echo "Hello World"
con_setcolor reset
con_font_attribute reset
con_msg 0 "wait key"
con_async 1
until con_kbhit; do
con_delay 5
done
con_getch kpress
con_async 0
if [[ $kpress == 'e' ]]; then
con_msg -1
else
con_msg 100
fi