-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_curses.sh
279 lines (243 loc) · 5.14 KB
/
simple_curses.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/bin/bash
#simple curses library to create windows on terminal
#
#author: Patrice Ferlet [email protected]
#license: new BSD
#
#create_buffer patch by Laurent Bachelier
create_buffer(){
# Try to use SHM, then $TMPDIR, then /tmp
if [ -d "/dev/shm" ]; then
BUFFER_DIR="/dev/shm"
elif [ -z $TMPDIR ]; then
BUFFER_DIR=$TMPDIR
else
BUFFER_DIR="/tmp"
fi
[[ "$1" != "" ]] && buffername=$1 || buffername="bashsimplecurses"
# Try to use mktemp before using the unsafe method
if [ -x `which mktemp` ]; then
#mktemp --tmpdir=${BUFFER_DIR} ${buffername}.XXXXXXXXXX
mktemp ${BUFFER_DIR}/${buffername}.XXXXXXXXXX
else
echo "${BUFFER_DIR}/bashsimplecurses."$RANDOM
fi
}
#Usefull variables
LASTCOLS=0
BUFFER=`create_buffer`
POSX=0
POSY=0
LASTWINPOS=0
#call on SIGINT and SIGKILL
#it removes buffer before to stop
on_kill(){
echo "Exiting"
rm -rf $BUFFER
exit 0
}
trap on_kill SIGINT SIGTERM
#initialize terminal
term_init(){
POSX=0
POSY=0
tput clear >> $BUFFER
}
#change line
_nl(){
POSY=$((POSY+1))
tput cup $POSY $POSX >> $BUFFER
#echo
}
move_up(){
set_position $POSX 0
}
col_right(){
left=$((LASTCOLS+POSX))
set_position $left $LASTWINPOS
}
#put display coordinates
set_position(){
POSX=$1
POSY=$2
}
#initialize chars to use
_TL="\033(0l\033(B"
_TR="\033(0k\033(B"
_BL="\033(0m\033(B"
_BR="\033(0j\033(B"
_SEPL="\033(0t\033(B"
_SEPR="\033(0u\033(B"
_VLINE="\033(0x\033(B"
_HLINE="\033(0q\033(B"
init_chars(){
if [[ "$ASCIIMODE" != "" ]]; then
if [[ "$ASCIIMODE" == "ascii" ]]; then
_TL="+"
_TR="+"
_BL="+"
_BR="+"
_SEPL="+"
_SEPR="+"
_VLINE="|"
_HLINE="-"
fi
fi
}
#Append a windo on POSX,POSY
window(){
LASTWINPOS=$POSY
title=$1
color=$2
tput cup $POSY $POSX
cols=$(tput cols)
cols=$((cols))
if [[ "$3" != "" ]]; then
cols=$3
if [ $(echo $3 | grep "%") ];then
cols=$(tput cols)
cols=$((cols))
w=$(echo $3 | sed 's/%//')
cols=$((w*cols/100))
fi
fi
len=$(echo "$1" | echo $(($(wc -c)-1)))
left=$(((cols/2) - (len/2) -1))
#draw up line
clean_line
echo -ne $_TL
for i in `seq 3 $cols`; do echo -ne $_HLINE; done
echo -ne $_TR
#next line, draw title
_nl
tput sc
clean_line
echo -ne $_VLINE
tput cuf $left
#set title color
case $color in
green)
echo -n -e "\E[01;32m"
;;
red)
echo -n -e "\E[01;31m"
;;
blue)
echo -n -e "\E[01;34m"
;;
grey|*)
echo -n -e "\E[01;37m"
;;
esac
echo $title
tput rc
tput cuf $((cols-1))
echo -ne $_VLINE
echo -n -e "\e[00m"
_nl
#then draw bottom line for title
addsep
LASTCOLS=$cols
}
#append a separator, new line
addsep (){
clean_line
echo -ne $_SEPL
for i in `seq 3 $cols`; do echo -ne $_HLINE; done
echo -ne $_SEPR
_nl
}
#clean the current line
clean_line(){
tput sc
#tput el
tput rc
}
#add text on current window
append_file(){
[[ "$1" != "" ]] && align="left" || align=$1
while read l;do
l=`echo $l | sed 's/____SPACES____/ /g'`
_append "$l" $align
done < "$1"
}
append(){
text=$(echo -e $1 | fold -w $((LASTCOLS-2)) -s)
rbuffer=`create_buffer bashsimplecursesfilebuffer`
echo -e "$text" > $rbuffer
while read a; do
_append "$a" $2
done < $rbuffer
rm -f $rbuffer
}
_append(){
clean_line
tput sc
echo -ne $_VLINE
len=$(echo "$1" | wc -c )
len=$((len-1))
left=$((LASTCOLS/2 - len/2 -1))
[[ "$2" == "left" ]] && left=0
tput cuf $left
echo -e "$1"
tput rc
tput cuf $((LASTCOLS-1))
echo -ne $_VLINE
_nl
}
#add separated values on current window
append_tabbed(){
[[ $2 == "" ]] && echo "append_tabbed: Second argument needed" >&2 && exit 1
[[ "$3" != "" ]] && delim=$3 || delim=":"
clean_line
tput sc
echo -ne $_VLINE
len=$(echo "$1" | wc -c )
len=$((len-1))
left=$((LASTCOLS/$2))
for i in `seq 0 $(($2))`; do
tput rc
tput cuf $((left*i+1))
echo "`echo $1 | cut -f$((i+1)) -d"$delim"`"
done
tput rc
tput cuf $((LASTCOLS-1))
echo -ne $_VLINE
_nl
}
#append a command output
append_command(){
buff=`create_buffer command`
echo -e "`$1`" | sed 's/ /____SPACES____/g' > $buff 2>&1
append_file $buff "left"
rm -f $buff
}
#close the window display
endwin(){
clean_line
echo -ne $_BL
for i in `seq 3 $LASTCOLS`; do echo -ne $_HLINE; done
echo -ne $_BR
_nl
}
#refresh display
refresh (){
cat $BUFFER
echo "" > $BUFFER
}
#main loop called
main_loop (){
term_init
init_chars
[[ "$1" == "" ]] && time=1 || time=$1
while [[ 1 ]];do
tput cup 0 0 >> $BUFFER
tput il $(tput lines) >>$BUFFER
main >> $BUFFER
tput cup $(tput lines) $(tput cols) >> $BUFFER
refresh
sleep $time
POSX=0
POSY=0
done
}