commit initial
|
1 |
#!/bin/bash |
2 | ||
3 |
test -n "$1" \ |
|
4 |
&& egrep -q '^[[:digit:]]+$' <<< $1 \ |
|
5 |
&& DEFAULT_ENTRY="$1" |
|
6 | ||
7 |
# mettre une valeur quelconconque pour entrer en mode deboggage, vide pour prod |
|
8 |
DEBUG= |
|
9 | ||
10 |
unset menuadm |
|
11 |
declare -A menuadm |
|
12 | ||
13 |
function build_menu () { |
|
14 |
awk -F'::' -v dq='"' \ |
|
15 |
'BEGIN { |
|
16 |
printf("unset action label translation;\n") |
|
17 |
printf("declare -a label translation action;\n") |
|
18 |
} |
|
19 |
!/^\s*(#|$)/ { |
|
20 |
if ($2 != "") { |
|
21 |
printf("translation[${#translation[*]}]=${#label[*]};\n") |
|
22 |
} |
|
23 |
printf("action[${#action[*]}]=%s;\n", dq $2 dq) |
|
24 |
printf("label[${#label[*]}]=%s;\n", dq $1 dq) |
|
25 |
}' ${menuadm[menulist]} |
|
26 |
} |
|
27 | ||
28 |
function show_menu_debug () { |
|
29 |
build_menu |
|
30 |
eval $(build_menu) |
|
31 |
read |
|
32 |
until check_answer; do |
|
33 |
show_labels |
|
34 |
read -p "Choix: " |
|
35 |
done |
|
36 |
echo "${action[$REPLY]}" |
|
37 |
} |
|
38 | ||
39 |
function show_menu () { |
|
40 |
eval $(build_menu) |
|
41 |
until check_answer; do |
|
42 |
show_labels |
|
43 |
test -n "$DEFAULT_ENTRY" \ |
|
44 |
&& test $DEFAULT_ENTRY -ge 0 \ |
|
45 |
&& test $DEFAULT_ENTRY -lt ${#translation[*]} \ |
|
46 |
&& DEFAULT_ENTRY_LABEL="$(get_chapter)" |
|
47 |
read -p "Choix${DEFAULT_ENTRY_LABEL}: " |
|
48 |
test -n "$DEFAULT_ENTRY_LABEL" \ |
|
49 |
&& REPLY=${REPLY:-$DEFAULT_ENTRY} |
|
50 |
done |
|
51 |
eval "${action[$REPLY]}" |
|
52 |
} |
|
53 | ||
54 |
function get_chapter () { |
|
55 |
for ((i=0, j=0; i<${#label[*]}; i++)); do |
|
56 |
if test -z "${action[$i]}"; then |
|
57 |
test -n "$DEFAULT_ENTRY" && test $DEFAULT_ENTRY -ge $j && chapter=${label[i]} |
|
58 |
else |
|
59 |
let j++ |
|
60 |
fi |
|
61 |
done |
|
62 |
echo -e " [$chapter / ${label[${translation[$DEFAULT_ENTRY]}]}]" |
|
63 |
} |
|
64 | ||
65 |
function show_labels () { |
|
66 |
for ((i=0, j=0; i<${#label[*]}; i++)); do |
|
67 |
if test -z "${action[$i]}"; then |
|
68 |
prefix="\n : " |
|
69 |
else |
|
70 |
prefix="$j:-->" |
|
71 |
let j++ |
|
72 |
fi |
|
73 |
echo -e "$prefix:${label[$i]}" |
|
74 |
done | column -ets ":" |
|
75 |
} |
|
76 | ||
77 |
function check_answer () { |
|
78 |
test -n "$REPLY" \ |
|
79 |
&& egrep -q '^[[:digit:]]+$' <<< $REPLY \ |
|
80 |
&& test $REPLY -ge 0 \ |
|
81 |
&& test $REPLY -lt ${#translation[*]} \ |
|
82 |
&& REPLY=${translation[$REPLY]} |
|
83 |
} |
|
84 | ||
85 |
# répertoire éléments du menu, priorité au menu perso |
|
86 |
if test -d $HOME/.local/share/$(basename $0); then |
|
87 |
menuadm[share]=$HOME/.local/share/$(basename $0) |
|
88 |
else |
|
89 |
menuadm[share]=/usr/local/share/$(basename $0) |
|
90 |
fi |
|
91 | ||
92 |
menuadm[keys]="${menuadm[share]}/keys" |
|
93 |
menuadm[contents]="${menuadm[share]}/contents" |
|
94 |
menuadm[api]=${menuadm[share]}/api |
|
95 | ||
96 |
# récupération de la config |
|
97 |
for conf in /etc/$(basename $0)/$(basename $0).conf $HOME/.config/$(basename $0)/$(basename $0).conf; do |
|
98 |
if test -r $conf; then |
|
99 |
eval $(awk -F'=' '!/^\s*(#|$)/ { |
|
100 |
printf("menuadm[%s]=%s\n", |
|
101 |
gensub("^\\s*(\\w.*\\w)\\s*$","\\1","",$1), |
|
102 |
gensub("^\\s*(\\w.*\\w)\\s*$","\\1","",$2)) |
|
103 |
}' $conf) |
|
104 |
fi |
|
105 |
done |
|
106 | ||
107 |
test -r ${menuadm[api]} && source ${menuadm[api]} |
|
108 | ||
109 |
# si pas de menu à afficher, on quitte... |
|
110 |
test -d ${menuadm[contents]} || exit 1 |
|
111 | ||
112 |
for menu in ${menuadm[contents]}/*; do |
|
113 |
if test -r $menu; then |
|
114 |
menuadm[menulist]+=$menu |
|
115 |
menuadm[menulist]+=" " |
|
116 |
fi |
|
117 |
done |
|
118 | ||
119 |
if test -n "$DEBUG"; then |
|
120 |
show_menu_debug |
|
121 |
else |
|
122 |
show_menu |
|
123 |
fi |