forked from denisidoro/navi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arg.sh
92 lines (75 loc) · 2.08 KB
/
arg.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
#!/usr/bin/env bash
ARG_REGEX="<[a-zA-Z_]+([- ]?\w+)*>"
arg::dict() {
local -r input="$(cat)"
local -r fn="$(echo "$input" | awk -F'---' '{print $1}')"
local -r opts="$(echo "$input" | awk -F'---' '{print $2}')"
dict::new fn "$fn" opts "$opts"
}
arg::escape() {
echo "$*" \
| tr '-' '_' \
| tr ' ' '_'
}
arg::interpolate() {
local -r arg="$1"
local -r value="$2"
local -r words="$(echo "$value" | wc -w | xargs)"
if [[ $words > 1 ]]; then
sed "s|<${arg}>|\"${value}\"|g"
else
sed "s|<${arg}>|${value}|g"
fi
}
arg::next() {
grep -Eo "$ARG_REGEX" \
| head -n1 \
| tr -d '<' \
| tr -d '>'
}
arg::deserialize() {
local arg="$1"
arg="${arg:1:${#arg}-2}"
echo "$arg" \
| tr "${ESCAPE_CHAR}" " " \
| tr "${ESCAPE_CHAR_2}" "'" \
| tr "${ESCAPE_CHAR_3}" '"'
}
arg::serialize_code() {
printf "tr ' ' '${ESCAPE_CHAR}'"
printf " | "
printf "tr \"'\" '${ESCAPE_CHAR_2}'"
printf " | "
printf "tr '\"' '${ESCAPE_CHAR_3}'"
}
arg::pick() {
local -r arg="$1"
local -r cheat="$2"
local -r prefix="$ ${arg}:"
local -r length="$(echo "$prefix" | str::length)"
local -r arg_dict="$(echo "$cheat" | grep "$prefix" | str::sub $((length + 1)) | arg::dict)"
local -r fn="$(dict::get "$arg_dict" fn)"
local -r args_str="$(dict::get "$arg_dict" opts)"
local arg_name=""
for arg_str in $args_str; do
if [ -z $arg_name ]; then
arg_name="$(echo "$arg_str" | str::sub 2)"
else
eval "local $arg_name"='$arg_str'
arg_name=""
fi
done
if [ -n "$fn" ]; then
local suggestions="$(eval "$fn" 2>/dev/null)"
if [ -n "$suggestions" ]; then
echo "$suggestions" | ui::fzf --prompt "$arg: " --header-lines "${headers:-0}" | str::column "${column:-}"
fi
elif ${NAVI_USE_FZF_ALL_INPUTS:-false}; then
echo "" | ui::fzf --prompt "$arg: " --print-query --no-select-1 --height 1
else
printf "\033[0;36m${arg}:\033[0;0m " > /dev/tty
read -r value
ui::clear_previous_line > /dev/tty
printf "$value"
fi
}