-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliaser
executable file
·284 lines (236 loc) · 6.35 KB
/
aliaser
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
aliaser() {
IFS=$'\n\t'
helpp () {
cat <<EOF
aliaser <option> [alias name]
options:
-h|help display this help message
-o|open open the alias file with the default gui editor (e.g. TextEdit)
-l|list list aliases saved in alias file
-e|edit edit alias file in $EDITOR
-r|rm remove alias from alias file
-d|dir create an alias from the current directory (alias name is basename)
-n|name create an alias from the current directory with a user defined name
-s|search search alias file and execute selection
-a|searchall search all aliases system wide and execute selection
-c|command create an alias from the previous command with a user defined name
examples:
aliaser rm "aliasname" remove alias named "aliasname" from alias file
aliaser -n "favoritedir" add an alias for the current directory named
"favoritedir" to alias file
be sure to source the alias file in your .bashrc or .bash_profile
EOF
}
listbox() {
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-o|--options)
local OIFS=$IFS;
IFS="|";
if [ -n "$ZSH_VERSION" ]; then
IFS=$'\n' opts=("$(echo "$2" | tr "|" "\n")")
else
IFS="|" read -r -a opts <<< "$2";
fi
IFS=$OIFS;
shift
;;
-t|--title)
local title="$2"
shift
;;
-r|--result)
local __result="$2"
shift
;;
-a|--arrow)
local arrow="$2"
shift
;;
*)
esac
shift
done
if [[ -z $arrow ]]; then
arrow=">"
fi
local len=${#opts[@]}
local choice=0
local titleLen=${#title}
if [[ -n "$title" ]]; then
echo -e "\n $title"
printf " "
printf %"$titleLen"s | tr " " "-"
echo ""
fi
draw() {
local idx=0
for it in "${opts[@]}"
do
local str="";
if [ $idx -eq $choice ]; then
str+="$arrow "
else
str+=" "
fi
echo "$str$it"
idx=$((idx+1))
done
}
move() {
for it in "${opts[@]}"
do
tput cuu1
done
tput el1
}
listen() {
while true
do
key=$(bash -c "read -n 1 -s key; echo \$key")
if [[ $key = q ]]; then
break
elif [[ $key = B ]]; then
if [ $choice -lt $((len-1)) ]; then
choice=$((choice+1))
move
draw
fi
elif [[ $key = A ]]; then
if [ $choice -gt 0 ]; then
choice=$((choice-1))
move
draw
fi
elif [[ $key = "" ]]; then
if [ -n "$ZSH_VERSION" ]; then
choice=$((choice+1))
fi
if [[ -n $__result ]]; then
eval "$__result=\"${opts[$choice]}\""
else
echo -e "\n${opts[$choice]}"
fi
break
fi
done
}
draw
listen
}
config="${HOME}/.aliaser/aliaser.conf"
if [[ ! -f "$config" ]]; then
read -r -p "Enter the path to your alias file: " aliasfile
echo "alias_file=$aliasfile" > "$config"
echo "$aliasfile set as alias file."
fi
file=$(awk -F '=' '{print $2}' "$config")
filedir=$(dirname "$file")
filename=$(basename "$file")
_open() {
if [[ $(uname -s) == "Linux" ]]; then
open="$(command -v xdg-open)";
else
open="$(command -v open)";
fi
find "$filedir/" -type f -name "$filename" -exec "$open" {} +
echo "Opening $file"
}
_list() {
echo "Printing aliases"
sleep .2
find "$filedir/" -type f -name "$filename" -exec cat {} +;
}
_edit() {
find "$filedir/" -type f -name "$filename" -exec "$EDITOR" {} +
}
_remove() {
grep -iv "$2" "$file" > .arm.tmp
cat .arm.tmp > "$file"
rm .arm.tmp
unalias "$2"
echo "Removed $2 from aliases"
}
_dir() {
local name
local dir
name=$(basename "$(pwd)")
dir=$(pwd|sed 's/ /\\ /')
printf "alias %s='cd %s'" "${name}" "${dir}" >> "$file";
printf '\n' >> "$file"
echo "Alias for $dir added to $file"
}
_named() {
echo "alias $2='cd $(pwd)'" >> "$file"
printf '\n' >> "$file"
echo "Alias for $2 created for $(pwd)"
}
_search() {
if [[ "$1" == "-a" ]] || [[ "$1" == "searchall" ]]; then
unset file;
alias > ~/.aliaser/aliaserall.tmp
file=~/.aliaser/aliaserall.tmp
fi
if [[ "$2" == "" ]]; then
printf "aliaser search needs an alias name to search for";
else
local srch="$2"
listresults() {
echo "Search results for \"$srch\":"
echo "-------------------------------"
grep -i "$srch" $file
echo
}
choosefromlist() {
results() {
grep -i "$srch" "$file" | sed "s/\#.*//g" | \
grep -v '^$' | tr '\n' '\|'
}
if [[ $(command -v fzf) ]]; then
comm="$(results "$@" | tr '\|' '\n' | fzf)"
else
listbox -t "Search results for \"$srch\"" \
-o "$(results "$@")CANCEL SEARCH" -r comm -a "⇨"
fi
local toexec; toexec=$(
echo "$comm" | \
awk -F '=' '{print $2}' | \
sed "s/'//g" | \
sed "s|~|/Users/$(whoami)|g"
);
local trigger; trigger="$(echo "$toexec" | awk -F ' ' '{print $1}')"
local directory; directory="${toexec//cd /}"
if [[ $trigger == "cd" ]]; then
{ pushd "${directory}" > /dev/null 2>&1; } || \
echo "unable to navigate to ${directory}"
else
eval "$toexec"
fi
}
choosefromlist "$@"
fi
}
_command() {
_cralias=$(
history | tail -n 2 | sort -r | tail -n 1 | awk '{first=$1; $1=""; print $0; }' | sed 's/^ //g'
)
[[ $(printf _cralias) =~ \' ]] && \
echo "alias $2=\"$_cralias\"" >> "$file" || \
echo "alias $2='$_cralias'" >> "$file"
echo "alias $2 created for \"$_cralias\""
}
case "$1" in
-h|--help|help|"") helpp ;;
-o|open) _open ;;
-l|ls|list) _list ;;
-e|edit) _edit ;;
-r|rm|remove) _remove "$@" ;;
-d|wd|dir) _dir ;;
-n|name) _named "$@" ;;
-s|search) _search "$@" ;;
-a|searchall) _search "$@" ;;
-c|command) _command "$@" ;;
esac
eval <(file)
}