-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.sh
More file actions
203 lines (186 loc) · 7.02 KB
/
buffer.sh
File metadata and controls
203 lines (186 loc) · 7.02 KB
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
declare -a _buffer_modifiers_ev
declare -a _buffer_draw_ev
declare _buffer_height
declare _buffer_width
_buffer_ev_separator='\x1F'
_buffer_ev_end_delim='\x1E'
#Setup up w and h properties for buffer
# $1 - x size
# $2 - y size
function buffer.init() {
unset _buffer_modifiers_ev
unset _buffer_draw_ev
declare -a _buffer_modifiers_ev
declare -a _buffer_draw_ev
_buffer_height=$2
_buffer_width=$1
}
#Prints all content of buffer and wipes it out
#(Supposing that buffer will take all space of creen)
# optional $1 - Clear screen before printing (that will make a "blink" effect)
# 1-yes any other value is no
function buffer.flush() {
if [ -n "$1" ] && [ $1 -ge 1 ]; then
clear
fi
# Use awk instead of bash loop, because awk are much more faster
buffer.move_cursor 0 0
awk \
-v height="${_buffer_height}" \
-v width="${_buffer_width}" \
-v clear_format="$CLEAR_FORMAT" \
-v _buffer_modifiers_ev="${_buffer_modifiers_ev[*]}" \
'
{
modifiers_size=split(_buffer_modifiers_ev, buffer_modifiers_ev, /\|/)
draw_ev_size=split($0, buffer_draw_ev, /\\x1E /)
# Process draw events
split("", chars_key)
for (i = 1; i <= draw_ev_size; i++) {
split(buffer_draw_ev[i], draw_event, /\\x1F/ )
if (draw_event[1] == "rect" ) {
for (x = draw_event[3]; x <= draw_event[5]; x++) {
for (y = draw_event[4]; y <= draw_event[6]; y++) {
pos=(y * (width + 1)) + x
chars_key[pos+1]=draw_event[2]
}
}
} else if (draw_event[1] == "rect_border" ) {
horizontal_char=draw_event[2]
vertical_char=draw_event[3]
start_x=draw_event[4]
start_y=draw_event[5]
end_x=draw_event[6]
end_y=draw_event[7]
for (x = start_x; x<= end_x; x++) {
pos=(start_y * (width + 1)) + x
chars_key[pos+1]=horizontal_char
pos=(end_y * (width + 1)) + x
chars_key[pos+1]=horizontal_char
}
for (y = start_y; y<= end_y; y++) {
pos=(y * (width + 1)) + start_x
chars_key[pos+1]=vertical_char
pos=(y * (width + 1)) + end_x
chars_key[pos+1]=vertical_char
}
} else if (draw_event[1] == "px" ) {
pos=(draw_event[4] * (width + 1)) + draw_event[3]
if (draw_event[2]=="") {
chars_key[pos+1]=" "
} else {
chars_key[pos+1]=draw_event[2]
}
} else if (draw_event[1] == "text" ) {
pos=(draw_event[4] * (width + 1)) + draw_event[3]
for (x=1; x<=length(draw_event[2]); x++) {
char=substr(draw_event[2], x, 1)
if (char=="" || char==" ") {
chars_key[pos+1]=" "
} else {
chars_key[pos+1]=char
}
pos+=1
}
} else if (draw_event[1] != "" ) {
print "Draw event " draw_event[1] " is unknown"
exit
}
}
# Style processing
split("", char_style)
for (i = 1; i <= modifiers_size; i++) {
split(buffer_modifiers_ev[i], current_style, " ")
if (current_style[2] == "UNSET" ) {
continue
}
if (current_style[1] == "rect" ) {
for (x = current_style[3]; x <= current_style[5]; x++) {
for (y = current_style[4]; y <= current_style[6]; y++) {
pos=(y * (width + 1)) + x
char_style[pos+1]=char_style[pos+1] current_style[2]
}
}
} else if (current_style[1] == "pixel" ) {
pos=(current_style[4] * (width + 1)) + current_style[3]
char_style[pos+1]=char_style[pos+1] current_style[2]
} else if (current_style[1] != "" ) {
print "Style " current_style[1] " is unknown"
exit
}
}
# Every char in screen processing
for (i = 0; i < (width + 1)*(height + 1); i++ ) {
char=""
if ( (i+1) in chars_key ) {
char=chars_key[i+1]
} else {
char=" "
}
out=out char_style[i+1] char clear_format
}
printf out
}
' <<< "${_buffer_draw_ev[*]}"
buffer.clear
}
#Empty all buffer
function buffer.clear() {
buffer.init $_buffer_width $_buffer_height
}
# $1 - symbol
# $2 - x pos
# $3 - y pos
# Inserts into buffer a single symbol.
# This function is fine for small amount of symbols.
# But if you know that you will insert multiple symbols,
# then use theese functions, they are more perfomant:
# buffer.insert_rect_border - draw symbol in border of rect
# buffer.insert_rect - fill full rect (or line) with given symbol
# buffer.insert_text - prints text in single line
function buffer.insert() {
_buffer_draw_ev+=("px${_buffer_ev_separator}$1${_buffer_ev_separator}$2${_buffer_ev_separator}$3${_buffer_ev_end_delim}")
}
# $1 - symbol for horizontal lines
# $2 - symbol for vertical lines
# $3 - start x pos
# $4 - start y pos
# $5 - end x pos
# $6 - end y pos
function buffer.insert_rect_border() {
_buffer_draw_ev+=("rect_border${_buffer_ev_separator}$1${_buffer_ev_separator}$2${_buffer_ev_separator}$3${_buffer_ev_separator}$4${_buffer_ev_separator}$5${_buffer_ev_separator}$6${_buffer_ev_end_delim}")
}
# $1 - symbol
# $2 - start x pos
# $3 - start y pos
# $4 - end x pos
# $5 - end y pos
function buffer.insert_rect() {
_buffer_draw_ev+=("rect${_buffer_ev_separator}$1${_buffer_ev_separator}$2${_buffer_ev_separator}$3${_buffer_ev_separator}$4${_buffer_ev_separator}$5${_buffer_ev_end_delim}")
}
# $1 - text string
# $2 - start x pos
# $3 - start y pos
#
function buffer.insert_text() {
_buffer_draw_ev+=("text${_buffer_ev_separator}$1${_buffer_ev_separator}$2${_buffer_ev_separator}$3${_buffer_ev_end_delim}")
}
# $1 - x pos
# $2 - y pos
function buffer.move_cursor() {
echo -en "\E[$1;${1}H"
}
# $1 - modifier
# $2 - x pos
# $3 - y pos
function buffer.insert_modifier() {
_buffer_modifiers_ev+=("pixel ${1:-"UNSET"} $2 $3|")
}
# $1 - modifier
# $2 - start x pos
# $3 - start y pos
# $4 - end x pos
# $5 - end y pos
function buffer.insert_modifier_rect {
_buffer_modifiers_ev+=("rect ${1:-"UNSET"} $2 $3 $4 $5|")
}