-
Notifications
You must be signed in to change notification settings - Fork 0
/
listAliases.sh
executable file
·264 lines (238 loc) · 9.24 KB
/
listAliases.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
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
#!/bin/bash
# Function Description:
# Lists all aliases and functions defined in the ~/.Musalias/aliases file.
# Usage:
# listAliases - Lists aliases and functions with their descriptions.
# listAliases -v - Displays the entire aliases file with syntax highlighting.
# Reset OPTIND to ensure getopts works correctly when sourced
OPTIND=1
# Initialize flags
LISTALIASES_VERBOSE=false
LISTALIASES_MARKDOWN=false
# Parse options
while getopts ":v:m" opt; do
case $opt in
v )
LISTALIASES_VERBOSE=true
echo "Verbose mode set"
;;
m )
LISTALIASES_MARKDOWN=true
;;
\? )
echo "Invalid option: -$OPTARG" >&2
echo "Usage: listAliases [-v]"
exit 2
;;
esac
done
shift $((OPTIND -1))
if [ "$LISTALIASES_VERBOSE" = true ]; then
# Verbose mode: display the entire aliases file with syntax highlighting
if command -v highlight &> /dev/null; then
highlight -O ansi --syntax=sh "$HOME/.Musalias/aliases"
else
printf "Error: No syntax highlighter found. Install 'highlight' to use verbose mode. For Debian-based distros use:\n"
printf "sudo apt install highlight\n" >&2
printf "Would you like to install 'highlight' now? [Y/n]: "
read -r response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]] || [ -z "$response" ]; then
echo "Installing 'highlight'..."
sudo apt install highlight
else
echo "Installation aborted by the user."
fi
return 1
fi
elif [ "$LISTALIASES_MARKDOWN" = true ]; then
# Markdown mode: list aliases and functions with their comments
# Path to the aliases file
ALIASES_FILE="$HOME/.Musalias/aliases"
# Check if the aliases file exists
if [ ! -f "$ALIASES_FILE" ]; then
echo "Error: Aliases file '$ALIASES_FILE' does not exist." >&2
return 1
fi
# Extract aliases with their comments
awk '
# Define the function in awk to handle comments
function print_comment(comment, name) {
len = length($0);
spaces = "";
for (i = 1; i <= len; i++) {
spaces = spaces " ";
}
if (comment != "") {
options_index = index(comment, "Options:");
if (options_index > 0) {
# Print the part before "Options:" and add a newline
printf " -%s \n", substr(comment, 1, options_index - 1);
# Extract the options part
options_part = substr(comment, options_index);
# Split the options_part into words
n = split(options_part, words, /[ \t]+/);
# Print "Options:" on a new line
printf "%s\n", words[1];
# Process the rest of the words
for (i = 2; i <= n; i++) {
if (words[i] ~ /^-/) {
printf " - **%s**", words[i];
} else {
# Continue printing the description on the same line
printf "%s", words[i];
}
# Add a space after each word unless its the last word
if (i < n && words[i+1] !~ /^-/) {
printf " ";
} else {
# if last option, do not print newline
if (i != n) {
printf "\n";
}
}
}
} else {
# Standard printing for comments without "Options:"
printf " -%s", comment;
}
}
}
# Match titles that begin and end with two '#' symbols
/^##.*##$/ {
if ($0 !~ /^#+$/) { # Ensure the line is not just a series of '#'
if (match($0, /^##\s*(.*?)\s*##$/, arr)) {
printf "\n\n### %s", arr[1]; # Print the content between ## in bold
}
}
}
# Process comments and skip to the next line
/^#/ {
comment = substr($0, 2);
next;
}
# For aliases
/^alias/ {
sub(/^alias\s+/, "");
sub(/=.*/, "");
printf "\n- **%s**", $0; # Print the alias name in bold
if (comment != "") {
print_comment(comment, $0);
}
comment = "";
}
# For functions
/^[a-zA-Z_][a-zA-Z0-9_]*\s*\(\)\s*\{/ {
if (match($0, /^([a-zA-Z_][a-zA-Z0-9_]*)\s*\(\)\s*\{/, arr)) {
printf "\n- **%s**", arr[1]; # Print the function name in bold
}
if (comment != "") {
print_comment(comment, arr[1]);
}
comment = "";
}
' "$ALIASES_FILE"
echo
echo "
### Legend
Aliase marked with 👑 will call sudo"
echo
else
# Standard mode: list aliases and functions with their comments
echo "Hint: To output the entire aliases file with syntax highlighting, use the -v option."
echo "Available Aliases and Functions:"
echo "---------------------------------"
# Path to the aliases file
ALIASES_FILE="$HOME/.Musalias/aliases"
# Check if the aliases file exists
if [ ! -f "$ALIASES_FILE" ]; then
echo "Error: Aliases file '$ALIASES_FILE' does not exist." >&2
return 1
fi
# Extract aliases with their comments
awk '
# Define the function in awk to handle comments
function print_comment(comment, name) {
len = length($0);
spaces = "";
for (i = 1; i <= len; i++) {
spaces = spaces " ";
}
if (comment != "") {
options_index = index(comment, "Options:");
if (options_index > 0) {
# Print the part before "Options:" and add a newline
printf " -%s\n", substr(comment, 1, options_index - 1);
# Extract the options part
options_part = substr(comment, options_index);
# Split the options_part into words
n = split(options_part, words, /[ \t]+/);
# Print "Options:" on a new line
printf "%s %s", spaces, words[1]; # Should be "Options:"
# Process the rest of the words
for (i = 2; i <= n; i++) {
if (words[i] ~ /^-/) {
# If the word is an option (starts with -), print it on a new indented line except for the first option
if (i == 2) {
printf " %s", words[i];
} else {
printf "%s %s", spaces, words[i];
}
} else {
# Continue printing the description on the same line
printf "%s", words[i];
}
# Add a space after each word unless its the last word
if (i < n && words[i+1] !~ /^-/) {
printf " ";
} else {
# if last option, do not print newline
if (i != n) {
printf "\n";
}
}
}
} else {
# Standard printing for comments without "Options:"
printf " -%s", comment;
}
}
}
# Match titles that begin and end with two '#' symbols
/^##.*##$/ {
if ($0 !~ /^#+$/) { # Ensure the line is not just a series of '#'
if (match($0, /^##\s*(.*?)\s*##$/, arr)) {
printf "\n\n\033[1m>\033[0m %s", arr[1]; # Print the content between ## in bold
}
}
}
# Process comments and skip to the next line
/^#/ {
comment = substr($0, 2);
next;
}
# For aliases
/^alias/ {
sub(/^alias\s+/, "");
sub(/=.*/, "");
printf "\n \033[1m%s\033[0m", $0; # Print the alias name in bold
if (comment != "") {
print_comment(comment, $0);
}
comment = "";
}
# For functions
/^[a-zA-Z_][a-zA-Z0-9_]*\s*\(\)\s*\{/ {
if (match($0, /^([a-zA-Z_][a-zA-Z0-9_]*)\s*\(\)\s*\{/, arr)) {
printf "\n \033[1m%s\033[0m", arr[1]; # Print the function name in bold
}
if (comment != "") {
print_comment(comment, arr[1]);
}
comment = "";
}
' "$ALIASES_FILE"
echo
echo
echo "Aliase marked with 👑 will call sudo"
echo
fi