-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot
executable file
·301 lines (259 loc) · 6.44 KB
/
dot
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
#!/bin/bash
function module::install()
{
local module="${1}"
local install_file="${MODS_DIR_BASE}/${module}/install"
local config_file="${MODS_DIR_BASE}/${module}/${MOD_CONFIG_FILE}"
local tmp_func
# source module config file
[ -f "${config_file}" ] && source "${config_file}" || true
# source module install file
if [ "${BASH_SOURCE[0]}" -ef "${0}" ]; then
source "${install_file}"
fi
for prefix in ${PREFIX_INSTALL_MODULE[@]}; do
tmp_func="${prefix}::${module}"
[ "$(type -t ${tmp_func})" == "function" ] && ${tmp_func}
done
}
: ' install function for all modules
DO_MODULE : specific array to do
if len DO_MODULE == 0 do all
'
function modules::install()
{
if [ "${#DO_MODULE[@]}" == 0 ]; then
DO_MODULE=(${MODULE[@]})
for module in ${INGORED_MODULE[@]}; do
DO_MODULE=(${DO_MODULE[@]//${module}})
done
fi
for module in ${DO_MODULE[*]}; do
log::info "installing ${ORE}${module}${RST}"
((LOG_LEVEL++))
module::install "${module}"
((LOG_LEVEL--))
log::pass "installed ${ORE}${module}${RST}"
done
}
function dot::update()
{
log::main_title "${GRE}Checking update${RST}" 1 "${LCYA}" 1
utils::update_repo "https://github.com/Pixailz/dot_files" "${DOT_INSTALL_DIR}"
utils::update_repo "https://github.com/Pixailz/lib_bash" "${DIR_LIB_BASH%\/*}"
}
# print help and quit
function usage()
{
# opt_usage
printf "%b: ${SCRIPT_NAME} " "${P_USAGE}"
printf "[-f|-n] [-p PART1... PARTN]\n"
# desc
printf "\tscripts to manage openvpn server docker\n"
printf "\n\t1. Base\n"
printf "\t\tflag available everywhere\n"
parse::print_usage "h" "help" "Display the help then exit."
parse::print_usage "" "version" "Display the version then exit."
parse::print_usage "" "no-color" "disable all color"
parse::print_usage "f" "force" "To force install"
parse::print_usage "u" "update" "Update this script and lib_bash"
parse::print_usage "p" "part" "install part, available part:"
for part in ${MODULE[@]}; do
parse::print_usage "" "" " - ${part}"
done
parse::print_usage "" "" "should be at the end of command"
printf "\n"
parse::print_infos
#printf " -u, --user install for specified user, default to current"
#TODO
}
function is::option()
{
local options_str=${1}
if [[ ${options_str} =~ --.* ]]; then
return 1
elif [[ ${options_str} =~ -.* ]]; then
return 2
else
return 0
fi
}
: ' parse::short
parse space separated and option separated
'
function parse::short()
{
local to_shift=0
PARSED_ARG=()
shift
for item in ${*}; do
is::option ${item}
[ ${?} != 0 ] && break
PARSED_ARG[${#PARSED_ARG[@]}]=${item}
done
return ${#PARSED_ARG[@]}
}
function parse::post_parsing()
{
local is_good
if [ "${HAVE_PART}" == "1" ] && [ "${#DO_MODULE[@]}" == 0 ]; then
print::warn "wrong usage. see --help"
usage
fi
for to_str in ${DO_MODULE[@]}; do
is_good=0
for from_str in "${MODULE[@]}"; do
if [ "${to_str}" == "${from_str}" ]; then
is_good=1
break
fi
done
if [ "${is_good}" != 1 ]; then
print::warn "wrong ${GRE}part name${RST} ${RED}${to_str}${RST}"
usage
exit 1
fi
done
}
# pars args, keep parsing even if launch modules
## if -f, --force set FORCE_INSTALL=1
## if -n, --no-color disable_color
## if -h, --help help
## if -p, --part set DO_MODULE=part1 ... partn
## -p && -a false
## -f|-n && -p | --part | -all true
function parse::arg()
{
HAVE_PART=0
DO_MODULE=()
# is launched by module
[ "${0}" == "${1}" ] && shift 1
while [ "${1:-}" != "" ]; do
case "${1}" in
-f|--force)
export FORCE_MOVE=1
;;
-h|--help)
usage
exit
;;
-p|--part)
parse::short ${*}
shift $?
DO_MODULE=${PARSED_ARG[*]}
HAVE_PART=1
;;
-n|--no-color)
NO_ANSI=1
ansi::toggle_color
;;
-u|--update)
dot::update
exit
;;
-l|--no-log)
DONT_PRINT=1
;;
*)
print::warn "wrong options ${RED}[${RST}${1}${RED}]${RST}"
# usage
exit
;;
esac
shift
done
parse::post_parsing
}
# install lib_bash
DIR_LIB_BASH="${HOME}/.local/lib/lib_bash"
[ ! -d "${DIR_LIB_BASH}" ] && \
bash -c "$(curl -sL https://bit.ly/lib_bash)"
# source .config
source "${DIR_LIB_BASH}/.config"
# test if work, in this case update lib_bash
# cli::update
# source .config
PATH_CONFIG=$(realpath ${BASH_SOURCE[0]})
DIR_BASE=${PATH_CONFIG%/*}
source "${DIR_BASE}/.config"
parse::arg ${*}
log::main_title "${GRE}DOT FILES${RST}" 1 "${RED}" 1
# if not sourced, BASH_SOURCE keep current file when $0 keep launched file
[ "${BASH_SOURCE[0]}" -ef "${0}" ] && modules::install
LOG_PAD_STR="${LOG_PAD_STR_BAK}"
exit
function menu::set::title()
{
TO_RENDER_TITLE="\x1b[0;0HTITLE v1.0.0"
}
function menu::set::date()
{
printf -v TO_RENDER_DATE '%(%d-%m-%Y %H:%M:%S)T' -1
local pos=$((SCREEN_SIZE[1] - ${#TO_RENDER_DATE} + 1))
TO_RENDER_DATE="\x1b[0;${pos}H${TO_RENDER_DATE}\n\n"
}
function menu::set::item()
{
local i=1
TO_RENDER_ITEM="Choose a module to run in the list below and press enter:\n\n"
for mods in ${MODULE[@]}; do
if [ "${i}" == "${SEL_ID}" ]; then
TO_RENDER_ITEM+="- ${SEL_FMT_BEG}${mods}${SEL_FMT_END}\n"
else
TO_RENDER_ITEM+="- ${mods}\n"
fi
((i++))
done
}
SCREEN_SIZE=($(echo -e "lines\ncols"|tput -S))
SEL_ID=1
SEL_MAX=
SEL_FMT_BEG="\x1b[3m\x1b[5m\x1b[4m\x1b[7m\x1b[1;32m"
SEL_FMT_END="\x1b[0m"
menu::set::title
function render::menu()
{
printf "${CLR_SCR}"
menu::set::date
menu::set::item
TO_RENDER="${TO_RENDER_TITLE}${TO_RENDER_DATE}${TO_RENDER_ITEM}"
printf "%b" "${TO_RENDER}"
}
function post_key_press()
{
if [ "${KEY_PRESS}" == "UP" -a ${SEL_ID} -gt 1 ]; then
((SEL_ID--))
elif [ "${KEY_PRESS}" == "DOWN" -a ${SEL_ID} -lt ${#MODULE[@]} ]; then
((SEL_ID++))
fi
}
# https://abs.traduc.org/abs-fr/ch05s02.html (chapter 5.3)
function key_press()
{
unset T1 T2 T3 KEY_PRESS
read -s -N1 T1
read -s -N2 -t 0.001 T2
read -s -N1 -t 0.001 T3
local mode="$T1$T2$T3"
case "${mode}" in
$'\x1b\x5b\x41') KEY_PRESS="UP" ;; # UP ARROW
$'\x77') KEY_PRESS="UP" ;; # 'w'
$'\x1b\x5b\x42') KEY_PRESS="DOWN" ;; # DOWN ARROW
$'\x73') KEY_PRESS="DOWN" ;; # 's'
$'\x1b\x5b\x43') KEY_PRESS="LEFT" ;; # LEFT ARROW
$'\x61') KEY_PRESS="LEFT" ;; # 's'
$'\x1b\x5b\x44') KEY_PRESS="RIGHT" ;; # RIGHT ARROW
$'\x64') KEY_PRESS="RIGHT" ;; # 'd'
$'\x0a') KEY_PRESS="ENTER" ;; # ENTER
*)
printf "UNKNOWN KEY 0x$(printf '%b' ${mode} | xxd -ps)\n"
KEY_PRESS="UNKNOWN"
;;
esac
post_key_press
render::menu
printf "KEY ${KEY_PRESS} PRESSED\n"
}
render::menu
# keypress
while key_press; do :; done