This repository was archived by the owner on Mar 3, 2020. It is now read-only.
forked from holman/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot
More file actions
executable file
·153 lines (130 loc) · 3.47 KB
/
Copy pathdot
File metadata and controls
executable file
·153 lines (130 loc) · 3.47 KB
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
#!/usr/bin/env bash
# vim: filetype=sh
set -euo pipefail
IFS=$' \n\t'
export DOTFILES="${DOTFILES:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null && pwd)}"
source "$DOTFILES/scripts/core/main.sh"
CALLING_DIR="${CALLING_DIR:-"$PWD"}"
##? A hub for an all-purpose set of scripts
#?? 1.0.0
##?
##? Usage:
##? dot
##? dot <context>
##? dot <context> <cmd> [<args>...]
list_command_paths() {
find "$DOTFILES/scripts" -maxdepth 2 -executable -type f \
| grep -v core \
| sort
}
list_contexts() {
local paths="$1"
echo "$paths" \
| xargs -I % -d '\n' sh -c 'basename $(dirname %)' \
| uniq
}
list_paths_for_context() {
local paths="$1"; local context="$2"
echo "$paths" \
| grep "$context/"
}
list_commands_for_context() {
local paths="$1"; local context="$2"
echo "$paths" \
| grep "$context/" \
| xargs -I % -d '\n' basename "%"
}
exact_match() {
local readonly query="$1"
local readonly matches="$2"
for m in $matches; do
if [[ "$m" = "$query" ]]; then
return 0
fi
done
exit 1
}
find_match() {
local candidates="$1"
local query="$2"
local matches="$(echo "$candidates" | grep $query)"
local count="$(echo "$matches" | wc -l)"
local exact="$(exact_match "$query" "$matches" || echo "")"
if [[ ! -z "${exact}" ]]; then
RESPONSE="$query"
elif [[ -z "${matches}" ]]; then
echo -e "No matches for $query\n\nPossible values:\n$candidates"
exit 1
elif [ $count -gt 1 ]; then
echo -e "Multiple matches for $query:\n$matches"
exit 2
else
RESPONSE=$(echo "$matches" | head -n 1)
fi
}
find_context() {
local contexts="$1"; local query="$2"
find_match "$contexts" "$query"
CTX=$RESPONSE
}
find_command() {
local paths="$1"; local context="$2"; local query="$3"
local commands=$(list_commands_for_context "$paths" "$context")
find_match "$commands" "$query"
CMD=$(basename $RESPONSE)
}
fzf_prompt() {
local paths="$1"
match="$(echo "$paths" \
| xargs -I % sh -c 'echo "$(basename $(dirname %)) $(basename %)"' \
| fzf-tmux --height 50% --preview 'dot $(echo {} | cut -d" " -f 1) $(echo {} | cut -d" " -f 2) -h')"
printf "$match "
read args
if coll::is_empty "$args"; then
dot $match
else
dot $match "$args"
fi
}
_alias() {
local readonly arg="$1"
case "$arg" in
and) echo android;;
clj) echo clojure;;
game) echo gaming;;
js) echo javascript;;
json) echo javascript;;
env) echo environment;;
self) echo environment;;
mac) echo osx;;
net) echo network;;
pkg) echo package;;
sec) echo security;;
sh) echo shell;;
fs) echo filesystem;;
sensor) echo sensors;;
not) echo notification;;
*) echo "$arg";;
esac
}
if [[ $# -eq 0 ]]; then
# Shows available contexts
# or prompts for a command if fzf is installed
platform::command_exists fzf \
&& fzf_prompt "$(list_command_paths)" \
|| list_contexts "$(list_command_paths)"
elif [[ $# -eq 1 ]]; then
# Shows available commands for a given context
context_query="$(_alias $1)"
paths="$(list_command_paths)"
contexts="$(list_contexts "$paths")"
find_context "$contexts" "$context_query"
list_paths_for_context "$paths" "$CTX" | xargs -I % -d '\n' basename "%"
else
# Executes a command directly
ctx="$(_alias $1)"
cmd="$2"
shift 2
export DOT_TRACE=${TRACE:-false}
"${DOTFILES}/scripts/${ctx}/${cmd}" "$@"
fi