-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwb_dircolors
executable file
·680 lines (571 loc) · 16.4 KB
/
pwb_dircolors
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#!/usr/bin/env bash
enable ate
enable pwb
source <( pwb_sources \
ate_confirm_functions \
ate_exit_on_error \
pwb_exit_on_error \
pwb_print_head \
pwb_print_message \
pwb_erase_dims \
pwb_edit_line \
)
# Prepared this collection of label descriptions from using
# `dircolors -p` and the contents of the page at URL:
# https://www.bigsoft.co.uk/blog/2008/04/11/configuring-ls_colors
declare -a LCLABELS=(
no:normal
fi:file
rs:reset
di:directory
ln:symbolic\ link
mh:multi-hard\ link
pi:FIFO\ pipe
so:socket
do:door
bd:block\ device
cd:char\ device
or:orphan
mi:missing
su:setuid
sg:setgid
ca:capability
tw:sticky\ other-writable
ow:other-writable
st:sticky
ex:executable
lc:opening\ terminal\ code
rc:closing\ terminal\ code
ec:non-filename\ text
)
declare LC_DEMO_STRING="test"
show_help()
{
local pwbh="$1"
local -a help_lines=(
" Use navigation keys and emacs keys to move around. "
" Press ENTER to open a dialog on the selected color. "
""
" Press '?' to see this page."
" Press 's' to save settings."
" Press 'q' to exit."
)
help_lines+=( "" " Press any key to close help." )
local -a msg_dims
pwb_print_message help_lines -a msg_dims
read -n1
pwb_erase_dims msg_dims
}
print_header()
{
local pwbh="$1"
local source="$2"
if [ -z "$source" ]; then
source="default"
else
source="file '$source'"
fi
local -a header_lines=(
$'\e[44;37m PWB DIRCOLORS \e[m'
"Source: $source"
"Press '?' for help"
)
pwb_print_head "$pwbh" header_lines
}
add_entry_dialog()
{
local pwbh="$1"
local -a dialog_lines=(
$'\e[7m Create New Entry \e[m'
""
""
""
" Press ENTER to save, ESC to cancel "
)
local curline
local exit_key
local -a exit_keys=( $'\cm' $'\e' )
local -i maxlen=40
local -a fargs=(
curline
exit_key
exit_keys
-l "$maxlen"
)
local -a msg_dims
while :; do
printf -v dialog_lines[2] $' \e[48;2;64;64;64m%s\e[5m_%*s\e[m ' \
"$curline" $(( maxlen - ${#curline} )) " "
pwb_print_message dialog_lines -a msg_dims
if ! pwb_edit_line "${fargs[@]}"; then
pwb_erase_dims msg_dims
if [[ "$exit_key" == $'\cm' ]]; then
// Make a new entry in the data source
local -a newrow=( "$curline" "0" "" )
pwb get_data_source "$pwbh"
ate append_data "$PWB_VALUE" "${newrow[@]}"
ate index_rows "$PWB_VALUE"
ate get_row_count "$PWB_VALUE"
pwb update_data_count "$pwbh" "$ATE_VALUE"
# Figure out how to move to and edit the new entry:
# Perhaps use the undocumented "trigger" action?
pwb print_row "$pwbh" "$ATE_VALUE"
break
elif [[ "$exit_key" == $'\e' ]]; then
return 1
fi
fi
done
}
get_current_color_string()
{
local -n gccs_return="$1"
local atable="$2"
local -a cs_array
gccs_callback()
{
local -n gc_row="$1"
cs_array+=( "${gc_row[*]:0:2}" )
}
local IFS=$'='
ate walk_rows "$atable" gccs_callback
ate_exit_on_error
local IFS=':'
gccs_return="${cs_array[*]}:"
}
export_current_colors()
{
local atable="$1"
local -a ecc_msg=( "\$0 is '$0', compared to ${BASH_SOURCE[0]}" )
local -i writable=0
if [[ "$0" == "${BASH_SOURCE[0]}" ]]; then
ecc_msg+=(
"The color changes will not be saved."
"To enable saving the values to the LS_COLORS environment"
"variable, quit and call this script as a source:"
""
". ${BASH_SOURCE[0]}"
" or"
"source ${BASH_SOURCE[0]}"
""
"to enable changing the LS_COLORS environment variable."
""
"Press 'q' to quit, ESC to continue."
)
else
writable=1
ecc_msg+=(
"Changed colors will be written to the environment."
""
"Press 'q' to quit, 'w' to write, ESC to abort."
)
fi
declare -a dims
pwb_print_message -a dims ecc_msg
while :; do
pwb get_keystroke
case "$PWB_VALUE" in
w )
if [ "$writable" -eq 1 ]; then
local colstr
get_current_color_string colstr "$atable"
LS_COLORS="$colstr"
export LS_COLORS
break
fi
;;
$'\e' ) break ;;
'q' ) exit ;;
esac
done
pwb_erase_dims dims
}
get_filename_dialog()
{
local -n gf_fname="$1"
local pwbh="$2"
local -a dialog_lines=(
" Enter a filename to which the contents will be written. "
""
""
""
" Press ENTER to use, ESC to cancel"
)
local curline="$gf_fname"
local exit_key
local -i maxlen=50
# Array of keystroke strings that will terminate the line edit function:
local -a exit_keys=( $'\cm' $'\e' )
# Arguments to the pwb_edit_line function
local -a fargs=(
curline
exit_key
exit_keys
-l "$maxlen"
)
local -a msg_dims
while :; do
# Update text-entry line
printf -v dialog_lines[2] $' \e[48;2;64;64;64m%s\e[5m_\e[25m%*s\e[m' \
"$curline" $(( maxlen - ${#curline} )) " "
# Overwrite the previous version of the dialog
pwb_print_message dialog_lines -a msg_dims
# Hand off to process keypresses
if ! pwb_edit_line "${fargs[@]}"; then
pwb_erase_dims msg_dims
# allow pwb_edit_line to handle everything
# until it gets an exit key, at which time it comes
# back here to respond appropriately
if [[ "$exit_key" == $'\cm' ]]; then
gf_fname="$curline"
break
elif [[ "$exit_key" == $'\e' ]]; then
return 1
fi
fi
done
return 0
}
# Save current database to a file, updating global LFILENAME
# variable with the file name.
save_contents_dialog()
{
local pwbh="$1"
local sc_file_name="$LFILENAME"
if get_filename_dialog sc_file_name "$pwbh"; then
local -a ctc_colors
add_el()
{
local -n ae_colors="ctc_colors"
local -n ae_row="$1"
local glob="${ae_row[0]}"
local cvalue="${ae_row[1]}"
ae_colors+=( "${glob}=$cvalue" )
# echo "Added '${glob}=$cvalue', the ${#ae_colors[*]} element"
}
ate walk_rows LTABLE add_el
local IFS=':'
echo "${ctc_colors[*]}:" > "$sc_file_name"
local -n scd_name="LFILENAME"
LFILENAME="$sc_file_name"
print_header "$pwbh" "$sc_file_name"
fi
}
find_label()
{
local -n fl_return="$1"
local name="$2"
local lpair
for lpair in "${LCLABELS[@]}"; do
if [ "${name}:" = "${lpair:0:3}" ]; then
fl_return="${lpair#*:}"
return 0
fi
done
return 1
}
# Load an ATE table with colors from LS_COLORS or,
# if missing, from `dircolors -b`.
get_user_file_colors()
{
local gufc_tname="$1"
ate get_array_name "$gufc_tname"
local -n gufc_tarray="$ATE_VALUE"
local label
local -a pair
# One argument means read values from environment or generate new ones:
if [ "$#" -eq 1 ]; then
# Ensure LS_COLORS is set
if ! declare -p LS_COLORS >/dev/null 2>&1 ; then
eval $( dircolors -b )
fi
while IFS='=' read -r -d ":" -a pair; do
if ! find_label label "${pair[0]}"; then
label=""
fi
gufc_tarray+=( "${pair[@]}" "$label" 0 )
done <<< "$LS_COLORS"
else
while IFS='=' read -r -d ":" -a pair; do
if ! find_label label "${pair[0]}"; then
label=""
fi
gufc_tarray+=( "${pair[@]}" "$label" 0 )
done < "$2"
fi
ate index_rows "$gufc_tname"
sort_func()
{
local -n sf_return="$1"
local -n sf_left="$2"
local -n sf_right="$3"
local left_val="${sf_left[0]}"
local right_val="${sf_right[0]}"
local -i len_left="${#left_val}"
local -i len_right="${#right_val}"
# keep two-letter and glob specs separate:
if (( len_left==2 && len_right>2 )); then
sf_return=-1
elif (( len_left>2 && len_right==2 )); then
sf_return=1
# if both on same side of the two-letter border, compare directly:
elif [[ "$left_val" < "$right_val" ]]; then
sf_return=-1
elif [[ "$left_val" == "$right_val" ]]; then
sf_return=0
else
sf_return=1
fi
}
}
pl_print_line()
{
local -i index="$1"
local dsource="$2"
local -i char_count="$3"
local -i in_focus="$4"
ate get_row "$dsource" "$index"
local ref="${ATE_ARRAY[0]}"
local val="${ATE_ARRAY[1]}"
local label="${ATE_ARRAY[2]}"
local -i selected="${ATE_ARRAY[3]}"
local hilite=""
if [ "$in_focus" -ne 0 ]; then
hilite=$'\e[7m'
elif [ "$selected" -ne 0 ]; then
hilite=$'\e[48;2;64;64;0m'
fi
printf -v tline $'%s%*s >\e[27m \e[%sm%s\e[m %s< %s\e[m' \
"$hilite" \
"$GLOB_WIDTH" \
"$ref" \
"$val" \
"$LC_DEMO_STRING" \
"$hilite" \
"$label"
pwb limit_print "$char_count" "$tline"
# Restore defaults before returning:"
printf $'\e[39;49m'
}
edit_color_dialog()
{
local -n pec_return="$1"
local og_color="$2"
local pwb_handle="$3"
pec_return=""
# dialog variables
local new_color="$2"
local black_square=$'\u25a0'
local black_square=$'\u25fc'
local title=$'\e[7m Enter a Color \e[27m'
local ogc_line new_line
printf -v ogc_line $'Original Color: |\e[%sm %s \e[m|' "$og_color" "$black_square"
local choice_line="ENTER to save, ESC to abort"
local keyp
local -a col_dialog=(
"$title"
"$ogc_line"
""
""
"$choice_line"
)
pwb measure_string "$title"
local -i maxlen=$(( PWB_VALUE - 4 ))
local blinky=$'\e[5m\u2581\e[m'
local start_field
printf -v start_field $'\e[48;2;128;128;128m\e[%dX' "$maxlen"
local -i exit_code=1
local -a msg_dims
while :; do
local -i collen="${#new_color}"
printf -v col_dialog[2] $' Edited Color: |\e[%sm %s \e[m|' \
"$new_color" "$black_square"
printf -v col_dialog[3] " %s%s%s" \
"$start_field" \
"${new_color}$blinky" \
$'\e[m'
pwb_print_message col_dialog -a msg_dims
pwb get_keystroke
if [[ "$PWB_VALUE" == $'\e' ]]; then
break
elif [[ "$PWB_VALUE" == $'\cm' ]]; then
exit_code=0
pec_return="$new_color"
break
# Backspace key emits DEL (\x7f)
elif [[ "$PWB_VALUE" == $'\x7f' ]]; then
if [ "${#new_color}" -gt 0 ]; then
new_color="${new_color:0:-1}"
fi
elif [[ "$PWB_VALUE" =~ ^[\;[:digit:]]$ ]]; then
if (( collen < maxlen )); then
new_color="${new_color}$PWB_VALUE"
fi
else
PWB_VALUE="${PWB_VALUE///[31;1m^[[m}"
fi
done
if [ "${#msg_dims[@]}" -eq 4 ]; then
pwb_erase_dims msg_dims
fi
pwb print_all "$pwb_handle"
return "$exit_code"
}
toggle_selection()
{
shift
local -i index="$1"
local dsource="$2"
local pwbh="$3"
local -a row
ate get_row "$dsource" "$index" -a row
ate_exit_on_error
(( row[3] = row[3]==0 ? 1 : 0 ))
ate put_row "$dsource" "$index" row
ate_exit_on_error
}
update_selected_rows()
{
local atable="$1"
local cval="$2"
set_col()
{
local -n sc_row="$1"
local -i index="$2"
if (( sc_row[3] != 0 )); then
sc_row[1]="$cval"
sc_row[3]=0
ate put_row "$atable" "$index" sc_row
fi
}
ate walk_rows "$atable" set_col
}
pl_exec_line()
{
local keyp="$1"
local dsource="$3"
local pwbh="$4"
case "$keyp" in
$'\cm' )
local -i index="$2"
local -a pel_row
ate get_row "$dsource" "$index" -a pel_row
local val="${pel_row[1]}"
local newval
if edit_color_dialog newval "$val" "$pwbh"; then
pel_row[1]="$newval"
ate put_row "$dsource" "$index" pel_row
update_selected_rows "$dsource" "$newval"
pwb print_all "$pwbh"
fi
;;
's' ) save_contents_dialog "$pwbh" ;;
'?' ) show_help "$pwbh" ;;
'a' ) add_entry_dialog "$pwbh" ;;
'u' ) export_current_colors "$dsource" ;;
+|= )
toggle_selection "$@"
pwb trigger "$pwbh" 1
;;
esac
pwb print_all "$pwbh" "$index"
}
set_traps()
{
local func="$1"
shift
for sig in "$@"; do
trap "$func $sig" "$sig"
done
}
get_printed_width()
{
local max_line_name="$1"
local max_glob_name="$2"
local tname="$3"
# Zero targets of name references
local -n gpw_max_glob_len="$max_glob_name"
gpw_max_glob_len=0
# Track label length only for calculated concatenated length
local -i max_label_len=0
count_row()
{
local -n cr_max_glob_len="$max_glob_name"
local -n cr_max_label_len="max_label_len"
local -n cr_row="$1"
local -i len_glob="${#cr_row[0]}"
local -i len_label="${#cr_row[2]}"
if (( cr_max_glob_len < len_glob )); then
(( cr_max_glob_len = len_glob ))
fi
if (( cr_max_label_len < len_label )); then
(( cr_max_label_len = len_label ))
fi
}
ate walk_rows "$tname" count_row
# The values are valid only AFTER walking the rows.
local -i len_string="${#LC_DEMO_STRING}"
local -n gpw_max_line_len="$max_line_name"
gpw_max_line_len=$(( max_label_len + gpw_max_glob_len + len_string + 2 ))
}
declare emacs_keymap=(
'q' 0 # exit
$'\cn' 1 # down one line
$'\cp' 2 # up one line
$'\cv' 3 # down one page
$'\ev' 4 # up one page
$'\e>' 5 # end of document
$'\e<' 6 # top of document
$'\cm' 7 # execute (open line)
'?' 7 # execute (show help)
's' 7 # execute (save to file)
'u' 7 # use colors
'a' 7 # execute (add entry)
'+' 7 # for persistent select
'=' 7 # (unshifted +) for persistent select
)
declare EMACS_KEYMAP
pwb declare_keymap EMACS_KEYMAP emacs_keymap
pwb_exit_on_error
#############################
### BEGIN EXECUTION ###
#############################
declare LFILENAME=""
# Table to hold color names, values, and labels (where available):
declare LTABLE
ate declare LTABLE 4
# Prepare arg array so arguments to 'get_user_file_colors'
# only gets two arguments if the call to pwb_lscolors gets
# a filename argument.
declare get_user_file_colors_args=( LTABLE )
if [ "$#" -gt 0 ]; then
LFILENAME="$1"
get_user_file_colors_args+=( "$LFILENAME" )
fi
get_user_file_colors "${get_user_file_colors_args[@]}"
# Get row_count for pwb declare
ate get_row_count LTABLE
ate_exit_on_error
declare pwb_view
pwb declare pwb_view -h LTABLE "$ATE_VALUE" pl_print_line -e pl_exec_line
pwb_exit_on_error
# Calculate side margins for centering table
declare -i pwidth GLOB_WIDTH
get_printed_width pwidth GLOB_WIDTH LTABLE
pwb get_dimensions
pwb_exit_on_error
declare rmarg=$(( (PWB_ASSOC[screen_cols] - pwidth) / 2 ))
declare lmarg=$(( (pwidth > PWB_ASSOC[screen_cols] - (rmarg * 2)) ? rmarg-1 : rmarg ))
pwb set_margins pwb_view 5 "$rmarg" 5 "$lmarg"
pwb_exit_on_error
trap_signal()
{
# echo
# read -n1 -p "trapped $1"
pwb restore
}
set_traps trap_signal EXIT SIGINT SIGABRT SIGKILL
pwb init
print_header pwb_view "$LFILENAME"
pwb start pwb_view -a EMACS_KEYMAP
pwb_exit_on_error