-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
aliasme.sh
135 lines (121 loc) · 2.44 KB
/
aliasme.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
#!/usr/bin/env bash
_list() {
if [ -s ~/.aliasme/cmd ];then
while read name
do
read value
echo "$name : $value"
done < ~/.aliasme/cmd
fi
}
_add() {
#read name
name=$1
if [ -z $1 ]; then
read -ep "Input name to add:" name
fi
#read path
cmd="$2"
if [ -z "$2" ]; then
read -ep "Input cmd to add:" cmd
fi
echo $name >> ~/.aliasme/cmd
echo $cmd >> ~/.aliasme/cmd
echo "add: $name -> $cmd"
_autocomplete
}
_remove() {
#read name
name=$1
if [ -z $1 ]; then
read -pr "Input name to remove:" name
fi
# read and replace file
if [ -s ~/.aliasme/cmd ];then
touch ~/.aliasme/cmdtemp
while read line
do
if [ "$line" = "$name" ]; then
read line #skip one more line
echo "remove $name"
else
echo $line >> ~/.aliasme/cmdtemp
fi
done < ~/.aliasme/cmd
mv ~/.aliasme/cmdtemp ~/.aliasme/cmd
fi
_autocomplete
}
_excute() {
if [ -s ~/.aliasme/cmd ];then
while read -u9 line; do
if [ "$1" = "$line" ]; then
read -u9 line
eval $line
return 0
fi
done 9< ~/.aliasme/cmd
fi
return 1
}
_bashauto()
{
local cur opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
opts=""
if [ -s ~/.aliasme/cmd ];then
while read line
do
opts+=" $line"
read line
done < ~/.aliasme/cmd
fi
COMPREPLY=( $(compgen -W "${opts}" ${cur}) )
return 0
}
_autocomplete()
{
if [ $ZSH_VERSION ]; then
# zsh
opts=""
if [ -s ~/.aliasme/cmd ];then
while read line
do
opts+="$line "
read line
done < ~/.aliasme/cmd
fi
compctl -k "($opts)" al
else
# bash
complete -F _bashauto al
fi
}
_autocomplete
al(){
if [ ! -z $1 ]; then
if [ $1 = "ls" ]; then
_list
elif [ $1 = "add" ]; then
_add $2 "$3"
elif [ $1 = "rm" ]; then
_remove $2
elif [ $1 = "-h" ]; then
echo "Usage:"
echo "al add [name] [command] # add alias command with name"
echo "al rm [name] # remove alias by name"
echo "al ls # alias list"
echo "al [name] # execute alias associate with [name]"
echo "al -v # version information"
echo "al -h # help"
elif [ $1 = "-v" ]; then
echo "aliasme 3.0.0"
echo "visit https://github.com/Jintin/aliasme for more information"
else
if ! _excute $1 ; then
echo "not found"
fi
fi
fi
}