forked from denisidoro/navi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.sh
125 lines (105 loc) · 2.9 KB
/
main.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
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
#!/usr/bin/env bash
if ${NAVI_FORCE_GNU:-false} && [ -n "${DOTFILES:-}" ]; then
source "${DOTFILES}/scripts/core/main.sh"
fi
source "${SCRIPT_DIR}/src/arg.sh"
source "${SCRIPT_DIR}/src/cheat.sh"
source "${SCRIPT_DIR}/src/coll.sh"
source "${SCRIPT_DIR}/src/dict.sh"
source "${SCRIPT_DIR}/src/health.sh"
source "${SCRIPT_DIR}/src/misc.sh"
source "${SCRIPT_DIR}/src/opts.sh"
source "${SCRIPT_DIR}/src/search.sh"
source "${SCRIPT_DIR}/src/selection.sh"
source "${SCRIPT_DIR}/src/str.sh"
source "${SCRIPT_DIR}/src/ui.sh"
handler::main() {
local -r cheats="$(cheat::memoized_read_all)"
if [ -z "${NAVI_CACHE:-}" ]; then
export NAVI_CACHE="$cheats"
fi
local -r selection="$(ui::select "$cheats")"
local -r cheat="$(cheat::from_selection "$cheats" "$selection")"
[ -z "$cheat" ] && exit 67
local -r interpolation="$(dict::get "$OPTIONS" interpolation)"
local cmd="$(selection::cmd "$selection" "$cheat")"
local arg value
while $interpolation; do
arg="$(echo "$cmd" | arg::next || echo "")"
if [ -z "$arg" ]; then
break
fi
value="$(arg::pick "$arg" "$cheat" || echo "")"
if [ -z "$value" ]; then
echoerr "Unable to fetch suggestions for '$arg'!"
exit 0
fi
eval "local $arg"='$value'
cmd="$(echo "$cmd" | arg::interpolate "$arg" "$value")"
done
local -r unresolved_arg="$(echo "$cmd" | arg::next || echo "")"
local -r print="$(dict::get "$OPTIONS" print)"
if $print || [ -n "$unresolved_arg" ]; then
echo "$cmd"
else
eval "$cmd"
fi
}
handler::preview() {
local -r query="$1"
local -r selection="$(echo "$query" | selection::dict)"
local -r cheats="$(cheat::memoized_read_all)"
local -r cheat="$(cheat::from_selection "$cheats" "$selection")"
[ -n "$cheat" ] && selection::cmd "$selection" "$cheat"
}
handler::help() {
echo "$TEXT"
}
handler::version() {
local -r full="${1:-false}"
echo "${VERSION:-unknown}"
if $full; then
source "${SCRIPT_DIR}/src/version.sh"
version::code 2>/dev/null || echo "unknown code"
fi
}
handler::script() {
"${SCRIPT_DIR}/scripts/${SCRIPT_ARGS[@]}"
}
handler::home() {
echo "${SCRIPT_DIR}"
}
main() {
case "$(dict::get "$OPTIONS" entry_point)" in
preview)
local -r query="$(dict::get "$OPTIONS" query)"
handler::preview "$query" \
|| echo "Unable to find command for '$query'"
;;
search)
health::fzf
local -r query="$(dict::get "$OPTIONS" query)"
search::save "$query" || true
handler::main
;;
version)
handler::version false
;;
full-version)
handler::version true
;;
home)
handler::home
;;
script)
handler::script
;;
help)
handler::help
;;
*)
health::fzf
handler::main
;;
esac
}