forked from liungkejin/Bash-Games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aclock.sh
executable file
·154 lines (133 loc) · 5.6 KB
/
aclock.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env bash
#
# Author: LKJ
# Date: 2013/5/14
# Email: [email protected]
#
asciinumber=(
' .XEEEEb ,:LHL :LEEEEEG .CNEEEEE8 bMNj NHKKEEEEEX 1LEEE1 KEEEEEEEKNMHH 8EEEEEL. cEEEEEO '
' MEEEUXEEE8 jNEEEEE EEEEHMEEEEU EEEELLEEEEc NEEEU 7EEEEEEEEEK :EEEEEEN, EEEEEEEEEEEEE OEEEGC8EEEM 1EEELOLEEE3 '
' NEE. OEEC EY" MEE OC LEEc :" EEE EEGEE3 8EN MEEM. :EE. 1EEj :EEO 1EE3 DEEc '
' ,EEj EEE HEE EEE cEE: EEU EEJ NEC EEE EEJ EEE EEE EEN KEE '
' HEE jEE1 NEE EEE EEE EEM EEJ EE LEE .. EEK DEEj :EE7 ,EE1 jEE '
' EEH EEZ KEE :EE1 .::jZEEG EEU EEJ .EEEEEENC EE77EEEEEEL NEE UEENj bEE7 .EEX :EE.'
'.EEZ EEM KEE EEK EEEEEEC .EEc EEC :X3DGMEEEEU 3EEEED.".GEEE. CEE. EEEEEEE EEEj :EEE '
' EEZ EEM KEE :EEK "jNEEZ :EE EE7 MEEU LEEb EEE .EE8 DEEL:.8EEEM NEEENMEEEHEE '
' EEN .EEG KEE bEEG 7EEM jEEN738ODDEEM3b EEE MEE 8EE, EEE EEE ,EEE .bEEEEC XEE '
' LEE 3EE: KEE .EEE, EEE LEEEEEEEEEEEEEE XEE 8EE cEE: NEE 7EE1 jEE1 :EE: '
' .EEc EEE KEE bEED EEE EE1 EEE EEX EEE 3EE: cEEc 7EEj CEEG '
' MEE7 NEE. EEE jEEK C EEE1 EEC j :EEE CEEG LEEj .EEU EEE: .EEE 1EEEJ '
' bEEEEEEEE. EEE NEEEEEEEEEEEE bEEEEEEEEEE7 EEd JEEEEEEEEEN jEEEEEEEEE7 .EEE KEEEEHEEEEL 8EEEEEEX '
' DEEEL7 CGD 3GD3DOGGGGGUX :DHEEEN8. bUd 7GNEEEMc 7LEEEX: 1XG JHEEEM1 COLIN" '
);
asciidot=(
' @@ '
' @@ '
);
len=${#asciinumber[@]};
#共有三个参数,
#第一个是所要打印的数字,
#第二个是之前打印的数字个数,
#第三个是之前打印的点的个数
function print_number {
start=$(($1*17));
start_y=$(($2*17+$3*4+$beg_y));
for (( i = 0; i < len; i++ )); do
echo -ne "\033[$((beg_x+i));${start_y}H\033[1;32m${asciinumber[$i]:$start:17}\033[0m";
done
}
#print_dot有两个参数
#第一个参数是之前打印的数字个数
#第二个参数是之前打印的点的个数
function print_dot {
local pt=$(($1*17+$2*4+beg_y));
for (( j = 0; j < 2; j++ )); do
echo -ne "\033[$((beg_x+j+3));${pt}H\033[1;32m${asciidot[$j]}\033[0m";
echo -ne "\033[$((beg_x+j+10));${pt}H\033[1;32m${asciidot[$j]}\033[0m";
done
}
function old_value {
orows=`tput lines`; beg_x=$((orows/2-6));
ocols=`tput cols`; beg_y=$((ocols/2-54));
ohur=$((10#`date +%H`));
omin=$((10#`date +%M`));
osec=$((10#`date +%S`));
print_number $((ohur/10)) 0 0; print_number $((ohur%10)) 1 0;
print_dot 2 0;
print_number $((omin/10)) 2 1; print_number $((omin%10)) 3 1;
print_dot 4 1;
print_number $((osec/10)) 4 2; print_number $((osec%10)) 5 2;
}
function print_all {
t_rows=`tput lines`; beg_x=$((t_rows/2-6));
t_cols=`tput cols`; beg_y=$((t_cols/2-54));
if [[ $t_rows -ne $orows || $t_cols -ne $ocols ]]; then
orows=$t_rows;
ocols=$t_cols;
check_win $orows $ocols;
old_value;
fi
hur=$((10#`date +%H`));
hft=$((hur/10)); hsd=$((hur%10));
if [[ $ohft -ne $hft ]]; then
print_number $hft 0 0;
ohft=$hft;
fi
if [[ $ohsd -ne $hsd ]]; then
print_number $hsd 1 0;
ohsd=$hsd;
fi
min=$((10#`date +%M`));
mft=$((min/10)); msd=$((min%10));
if [[ $omft -ne $mft ]]; then
print_number $mft 2 1;
omft=$mft;
fi
if [[ $omsd -ne $msd ]]; then
print_number $msd 3 1;
omsd=$msd;
fi
sec=$((10#`date +%S`)); #出现(())bug的原因:date +%S < 10 的时候会有前置0
#所以((08/10))会出错,但是使用expr不会出现错误,let也会有此错误
#解决方法是$((10#08/10));
sft=$((sec/10)); ssd=$((sec%10));
if [[ $osft -ne $sft ]]; then
print_number $sft 4 2;
osft=$sft;
fi
if [[ $ossd -ne $ssd ]]; then
print_number $ssd 5 2;
ossd=$ssd;
fi
}
function check_win {
if [[ $1 -lt 14 || $2 -lt 110 ]]; then
clear;
echo -ne "\033[8;15;120t"; #change the window size
fi
clear; #若窗口改变则重新刷新
}
function INIT {
tput smcup; #保存屏幕
check_win `tput lines` `tput cols`;
trap 'EXIT;' SIGINT; #将光标重新设置为白色
tput civis; #设置光标不可见
old_value;
}
function EXIT {
tput cvvis; #使光标可见
tput rmcup; #恢复屏幕
exit 0;
}
INIT;
while true; do
read -t 1 -n 1 anykey;
if [[ $? -eq 0 ]]; then
EXIT;
fi
print_all;
# if sleep 0.3 &> /dev/null; then
# sleep 1;
# fi;
done
exit 0;