-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwb_ls
executable file
·649 lines (537 loc) · 14.5 KB
/
pwb_ls
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
#!/usr/bin/env bash
enable ate
enable pwb
source <( pwb_sources \
ate_exit_on_error \
pwb_exit_on_error \
pwb_newline_head \
pwb_print_message \
)
declare PWB_MARGIN_COLOR=$'\e[48;2;64;64;64m'
declare ATTR_INDENT=" "
declare STAT_ITEM_IFS='|'
declare EXIT_KEYP
declare COLOR_DIR
declare COLOR_PDF
declare COLOR_IMG
# $'\cm' (ENTER KEY) opens the file entry
declare -a STAT_KEYMAP=(
$'\e' 7 # ESC to go back one level
e 7 # to edit (with EMACS)
r 7 # to read (with EMACS)
o 7
t 7 # for testing (for now)
q 7
)
log_msg()
{
local str="$1"
local -i color="${2:-1}"
printf $'\e[3%d;1m%s\n\e[m' "$color" "$str" >&2
}
prepare_color_constants()
{
local IFS=':'
local -a colvals=( $LS_COLORS )
IFS='='
local -a colpair
for col in "${colvals[@]}"; do
colpair=( $col )
case "${colpair[0]}" in
di ) COLOR_DIR="${colpair[1]}" ;;
.pdf ) COLOR_PDF="${colpair[1]}" ;;
.bmp ) COLOR_IMG="${colpair[1]}" ;;
esac
done
if [ -z "$COLOR_PDF" ]; then
COLOR_PDF=$'38;2;255;255;0'
fi
}
##################################
### FILE ATTRIBUTES PROCESSING ###
##################################
# This array is used to create other working
# arrays for program execution. It's easier
# to add here from `man stat(1)` and let the
# script manage taking what it needs.
declare -a STAT_FILE_ATTRIBUTES=(
F "File type"
f "Raw mode"
# a "Permission bits"
A "Permission settings"
s "size in bytes"
U "Owner's name"
G "Owner's group name"
w "File created"
x "File access"
y "File modified"
z "File status"
N "Link name"
# h "Hard-link count"
# m "Mount point"
# t "File system type"
# T "Human-readable file system type"
)
declare STAT_ITEM_FORMAT
declare -a STAT_ATTRIBUTE_KEYS
declare -a STAT_ATTRIBUTE_LABELS
declare -i STAT_KEY_COUNT
declare -a STAT_OUTPUT_ARRAY
find_stat_by_token()
{
local -n fsbt_return="$1"
local -n fsbt_array="$2"
local key="$3"
local IFS=
local SKEYS="${STAT_ATTRIBUTE_KEYS[*]}"
local -i ndx="${SKEYS#*$key}"
if [ "$ndx" -ge 0 ]; then
fsbt_return="${fsbt_array[$ndx]}"
return 0
fi
return 1
}
construct_stat_attribute_keys()
{
STAT_ATTRIBUTE_KEYS=()
STAT_ATTRIBUTE_LABELS=()
local token
local -i count=0
for attr in "${STAT_FILE_ATTRIBUTES[@]}"; do
if [ -z "$token" ]; then
token="$attr"
else
STAT_ATTRIBUTE_KEYS+=( "$token" )
STAT_ATTRIBUTE_LABELS+=( "$attr" )
(( ++count ))
token=
fi
done
STAT_KEY_COUNT="$count"
}
# Construct a format string for `stat` output
construct_stat_format()
{
local -a tokens=()
local token
for attr in "${STAT_FILE_ATTRIBUTES[@]}"; do
if [ -z "$token" ]; then
token="$attr"
else
tokens+=( "%${token}" )
token=
fi
done
local IFS="$STAT_ITEM_IFS"
STAT_ITEM_FORMAT="${tokens[*]}"
}
construct_header_attribute_array()
{
local -n chaa_index_array="$1"
local -n chaa_token_array="$2"
while token in "${chaa_token_array[@]}"; do
local -i index=0
while key in "${STAT_ATTRIBUTE_KEYS[@]}"; do
if [[ "$key" == "token" ]]; then
chaa_index_array+=( "$index" )
break
else
(( ++index ))
fi
done
done
}
get_stat_key_index()
{
local -n gski_index="$1"
local search_key="$2"
if [[ "$search_key" == "M" ]]; then
gski_index=-1
return 0
else
local -i index=0
for key in "${STAT_ATTRIBUTE_KEYS[@]}"; do
if [[ "$key" == "$search_key" ]]; then
gski_index="$index"
return 0
fi
(( ++index ))
done
fi
return 1
}
construct_output_array()
{
STAT_OUTPUT_ARRAY=()
if [ "$#" -eq 0 ]; then
local -i count="${#STAT_ATTRIBUTE_KEYS[*]}"
for ((i=-1; i<STAT_KEY_COUNT; ++i)); do
STAT_OUTPUT_ARRAY+=( "$i" )
done
else
local -n coa_key_array="$1"
local -i index
for key in "${coa_key_array[@]}"; do
if get_stat_key_index index "$key"; then
STAT_OUTPUT_ARRAY+=( "$index " )
fi
done
fi
}
######################################
### TABLE BUILDING AND MAINTENANCE ###
######################################
load_table()
{
local tname="$1"
local path="$2"
ate get_array_name "$tname"
ate_exit_on_error
local -n lt_tarray="$ATE_VALUE"
lt_tarray=()
local -a mimetype_args=()
if mimetype --version >/dev/null 2>&1; then
mimetype_args=( mimetype -L )
else
mimetype_args=( file -L --mime-type )
fi
# Get common file list for two read loops to ensure they sync later
local -a filelist=( "${path}"/* )
# Make sparse ATE table to fill later with stats
local -a iarray
while IFS=':' read -r -a iarray; do
# trim path from file name and leading spaces from mime-type
lt_tarray+=( "${iarray[0]##*/}" "${iarray[1]##* }" "" )
done < <( "${mimetype_args[@]}" "${filelist[@]}" )
ate index_rows "$tname"
ate_exit_on_error
# Take advantage of fast indexed access to fill missing fields
local stat_line
local -i index=0
while read -r stat_line; do
ate get_row "$tname" "$index"
ate_exit_on_error
ATE_ARRAY[2]="$stat_line"
ate put_row "$tname" "$index" ATE_ARRAY
ate_exit_on_error
(( ++index ))
done < <( stat --format="$STAT_ITEM_FORMAT" "${filelist[@]}" )
}
#####################
### PWB CALLBACKS ###
#####################
print_line()
{
local -i index="$1"
local dname="$2"
local -i char_limit="$3"
ate get_row "$dname" "$index"
ate_exit_on_error
local OIFS="$IFS"
local IFS
local name="${ATE_ARRAY[0]}"
local mimetype="${ATE_ARRAY[1]}"
IFS="$STAT_ITEM_IFS"
local -a stats=( ${ATE_ARRAY[2]} )
local link_char=' '
IFS="$OIFS"
local file_type
if find_stat_by_token file_type stats F; then
if [[ "$file_type" == "symbolic link" ]]; then
link_char="*"
fi
elif find_stat_by_token file_type stats A; then
if [[ "${file_type:0:1}" == 'l' ]]; then
link_char="*"
fi
fi
local color=""
IFS='/'
if [[ "$mimetype" == application/pdf ]]; then
printf -v color $'\e[%sm' "$COLOR_PDF"
else
local -a mimepair=( $mimetype )
case "${mimepair[0]}" in
inode )
printf -v color $'\e[%sm' "$COLOR_DIR"
name="/$name"
;;
image )
printf -v color $'\e[%sm' "$COLOR_IMG"
;;
esac
fi
IFS="$OIFS"
if [ -n "$color" ]; then
printf "%s" "$color"
fi
local tline
printf -v tline "%c%-*s" "$link_char" "$char_limit" "${name}"
printf "%s" "${tline:0:$char_limit}"
printf $'\e[m'
}
print_top()
{
local -i char_limit="$3"
printf $'\e[%dX\e[%dG%s' "$char_limit" "$sdi_path_centering" "$sdi_path"
}
append_output_attribute()
{
local -n pra_output_array="$1"
local -n pra_attributes="$2"
local mime_type="$3"
local -i index="$4"
local label value
if [ "$index" -eq -1 ]; then
label="Mime-type"
value="$mime_type"
elif (( index < STAT_KEY_COUNT )); then
label="${STAT_ATTRIBUTE_LABELS[$index]}"
value="${pra_attributes[$index]}"
else
printf "Index %d is out-of-range.\n" "$index"
exit 1
fi
pra_output_array+=( "$label" "${ATTR_INDENT}$value" )
}
print_right()
{
local -i index="$1"
local dname="$2"
local -i char_limit="$3"
local pname="$5"
# Get item data:
ate get_row "$dname" "$index"
ate_exit_on_error
local OIFS="$IFS"
local IFS="$STAT_ITEM_IFS"
local name="${ATE_ARRAY[0]}"
local mimetype="${ATE_ARRAY[1]}"
local -a attributes=( ${ATE_ARRAY[2]} )
IFS="$OIFS"
unset OIFS
# Stage output into an array to prevent screen overflow:
local -a outlines=( "$name" "" )
local def_app
if [ -n "$mimetype" ]; then
def_app=$( xdg-mime query default "$mimetype" )
else
def_app="n/a"
fi
outlines+=( "Default application", "${ATTR_INDENT}$def_app" )
for key_index in "${STAT_OUTPUT_ARRAY[@]}"; do
append_output_attribute outlines attributes "$mimetype" "$key_index"
done
# Start printing
# Calculate positions for output, need handle for index 'margin_right' nad 'pager_lines'
pwb get_dimensions "$pname"
pwb_exit_on_error
local -i left_start=$(( PWB_ASSOC[screen_cols] - PWB_ASSOC[margin_right] + 1 ))
local -i lines="${PWB_ASSOC[pager_lines]}"
local -i output_count="${#outlines[*]}"
local curline
# Set background color
printf "$PWB_MARGIN_COLOR"
# Print output lines
for ((i=0; i<lines; ++i)); do
# erase line
printf $'\e[%dX' "$char_limit"
if (( i < output_count )); then
curline="${outlines[$i]}"
printf "%s" "${curline:0:$char_limit}"
fi
# move to next line (at proper margin)
printf $'\e[1E\e[%dG' "$left_start"
done
# Cancel background color
printf $'\e[m'
}
execute_line_emacs()
{
local -i index="$1"
local dname="$2"
local pname="$3"
local -i readonly="${4:-0}"
ate get_row "$dname" "$index"
ate_exit_on_error
local name="${ATE_ARRAY[0]}"
local path="${sdi_path}/$name"
local mimetype="${ATE_ARRAY[1]}"
local -a term_args=(
--window
--working-directory="$sdi_path"
--
emacs "$path"
)
if [ "$readonly" -ne 0 ]; then
term_args+=( --eval '(setq buffer-read-only 1)' )
fi
gnome-terminal "${term_args[@]}"
if [ "$?" -ne 0 ]; then
read -n1 -pSee\ your\ error?
fi
}
execute_line_open()
{
local -i index="$1"
local dname="$2"
local pname="$3"
ate get_row "$dname" "$index"
ate_exit_on_error
local name="${ATE_ARRAY[0]}"
local path="${sdi_path}/$name"
local mimetype="${ATE_ARRAY[1]}"
local OIFS="$IFS"
local IFS='='
local -a mimeparts=( $mimetype )
IFS="$OIFS"
local -a xte_args=(
--window
--working-directory="$sdi_path"
--
)
case "$mimetype" in
inode/directory )
local -n ELO_EXIT_KEYP=EXIT_KEYP
start_directory_instance "$path"
if [[ "$ELO_EXIT_KEYP" == "q" ]]; then
return 0
fi
pwb print_all "$pname"
;;
application/x-troff-man )
# text/troff )
gnome-terminal "${xte_args[@]}" man -l "$path"
;;
text/markdown )
chromium "$path" 2>/dev/null
;;
* )
local dtapp
dtapp=$( xdg-mime query default "$mimetype" )
if [ "$?" -eq 0 ]; then
if [[ "$dtapp" == "emacsclient.desktop" ]]; then
gnome-terminal "${xte_args[@]}" emacs "$path"
elif [ -n "$dtapp" ]; then
gtk-launch "$dtapp" "$name"
if [ "$?" -ne 0 ]; then
echo
echo "Failed to open '$name' with '$dtapp'"
echo
read -n1 -p Press
fi
fi
fi
;;
esac
}
execute_line()
{
# Get global EXIT_KEYP variable
local -n EL_EXIT_KEYP=EXIT_KEYP
EL_EXIT_KEYP=
local arrname keyp="${1///\\e}"
ate get_array_name "$3" -v arrname
local pwb_handle="$4"
case "$1" in
$'\e' )
EL_EXIT_KEYP=$'\e'
return 1 ;;
q | Q )
EL_EXIT_KEYP="q"
return 1 ;;
o | $'\cm' )
execute_line_open "${@:2}"
if [[ "$EL_EXIT_KEYP" == 'q' ]]; then
return 1
fi
;;
e ) execute_line_emacs "${@:2}" ;;
r ) execute_line_emacs "${@:2}" 1 ;;
t )
local -a msg=(
"This is a long line of text"
"This is your bogus second"
""
"Press any key to continue"
)
pwb_print_message "$pwb_handle" msg
read -n1
pwb print_all "$pwb_handle"
;;
esac
}
set_pwb_split_screen_margins()
{
local pwb_name="$1"
pwb get_dimensions
pwb_exit_on_error
declare -i right_margin=$(( ( PWB_ASSOC[screen_cols] - 2 ) / 2 ))
pwb set_margins "$pwb_name" 2 "$right_margin" 2 2
pwb_exit_on_error
}
################################
### START DIRECTORY INSTANCE ###
################################
start_directory_instance()
{
local sdi_path="$1"
# At current scope, create and populate file contents table:
local sdi_table
ate declare sdi_table 3
ate_exit_on_error
load_table sdi_table "$sdi_path"
ate get_row_count sdi_table
ate_exit_on_error
local sdi_display
pwb declare sdi_display \
sdi_table "$ATE_VALUE" \
print_line \
-t print_top \
-r print_right \
-e execute_line
local sdi_keymap
pwb declare_keymap sdi_keymap STAT_KEYMAP
pwb_exit_on_error
set_pwb_split_screen_margins sdi_display
# Declare the variable so scope/recursion works:
pwb get_dimensions
pwb_exit_on_error
# position for centring path:
local -i sdi_path_centering="${#sdi_path}"
(( sdi_path_centering = PWB_ASSOC[screen_cols] - sdi_path_centering ))
(( sdi_path_centering /= 2 ))
local latename
ate get_array_name sdi_table -v latename
local -n G_EXIT_KEYP=EXIT_KEYP
G_EXIT_KEYP=
pwb start sdi_display -a sdi_keymap
if [[ "$G_EXIT_KEYP" == 'q' ]]; then
return 0
else
return 1
fi
}
trap_for_pwb_cleanup()
{
pwb restore
}
trap_for_segfault()
{
echo "Got the segfault signal" &2>pwb.log
}
#######################
### BEGIN EXECUTION ###
#######################
declare START_DIR="${1:-$(pwd)}"
# Prepare global variables
prepare_color_constants
construct_stat_attribute_keys
construct_stat_format
construct_output_array
# Trap errors after pwb init to ensure cleanup for
# an unexpected termination
trap "trap_for_pwb_cleanup" EXIT SIGINT SIGQUIT SIGABRT
trap "trap_for_segfault" SIGSEGV
pwb init
start_directory_instance "$START_DIR"