-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_list.sh
99 lines (85 loc) · 1.78 KB
/
item_list.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
#!/bin/bash
select_items() {
print_heading=$1
local -n items=$2
local -n choices=$3
current_index=0
while (( $? == 0 )) ; do
$print_heading
_show_list
read -n 1 key
clear
_perform_action $key
done
}
_show_list() {
for num in ${!items[@]}; do
line=""
if [[ $current_index == $num ]]; then
line="->"
else
line=" "
fi
echo "$line [${choices[num]:- }] ${items[num]}"
done
_print_keys_legend
}
_print_keys_legend() {
echo -e "\nKeys:"
echo " j -> go down"
echo " k -> go up"
echo " x -> check/uncheck option"
echo " a -> check all"
echo " u -> uncheck all"
echo " d -> done"
}
_perform_action() {
key=$1
case "$key" in
'j') if [[ $current_index -lt $((${#items[@]} - 1)) ]]; then
((current_index+=1));
fi;;
'k') if [[ $current_index -gt 0 ]]; then
((current_index--));
fi ;;
'x') _toggle_item $choices;;
'a') _check_all_items $items $choices;;
'u') _uncheck_all_items $items $choices;;
'd') return 1;;
esac
}
_toggle_item() {
if [[ "${choices[current_index]}" ]]; then
choices[current_index]=""
else
choices[current_index]="X"
fi
}
_check_all_items() {
for position in ${!items[@]}; do
choices[position]="X"
done
}
_uncheck_all_items() {
for position in ${!items[@]}; do
choices[position]=""
done
}
print_selected_items() {
title=$1
local -n items=$2
local -n choices=$3
echo "Selected ${title}:"
for position in ${!choices[@]}; do
if [[ ${choices[position]} ]]; then
echo " - ${items[position]}"
fi
done
}
selected_item() {
local -n choices=$1
position=$2
if [[ -z ${choices[position]} ]]; then
return 1
fi
}