forked from dylanaraps/bareutils
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbano
executable file
·435 lines (412 loc) · 14.1 KB
/
bano
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env bash
#stolen from https://github.com/comfies/bed and edited to be like nano and added few stuff like syntax highlighting
#i guess i will turn this into ebashs later :)
printf '\e[?7l'
declare -A syntax
declare -a buffer
declare -a buffersyntax
declare -i line base rl
declare file message modified filetype enable_mouse comment stringone stringtwo reservedlines
printf "\e[?1049h" #switch to alternative buffer
buffer=() # File contents
buffersyntax=() # File contents
line=0 # Currently selected line (0 means the buffer is empty)
base=1 # Top-most line shown
rl=0 # how left or right the cursor is
file= # Currently addressed file
message="Welcome to bano (press 'x' to quit)." # Feedback text in the status bar
modified=false # Tracking whether a file was modified
enable_mouse=true # Xterm mouse support
reservedlines=1 # Number of lines not displaying file
shopt -s lastpipe
shopt -s extglob # Ensure advanced pattern matching is available
shopt -s checkwinsize; (:) # Enable and then trigger a terminal size refresh
trap redraw WINCH ALRM # Attach WINCH and ALRM to redraw the screen
trap die EXIT HUP USR1 # Attach most exit codes to cleanup and exit
trap quit INT
trap quit SIGTERM
syntax[bash]=builtin_syntax_bash
syntax[sh]=builtin_syntax_bash #there isnt anything really bash specific currently
syntax[ksh]=builtin_syntax_bash
syntax[mksh]=builtin_syntax_bash
syntax[dash]=builtin_syntax_bash
syntax[zsh]=builtin_syntax_bash
syntax[conf]=builtin_syntax_conf
syntax[unknown]=builtin_syntax_unknown
builtin_syntax_bash() {
case "${word:0:2}" in
'"$') colour 0 36 48 "${word}"; return ;;
esac
case "${word:0:1}" in
'#') colour 0 37 48 "${word}" && comment=true; return;;
'$') colour 0 96 108 "${word}"; return ;;
'-') colour 0 93 108 "${word}"; return ;;
esac
"${comment}" && colour 0 37 48 "${word}" && return
case "${word}" in
'||') colour 0 95 108 "${word}"; return ;;
'&&') colour 0 95 108 "${word}"; return ;;
']') colour 1 95 108 "${word}"; return ;;
'[') colour 1 95 108 "${word}"; return ;;
'[[') colour 1 95 108 "${word}"; return ;;
']]') colour 1 95 108 "${word}"; return ;;
')') colour 1 95 108 "${word}"; return ;;
'(') colour 1 95 108 "${word}"; return ;;
'{') colour 1 95 108 "${word}"; return ;;
'}') colour 1 95 108 "${word}"; return ;;
';') colour 0 95 108 "${word}"; return ;;
*"'"*) colour 0 92 108 "${word}" && return ;;
*'"'*) colour 0 32 48 "${word}" && return ;;
*'='*) colour 0 94 108 "${word}"; return ;;
esac
[[ "${word: -2}" = '()' ]] && colour 0 30 44 "${word}" && return
case "${word}" in
'echo'|'return'|'case'|'esac'|'for'|'while'|'do'|'done'|'if'|'elif'|'else'|'printf'|'fi'|'continue'|'exit'|'bind'|'then'|'break'|'read'|'declare'|'typeset'|'local'|'let'|'shopt'|'trap'|'set') colour 0 91 108 "${word}" && return
esac
colour 0 0 0 "${word}"
}
builtin_syntax_unknown() {
colour 0 0 0 "${word}"
}
builtin_syntax_conf(){
[[ "${word}" =~ "#" ]] && colour 38 5 4 "${word}" && comment=true && return
"${comment}" && colour 38 5 4 "${word}" && return
colour 0 0 0 "${word}"
}
syntax() {
for s in "${!syntax[@]}"; {
[ "$s" = "$filetype" ] && "${syntax[$s]}" && return
}
"${syntax[unknown]}"
}
basenam() {
for file in "$@"; do
file="${file%/}"
printf '%s\n' "${file##*/}"
done
}
filetype(){
[ $# = 0 ] && return
IFS='.'
filename="${1%/}"
filename="${filename##*/}"
filenamesplit=($filename)
extension="${filenamesplit[-1]}"
[ "${#filenamesplit[@]}" -gt 1 ] && echo "$extension" && return
mapfile -n 1 file_data < "$1"
shebang="$(echo -n "${file_data[@]}")"
[ ! "${shebang:0:1}" = '#' ] && echo unknown && return
shebang="${shebang%/}"
IFS=' '
shebang="${shebang##*/}"
shebangsplit=($shebang)
printf '%s\n' "${shebangsplit[-1]}"
}
set_buffer_file() {
printf '\e[%s;0H' "${LINES}"
printf '\e[D\033[1;93;108m' #make prompt nicer
bind 'set disable-completion off' 2>/dev/null # Enable completion
printf '\e[?25h' # Enable cursor
if read -rei "$1$file" -p "${BED_FILE_PROMPT:=Path: }" file; then
modified=true
fi
bind 'set disable-completion on' 2>/dev/null
}
read_buffer() {
filetype="$(filetype "$1")"
set_buffer_file "$1" # Update target file (pass on default if present)
mapfile -t -O 1 buffer <"$file" # Read file into an array
pipesyntax < "$file" | mapfile -t -O 1 buffersyntax # Read file into an array
if [[ "${buffer[1]}" ]]; then # Ensure that something was actually read into the file
line=1 # Indicate that we have a buffer loaded
modified=false
message="Read ${#buffer[@]} lines from '$file'"
else
message="'$file' is empty"
fi
}
write_buffer() {
true >"$file" # Set the file to an empty text file
for ln in "${buffer[@]}"; do # Write in the buffer to the file
echo "$ln" >>"$file"
done
modified=false
message="Wrote ${#buffer[@]} lines to '$file'"
}
megaredraw() {
printf '%s\n' "${buffer[@]}" | pipesyntax | mapfile -t -O 1 buffersyntax
}
megaredrawline() {
buffersyntax[line]="$(printf '%s' "${buffer[line]}" | pipesyntax)"
}
#megaredrawlinesyntax(){
# buffersyntax[line]="$(printf '%s' "${buffer[line]}" | pipesyntax)"
#}
tabrender() {
IFSbck="$IFS"
IFS=
while read -rsn1 char
do
[ "$char" = " " ] && printf ' ' && continue
[ "$char" = " " ] && printf '\033[0;37;48m%s\033[0;0m' "${BANO_RENDER_TAB:=.}" && continue
printf '%s' "$char"
done
IFS="$IFSbck"
}
tabrenderraw() {
IFSbck="$IFS"
IFS=
while read -rsn1 char
do
[ "$char" = " " ] && printf ' ' && continue
[ "$char" = " " ] && printf '%s' "${BANO_RENDER_TAB_ALT:= }" && continue
printf '%s' "$char"
done
IFS="$IFSbck"
}
new_line() {
buffer=("" "${buffer[@]:1:line}" "" "${buffer[@]:line+1}")
unset 'buffer[0]'
modified=true
megaredraw
down
redraw
}
delete_line() {
buffer=("" "${buffer[@]:1:line-1}" "${buffer[@]:line+1}")
unset 'buffer[0]'
((line > ${#buffer[@]})) && up
modified=true
megaredraw
}
quit() {
printf '\e[%s;0H' "${LINES}"
printf '\e[D\033[1;93;108m' #make prompt nicer
if [[ "$modified" == "true" ]]; then
while :; do
read -rsN1 -p "Buffer modified, save before close? [Y/n/c]" choice
case "$choice" in
Y|y) write_buffer; die;;
N|n) die;;
C|c) message="Quit canceled"; break;;
*) continue;;
esac
done
else
die
fi
}
up() {
for ((i = 0; i < ${1:-1}; i++)); do
((line > 1)) && ((line--)) # As long as we can keep going up, go up
((line < base)) && ((base--)) # Push back the top if we need to
((base <= 0)) && base=1 # Don't push back if our base is at 1
[ -z "$bckrl" ] && bckrl="${rl}"
(( "${#buffer[line]}" < "${rl}" )) && rl="${#buffer[line]}"
(( "${#buffer[line]}" > "${rl}" )) && rl=bckrl
done
}
page_up() {
up $((LINES - 3))
}
down() {
for ((i = 0; i < ${1:-1}; i++)); do
((line < ${#buffer[@]})) && ((line++)) # If we can go down, go down
((line > base + LINES - 3)) && ((base++)) # Move window down if needed
[ -z "$bckrl" ] && bckrl="${rl}"
(( "${#buffer[line]}" < "${rl}" )) && rl="${#buffer[line]}"
(( "${#buffer[line]}" > "${rl}" )) && rl=bckrl
done
}
page_down() {
down $((LINES - 3))
}
execute() {
((line == 0)) && return # If the line is not possible, do nothing
printf '\e[?25h\e[%sH' "$((line + 2 - base))" # Reset cursor position and enable cursor
read -re -p "$(printf '%4s ' "$")" # Present editable line
if [[ "$REPLY" != "${buffer[line]}" ]]; then # If the line is changed, update and inform
buffer[line]="$($REPLY)"
modified=true
fi
megaredraw
redraw
}
right() {
case "${buffer[line]}" in
*)((rl++));;
esac
[ "$rl" -gt "${#buffer[line]}" ] && { rl=0; down; }
bckrl=
}
left() {
((rl--))
[ "$rl" -lt 0 ] && { up; rl="${#buffer[line]}"; }
bckrl=
}
shell() {
printf "\033c"
bash
redraw
}
help() {
printf '\e[%s;0H' "${LINES}"
printf '\033[0;0m'
printf '\n' # Add final newline to seperate commandline
printf '\e[0;107;30m C-x\e[0;m Exit \e[0;107;30m⏎ \e[0;m New Line \e[0;107;30m M-r\e[0;m Set Target \e[0;107;30m C-r\e[0;m Read File \e[0;107;30m C-o\e[0;m Write Out \e[0;107;30m C-d\e[0;m Delete Line \e[0;107;30m C-e\e[0;m Toggle Mouse\n'
printf '\e[0;107;30m C-l\e[0;m New Line \e[0;107;30m C-p\e[0;m Prev Line \e[0;107;30m C-n\e[0;m Next Line \e[0;107;30m C-y\e[0;m Prev Page \e[0;107;30m C-v\e[0;m Next Page \e[0;107;30m C-t\e[0;m Execute \e[0;107;30m C-s\e[0;m Shell \n'
read -rsn1
redraw
}
die() {
bind 'set disable-completion off' 2>/dev/null # Enable completion
printf '\e[?25h\e[?7h\e[?1047l' # Reset terminal to sane mode
printf "\e[?1049l" # switch back to main buffer
echo -e "\e[?1000;1006;1015l" #disable mouse tracking
exit "${errno:-0}" # Assume that we are exiting without an error
}
mtoggle() {
[ "$enable_mouse" = true ] && echo -ne "\e[?1000;1006;1015l" && enable_mouse=false && return
[ "$enable_mouse" = false ] && echo -ne "\e[?1000;1006;1015h" && enable_mouse=true && return
}
redraw() {
(printf '\e[0;30m \e[H\e[?25l\e[107m%*s\r %s \e[46;30m %s \e[0;107;30m L%s W%s\e[m' \
"$COLUMNS" "$message" "bano (press C-g for help)" \
"$(basenam "$file") ($filetype)" "$line" "${rl}") # Status line, among others
for ((i = base; i - base < LINES - reservedlines; i++)); do # Iterate over shown lines
((i != line)) && printf '\e[90m' # Fade line number if not selected
((i > ${#buffer[@]})) && printf '\n\e[K ~\e[m' || \
case "${buffer[i]}" in
*"TODO: "*|*TODO:) printf '\n\e[K%4s\e[m \033[0;45m%s\033[0;0m' "$i" "$(printf '%s' "${buffer[i]}" | tabrenderraw)";;
*"NOTE: "*|*NOTE:) printf '\n\e[K%4s\e[m \033[0;47m%s\033[0;0m' "$i" "$(printf '%s' "${buffer[i]}" | tabrenderraw)";;
*) printf '\n\e[K%4s\e[m %s' "$i" "${buffersyntax[i]}";;
esac
#TODO: make proper coloring based on variable for future config
done
printf '\e[?25h\e[%s;%sH' $((line + 2 - base)) $(( rl + 6 )) # move cursor to the line
}
backspace() {
while [ "${#buffer[line]}" = 0 ]; do
delete_line
done
buffer[line]="${buffer[line]:0:$(( rl - 1 ))}${buffer[line]:${rl}}" #add the key at $rl (cursor position)
left #go left with cursor
megaredrawline #not sure why this is needed but it prevents duplication of the letter
redraw #draw it
modified=true
}
keytype() {
mouse=false
case "$1" in
${BANO_KEY_PGUP:=$'\E[5~'}) page_up; redraw; return;;
${BANO_KEY_PGDN:=$'\E[6~'}) page_down; redraw; return;;
${BANO_KEY_UP:=$'\E[A'}*) up; redraw; return;;
${BANO_KEY_DOWN:=$'\E[B'}*) down; redraw; return;;
${BANO_KEY_RIGHT:=$'\E[C'}*) right; redraw; return;;
${BANO_KEY_LEFT:=$'\E[D'}*) left; redraw; return;;
esac
hexkey=$(printf "%x\n" "'$1")
#clear #stuff for debugging
#echo $hexkey #stuff for debugging
#read #stuff for debugging
#[ "$hexkey" = "$1b" ] && { read -r a; clear; echo $a; read; } && return
#[ "${1:1:2}" = '[' ] && echo E && return
case "$hexkey" in
${BANO_KEY_EXIT:=18}) quit; return;;
${BANO_KEY_PREVIOUS:=10}) up; redraw; return;;
${BANO_KEY_NEXT:=e}) down; redraw; return;;
${BANO_KEY_FORTH:=6}) right; redraw; return;;
${BANO_KEY_BACK:=2}) left; redraw; return;;
${BANO_KEY_PGPREVIOUS:=19}) page_down; redraw; return;;
${BANO_KEY_PGNEXT:=16}) page_up; redraw; return;;
${BANO_KEY_NEWLINE:=0}) new_line; return;;
${BANO_KEY_NEWLINEALT:=c}) new_line; return;;
${BANO_KEY_HELP:=7}) help; return;;
${BANO_KEY_SAVE:=f}) write_buffer; return;;
${BANO_KEY_READ:=12}) read_buffer; return;;
${BANO_KEY_READ:=1b}) set_buffer_file; return;;
${BANO_KEY_EXECUTE:=14}) execute; return;;
${BANO_KEY_DELETE:=4}) delete_line; redraw; return;;
${BANO_KEY_SHELL:=13}) shell; return;;
${BANO_KEY_MOUSE_TOGGLE:=5}) mtoggle; return;;
${BANO_KEY_SCROLL:=1b}) mouse=true;;
${BANO_KEY_BACKSPACE:=7f}) backspace; return;; #delete last char when backspace is pressed
esac
#TODO: make mouse scrolling nice instead of ignoring it
#parse mouse stuff somewhatish weirdly
$mouse && {
a=$(echo "$1")
[ "${a: -1}" = 'm' ] && {
value=0
echo "$a" | while read -rsn1 mchar; do
case "$mchar" in
'0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|9)tmp="${tmp}${mchar}";;
'm'|'M'|';') {
[ "$value" = '0' ] && mode="${tmp}" && tmp=
[ "$value" = '1' ] && y="${tmp}" && tmp=
[ "$value" = '2' ] && x="${tmp}" && tmp=
};;
esac
case "$mchar" in
';')((value++));;
'm'|'M')value=0 && break;;
esac
done
}
#limitations of movenment
[ $x = 1 ] && return
[ $y -lt 6 ] && return
line=$(( base + ( x - 2 ) ))
rl=$(( y - 6 ))
((line > ${#buffer[@]})) && line=${#buffer[@]}
((rl > ${#buffer[line]})) && rl=${#buffer[line]}
redraw
return
}
buffer[line]="${buffer[line]:0:${rl}}$1${buffer[line]:${rl}}" #add the key at $rl (cursor position)
right #go right with cursor
megaredrawline #not sure why this is needed but it prevents duplication of the letter
redraw #draw it
modified=true
}
main() {
printf '\e[?1047h' # Switch to alternative buffer
echo -ne "\e[?1000;1006;1015h" #enable mouse tracking
if [[ "$1" ]]; then # If a file was provided in the terminal pre-load it
redraw # Draw out the UI before loading file
read_buffer "$1"
fi
redraw
while true; do
local -a k=()
local -i i=1
if read -rsN1 -t"${BED_REFRESH_TIMEOUT:=1}" k[0]; then # Check for ready input
while read -rsN1 -t0.0001 k[$i]; do ((i++)); done # Multibyte hack
keytype "$(printf '%s' "${k[@]}")" # Handle keypress event
fi
done
}
pipesyntax() {
comment=false
stringone=false
stringtwo=false
word=
bckIFS="${IFS}"
IFS=
colour(){
printf "\033[%s;%s;%sm%s\033[0;0m" "$1" "$2" "$3" "$4"
}
readstuff() {
while read -rsn1 char
do
[ "$char" = " " ] && syntax && word='' && printf ' ' && continue
[ "$char" = " " ] && syntax && word='' && printf '\033[0;37;48m%s\033[0;0m' "${BANO_RENDER_TAB:=.}" && continue
[ -z "$char" ] && syntax && word='' comment='false' && echo && continue
word="${word}${char}" && printf '%s\e[%sD' "$word" "${#word}"
done
}
readstuff
IFS="${bckIFS}"
}
main "$@"
printf '\e[?7h'