-
Notifications
You must be signed in to change notification settings - Fork 0
/
shalkboard.sh
executable file
·292 lines (248 loc) · 8.09 KB
/
shalkboard.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
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env bash
pushd "$(dirname -- "$0")" >/dev/null || exit 1
source lib/terminal.sh
source lib/toolbar.sh
source lib/stack.sh
source lib/buffer.sh
source shalkboard_text.sh
source shalkboard_draw.sh
source shalkboard_clipboard.sh
popd >/dev/null || true
on_mouse_press() {
if ! toolbar_click $1 $2; then
shalkboard_text_confirm
case "$tool" in
draw) shalkboard_draw_start "$1" "$2" "$brush" "$color" ;;
text) shalkboard_text_start "$1" "$2" "$text_font" "$color" ;;
copy) shalkboard_clipboard_copy_start "$@" ;;
paste) shalkboard_clipboard_paste "$1" "$2" "$color" ;;
esac
fi
}
on_mouse_release() {
case "$tool" in
draw) shalkboard_draw_end "$@" ;;
copy) shalkboard_clipboard_copy_end "$@" ;;
esac
}
on_mouse_drag() {
case "$tool" in
draw) shalkboard_draw_mouse_drag $1 $2 ;;
esac
}
on_key_arrow_left() {
case "$tool" in
draw) shalkboard_draw_left ;;
esac
}
on_key_arrow_right() {
case "$tool" in
draw) shalkboard_draw_right ;;
esac
}
on_key_arrow_up() {
case "$tool" in
draw) shalkboard_draw_up ;;
esac
}
on_key_arrow_down() {
case "$tool" in
draw) shalkboard_draw_down ;;
esac
}
on_key_type() {
case "$tool" in
text) shalkboard_text_type "$1" ;;
esac
}
on_key_enter() {
shalkboard_text_enter
}
on_key_backspace() { stack undo; stack commit; }
on_key_page_up() {
shalkboard_change_page $((shalkboard_page_number - 1));
toolbar_draw
}
on_key_page_down() {
shalkboard_change_page $((shalkboard_page_number + 1));
toolbar_draw
}
on_before_stack_undo() {
shalkboard_draw_undo "$@"
shalkboard_text_undo "$@"
}
on_after_stack_undo() { stack replay; toolbar_draw; shalkboard_text_refresh; }
on_after_stack_rollback() { shalkboard_clear_screen; stack replay; toolbar_draw ; shalkboard_text_refresh; }
shalkboard_clear_screen() {
toolbar_clear_screen
on_terminal_clear
}
shalkboard_clear_menu() {
terminal clear-up "$toolbar_bottom_right_y"
on_terminal_clear
}
on_key_delete() {
stack rollback
}
on_toolbar_event() {
tool_is() { [ "$tool" == "$1" ] && echo true || echo false; }
brush_is() { [ "$brush" == "$1" ] && [ "$tool" == "draw" ] && echo true || echo false; }
color_is() { [ "$color" == "$1" ] && echo true || echo false; }
font_is() { [ "$text_font" == "$1" ] && [ "$tool" == "text" ] && echo true || echo false; }
page_is() { [ "$shalkboard_page_number" == "$1" ] && echo true || echo false; }
lite_menu() {
toolbar_start ||
toolbar_line_start ||
toolbar_button "redraw" shalkboard_redraw ||
toolbar_button "exit" "tool=exit; terminal mouse_off" "$(tool_is exit)" ||
toolbar_button ">>>" "menu=extended" ||
toolbar_spacer ||
toolbar_label "page:" ||
toolbar_button "1" "shalkboard_change_page 1" "$(page_is 1)" ||
toolbar_button "2" "shalkboard_change_page 2" "$(page_is 2)" ||
toolbar_button "3" "shalkboard_change_page 3" "$(page_is 3)" ||
toolbar_button "4" "shalkboard_change_page 4" "$(page_is 4)" ||
toolbar_button "5" "shalkboard_change_page 5" "$(page_is 5)" ||
toolbar_button "6" "shalkboard_change_page 6" "$(page_is 6)" ||
toolbar_button "7" "shalkboard_change_page 7" "$(page_is 7)" ||
toolbar_button "8" "shalkboard_change_page 8" "$(page_is 8)" ||
toolbar_button "9" "shalkboard_change_page 9" "$(page_is 9)" ||
toolbar_line_end ||
toolbar_end
}
extended_menu() {
toolbar_start ||
toolbar_line_start ||
toolbar_button "redraw" shalkboard_redraw ||
toolbar_button "exit" "tool=exit; terminal mouse_off" "$(tool_is exit)" ||
toolbar_button "<<<" "menu=lite; shalkboard_clear_menu; stack replay" ||
toolbar_spacer ||
toolbar_label "page:" ||
toolbar_button "1" "shalkboard_change_page 1" "$(page_is 1)" ||
toolbar_button "2" "shalkboard_change_page 2" "$(page_is 2)" ||
toolbar_button "3" "shalkboard_change_page 3" "$(page_is 3)" ||
toolbar_button "4" "shalkboard_change_page 4" "$(page_is 4)" ||
toolbar_button "5" "shalkboard_change_page 5" "$(page_is 5)" ||
toolbar_button "6" "shalkboard_change_page 6" "$(page_is 6)" ||
toolbar_button "7" "shalkboard_change_page 7" "$(page_is 7)" ||
toolbar_button "8" "shalkboard_change_page 8" "$(page_is 8)" ||
toolbar_button "9" "shalkboard_change_page 9" "$(page_is 9)" ||
toolbar_line_end ||
toolbar_line_start ||
toolbar_button "undo" "stack rollback" ||
toolbar_button "reset" "shalkboard_reset" ||
toolbar_button "reload" shalkboard_load ||
toolbar_button "save" shalkboard_save ||
toolbar_spacer ||
toolbar_button "copy" tool=copy "$(tool_is copy)" ||
toolbar_button "paste" tool=paste "$(tool_is paste)" ||
toolbar_line_end ||
toolbar_line_start ||
toolbar_label "color:" ||
toolbar_button "red" color=red "$(color_is red)" ||
toolbar_button "green" color=green "$(color_is green)" ||
toolbar_button "yellow" color=yellow "$(color_is yellow)" ||
toolbar_button "blue" color=blue "$(color_is blue)" ||
toolbar_button "white" color=white "$(color_is white)" ||
toolbar_line_end ||
toolbar_line_start ||
toolbar_label "draw:" ||
toolbar_button "██" "brush=block; tool=draw" "$(brush_is block)" ||
toolbar_button "▄█▀" "brush=brush; tool=draw" "$(brush_is brush)" ||
toolbar_button "┏━┛" "brush=bold; tool=draw" "$(brush_is bold)" ||
toolbar_button "┌─┘" "brush=thin; tool=draw" "$(brush_is thin)" ||
toolbar_button "+-+" "brush=ascii; tool=draw" "$(brush_is ascii)" ||
toolbar_spacer ||
toolbar_button " " "brush=eraser; tool=draw" "$(brush_is eraser)" ||
toolbar_line_end ||
toolbar_line_start ||
toolbar_label "text:" ||
toolbar_button "small" "text_font=small; tool=text" "$(font_is small)" ||
toolbar_button "6px" "text_font=6px; tool=text" "$(font_is 6px)" ||
toolbar_button "10px" "text_font=10px; tool=text" "$(font_is 10px)" ||
toolbar_button "line" "text_font=line; tool=text" "$(font_is line)" ||
toolbar_line_end ||
toolbar_end
}
if [[ "$menu" == "extended" ]]; then
extended_menu || return 1
else
lite_menu || return 1
fi
}
on_mouse_move() { terminal cursor-move 1024 1024; terminal cursor-request-position; }
on_cursor_position() {
if [[ "$shalkboard_width" != "$1" || "$shalkboard_height" != "$2" ]]; then
shalkboard_redraw
toolbar_draw
fi
shalkboard_width="$1"
shalkboard_height="$2"
}
shalkboard_redraw() {
shalkboard_clear_screen
stack replay
}
shalkboard_save() {
on_terminal_write() { buffer write "$@"; }
on_terminal_clear() { buffer clear; }
stack save >"$shalkboard_page_file"
terminal clear
stack replay
buffer save >"${shalkboard_page_file}.txt"
on_terminal_write() { :; }
on_terminal_clear() { :; }
}
shalkboard_load() {
if [ -f "${shalkboard_page_file}" ]; then
stack load <"${shalkboard_page_file}"
shalkboard_clear_screen
stack replay
fi
}
shalkboard_change_page() {
#tool=""
stack save >"$shalkboard_page_file"
shalkboard_page_number="$1"
if [[ "$shalkboard_page_number" == "1" ]]; then
shalkboard_page_file="${shalkboard_file}"
else
shalkboard_page_file="${shalkboard_file%.*}.${shalkboard_page_number}.board"
fi
shalkboard_load
}
shalkboard_reset() {
stack clear
shalkboard_clear_screen
brush=brush
color=green
text_font=line
tool=draw
}
shalkboard() {
brush=brush
color=green
text_font=line
tool=
shalkboard_page_number=1
shalkboard_file="${1-default.board}"
shalkboard_page_file="$shalkboard_file"
terminal clear
shalkboard_load
toolbar_draw
trap 'terminal cursor-move 1 1024; terminal mouse-off; terminal cursor-on' EXIT
terminal cursor-off
while true; do
terminal mouse-on
terminal cursor-move 1024 1024;
terminal cursor-request-position
terminal read;
terminal mouse-off
case "$tool" in
exit) exit ;;
esac;
done
}
if [ "$(basename -s .sh "$0")" == "shalkboard" ]; then
shalkboard "$@"
fi