-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathaliasme.sh
More file actions
139 lines (124 loc) · 2.67 KB
/
aliasme.sh
File metadata and controls
139 lines (124 loc) · 2.67 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
#!/usr/bin/env bash
# Storage path
ALIASME_DIR="${ALIASME_DIR:-$HOME/.aliasme}"
ALIASME_CMD="$ALIASME_DIR/cmd"
_list() {
if [ -s "$ALIASME_CMD" ];then
while read -r name
do
if ! read -r value; then break; fi
echo "$name : $value"
done < "$ALIASME_CMD"
fi
}
_add() {
# Ensure directory exists
mkdir -p "$ALIASME_DIR"
name=$1
if [ -z "$1" ]; then
read -rep "Input name to add:" name
fi
cmd="$2"
if [ -z "$2" ]; then
read -rep "Input cmd to add:" cmd
fi
echo "$name" >> "$ALIASME_CMD"
echo "$cmd" >> "$ALIASME_CMD"
echo "add: $name -> $cmd"
_autocomplete
}
_remove() {
name=$1
if [ -z "$1" ]; then
read -pr "Input name to remove:" name
fi
if [ -s "$ALIASME_CMD" ];then
touch "$ALIASME_DIR/cmdtemp"
while read -r line
do
if [ "$line" = "$name" ]; then
read -r _ #skip one more line
echo "remove $name"
else
echo "$line" >> "$ALIASME_DIR/cmdtemp"
fi
done < "$ALIASME_CMD"
mv "$ALIASME_DIR/cmdtemp" "$ALIASME_CMD"
fi
_autocomplete
}
_excute() {
if [ -s "$ALIASME_CMD" ];then
while read -u9 -r line; do
if [ "$1" = "$line" ]; then
read -u9 -r 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 -r line
do
opts+=" $line"
read -r _
done < "$ALIASME_CMD"
fi
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
}
_autocomplete()
{
if [ -n "$ZSH_VERSION" ]; then
# zsh
opts=""
if [ -s "$ALIASME_CMD" ];then
while read -r line
do
opts+="$line "
read -r _
done < "$ALIASME_CMD"
fi
# shellcheck disable=SC2154
compctl -k "($opts)" al
else
# bash
complete -F _bashauto al
fi
}
_autocomplete
al(){
if [ -n "$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.1.0"
echo "visit https://github.com/Jintin/aliasme for more information"
else
if ! _excute "$1" ; then
echo "not found"
fi
fi
fi
}