学习完 bash 操作屏幕的成果
发表于 : 2011-08-03 20:48
[bash]#!/bin/bash
# Screen-funcs
# proto: e(...)
# Print a Command Sequence Introducer
e() { echo -ne "\e[$*"; }
# proto: pos(y, x)
# Position the cursor
pos() { e "$1;$2H"; }
# proto: topleft()
# Move cursor to the top-left corner
topleft() { e "H"; }
# proto: clear_screen()
# Clear screen but do NOT move the cursor
clear_screen() { e "J"; }
# proto: cls()
# Clear screen and move the cursor
cls() { topleft; clear_screen; }
# proto: cl_XoY()
# Clear XoY, where X = E(nd) / B(egining)
# and Y = L(ine) / S(creen).
cl_eol() { e "K"; }
cl_bol() { e "1K"; }
cl_eos() { e "0J"; }
cl_bos() { e "1J"; }
# proto: cu_hide(), cu_show()
# Hide/show cursor
cu_hide() { e "?25l"; }
cu_show() { e "?12l"; e "?25h"; }
# proto: cu_save(), cu_restore()
# Save/restore cursor position
cu_save() { e "s"; }
cu_restore() { e "u"; }
# FIXME: This don't work.
## proto: cu_D()
## Move cursor D, where D = up / down / left / right
#cu_up() { e "1A"; }
#cu_down() { e "1B"; }
#cu_left() { e "1C"; }
#cu_right() { e "1D"; }
# proto: attr(attr), sfg(color), sbg(color), rac()
# Set attribute / screen fg color
# / screen bg color, and restore attrs & colors.
# attr:
BOLD=1
UL=4 # UnderLine
REV=7 # REVerse
# color:
BLACK=0
RED=1
GREEN=2
YELLOW=3
BLUE=4
MAGENTA=5
CYAN=6
WHITE=7
# function:
attr() { e "$1m"; }
sfg() { e "3$1m"; }
sbg() { e "4$1m"; }
rac() { e "m"; }
# Test
rac
cls
cu_save
cu_hide
sleep 1
pos 5 3
attr $BOLD
echo 囗囗囗囗
cu_show
sleep 1
cu_restore
sleep 1
cu_hide
attr $UL
sfg $BLUE
for i in `seq 0 70`; do
echo -n "F"
sleep 0.01
done
sbg $YELLOW
for i in `seq 0 70`; do
echo -ne "\bF\b"
sleep 0.01
done
attr $REV
for i in `seq 0 70`; do
echo -n "F"
sleep 0.01
done
rac
attr "$UL"
sfg $GREEN
for i in `seq 0 70`; do
echo -ne "\bF\b"
sleep 0.01
done
rac
attr "$BOLD"
sfg $RED
for i in `seq 0 70`; do
echo -n "F"
sleep 0.01
done
echo
sfg $YELLOW
for i in `seq 0 70`; do
echo -n "u"
sleep 0.01
done
echo
sfg $CYAN
for i in `seq 0 70`; do
echo -n "c"
sleep 0.01
done
echo
sfg $MAGENTA
for i in `seq 0 70`; do
echo -n "k"
sleep 0.01
done
echo
rac
cu_save
attr $BOLD
pos 5 10
echo -n "^_^"
cu_restore
cu_show
sleep 2
[/bash]
很口口的一个动画
# Screen-funcs
# proto: e(...)
# Print a Command Sequence Introducer
e() { echo -ne "\e[$*"; }
# proto: pos(y, x)
# Position the cursor
pos() { e "$1;$2H"; }
# proto: topleft()
# Move cursor to the top-left corner
topleft() { e "H"; }
# proto: clear_screen()
# Clear screen but do NOT move the cursor
clear_screen() { e "J"; }
# proto: cls()
# Clear screen and move the cursor
cls() { topleft; clear_screen; }
# proto: cl_XoY()
# Clear XoY, where X = E(nd) / B(egining)
# and Y = L(ine) / S(creen).
cl_eol() { e "K"; }
cl_bol() { e "1K"; }
cl_eos() { e "0J"; }
cl_bos() { e "1J"; }
# proto: cu_hide(), cu_show()
# Hide/show cursor
cu_hide() { e "?25l"; }
cu_show() { e "?12l"; e "?25h"; }
# proto: cu_save(), cu_restore()
# Save/restore cursor position
cu_save() { e "s"; }
cu_restore() { e "u"; }
# FIXME: This don't work.
## proto: cu_D()
## Move cursor D, where D = up / down / left / right
#cu_up() { e "1A"; }
#cu_down() { e "1B"; }
#cu_left() { e "1C"; }
#cu_right() { e "1D"; }
# proto: attr(attr), sfg(color), sbg(color), rac()
# Set attribute / screen fg color
# / screen bg color, and restore attrs & colors.
# attr:
BOLD=1
UL=4 # UnderLine
REV=7 # REVerse
# color:
BLACK=0
RED=1
GREEN=2
YELLOW=3
BLUE=4
MAGENTA=5
CYAN=6
WHITE=7
# function:
attr() { e "$1m"; }
sfg() { e "3$1m"; }
sbg() { e "4$1m"; }
rac() { e "m"; }
# Test
rac
cls
cu_save
cu_hide
sleep 1
pos 5 3
attr $BOLD
echo 囗囗囗囗
cu_show
sleep 1
cu_restore
sleep 1
cu_hide
attr $UL
sfg $BLUE
for i in `seq 0 70`; do
echo -n "F"
sleep 0.01
done
sbg $YELLOW
for i in `seq 0 70`; do
echo -ne "\bF\b"
sleep 0.01
done
attr $REV
for i in `seq 0 70`; do
echo -n "F"
sleep 0.01
done
rac
attr "$UL"
sfg $GREEN
for i in `seq 0 70`; do
echo -ne "\bF\b"
sleep 0.01
done
rac
attr "$BOLD"
sfg $RED
for i in `seq 0 70`; do
echo -n "F"
sleep 0.01
done
echo
sfg $YELLOW
for i in `seq 0 70`; do
echo -n "u"
sleep 0.01
done
echo
sfg $CYAN
for i in `seq 0 70`; do
echo -n "c"
sleep 0.01
done
echo
sfg $MAGENTA
for i in `seq 0 70`; do
echo -n "k"
sleep 0.01
done
echo
rac
cu_save
attr $BOLD
pos 5 10
echo -n "^_^"
cu_restore
cu_show
sleep 2
[/bash]
很口口的一个动画
