-
Notifications
You must be signed in to change notification settings - Fork 8
/
autocomplete.sh
146 lines (125 loc) · 3.18 KB
/
autocomplete.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/sh
{ # this ensures the entire script is downloaded #
is_installed() {
type "$1" > /dev/null 2>&1
}
error_exit() {
if is_installed tput; then
tput sgr0
tput setaf 1
echo "ERROR: $1"
tput sgr0
else
echo "ERROR: $1"
fi
exit 1
}
success_msg() {
if is_installed tput; then
command printf "["
tput sgr0
tput setaf 2
command printf "OK"
tput sgr0
command printf "] ${1}\\n"
else
echo "[OK]: $1"
fi
}
warning_msg() {
if is_installed tput; then
command printf "["
tput sgr0
tput setaf 3
command printf "!!"
tput sgr0
command printf "] ${1}\\n"
else
echo "[!!]: $1"
fi
}
try_profile() {
if [ -z "${1-}" ] || [ ! -f "${1}" ]; then
return 1
fi
echo "${1}"
}
#
# Detect profile file if not specified as environment variable
# (eg: PROFILE=~/.myprofile)
# The echo'ed path is guaranteed to be an existing file
# Otherwise, an empty string is returned
#
detect_profile() {
if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
echo "${PROFILE}"
return
fi
local DETECTED_PROFILE
DETECTED_PROFILE=''
local SHELLTYPE
SHELLTYPE="$(basename "/$SHELL")"
if [ "$SHELLTYPE" = "bash" ]; then
if [ -f "$HOME/.bash_profile" ]; then
DETECTED_PROFILE="$HOME/.bash_profile"
elif [ -f "$HOME/.bashrc" ]; then
DETECTED_PROFILE="$HOME/.bashrc"
fi
elif [ "$SHELLTYPE" = "zsh" ]; then
DETECTED_PROFILE="$HOME/.zshrc"
fi
if [ -z "$DETECTED_PROFILE" ]; then
for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zshrc"
do
if DETECTED_PROFILE="$(try_profile "${HOME}/${EACH_PROFILE}")"; then
break
fi
done
fi
if [ ! -z "$DETECTED_PROFILE" ]; then
echo "$DETECTED_PROFILE"
else
command touch $HOME/.bash_profile
echo "$HOME/.bash_profile"
fi
}
autocomplete() {
local DETECTED_PROFILE
AUTOCOMPLETE=''
local SHELLTYPE
SHELLTYPE="$(basename "/$SHELL")"
if [ "$SHELLTYPE" = "bash" ]; then
AUTOCOMPLETE=$(cat <<'EOF'
#! /bin/bash
export MANIFOLD_AUTOCOMPLETE=true
_manifold_bash_autocomplete() {
local cur opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
complete -F _manifold_bash_autocomplete manifold
EOF
)
fi
if [ ! -z "$AUTOCOMPLETE" ]; then
echo "$AUTOCOMPLETE"
fi
}
PROFILE=`detect_profile`
AUTOCOMPLETE=`autocomplete`
if [ -z "$MANIFOLD_DIR" ]; then
MANIFOLD_DIR="${HOME}/.manifold/bin"
fi
mkdir -p $MANIFOLD_DIR
if [ "$PROFILE" = "" ]; then
error_exit "Unable to locate profile settings file (something like $HOME/.bashrc or $HOME/.bash_profile)"
elif [ "$MANIFOLD_AUTOCOMPLETE" = "" ]; then
echo "$AUTOCOMPLETE" > "$MANIFOLD_DIR/.manifold_completion"
echo "source $MANIFOLD_DIR/.manifold_completion" >> $PROFILE
success_msg "Your $PROFILE has changed to manifold autocomplete"
warning_msg "Please restart or re-source your terminal session."
fi
}