-
Notifications
You must be signed in to change notification settings - Fork 1
/
completions.sh
executable file
·98 lines (87 loc) · 2.6 KB
/
completions.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
# Completions
comp_complete() {
# We sadly need it to interpret backslashes for directory names
line_array=($READLINE_LINE)
line_array=( "${line_array[@]// /\\ }" )
local comp_array=( )
if [[ "${#line_array[@]}" -gt 1 ]]
then
case "${line_array[-1]}" in
'-'*)
man_completion "${line_array[0]}" "${line_array[-1]}"
readarray -t comp_array <<<"$return_args"
_color="$_option_color" ;;
*)
dir_suggest "${line_array[-1]}"
readarray -t comp_array <<<"$return_path"
_color="$_directory_color" ;;
esac
else
readarray -t comp_array <<<"$_commands"
_color="$_command_color"
fi
_post_prompt=""
for line in "${comp_array[@]}"
do
case "$line" in
"${line_array[-1]}"*)
line_array[-1]="$line"
_post_prompt="${line_array[*]}"
break ;;
esac
done
}
dir_suggest() {
local temp_path="$1"
local tilde_yes=false
local complete_path
local unfinish_path
local files
if [[ "$temp_path" == '~/'* ]]; then
tilde_yes=true
temp_path="${temp_path/~\//"$HOME"\/}"
elif [[ "$temp_path" == '/' ]]; then
temp_path='/'
fi
complete_path="${temp_path%/*}"
unfinish_path="${temp_path##*/}"
# If its a directory
if [ -d "${complete_path//\\ / }"/ ]; then
files=$(printf '%q\n' "${complete_path//\\ / }"/*/ "${complete_path//\\ / }"/* )
# If it isnt yet (current folder)
else
files=$(printf '%q\n' */ *)
fi
return_path=$(while IFS= read -r line; do if [[ "$line" == "$temp_path"* ]]; then printf '%s\n' "$line"; break; fi; done <<<"$files") # This is probably ass but grep is annoying bc of requiring regex escaping
if [ -d "${return_path//\\/}" ]; then
_color="$_directory_color"
else
_color="$_file_color"
fi
if [ "$tilde_yes" = true ]; then
return_path="${return_path/"$HOME"\//~\/}"
fi
}
man_completion() {
local man_string
local opt_string
man_string="$1"
opt_string="$2"
if [[ "${#opt_string}" -le 1 ]];
# Command length just makes sure it doesnt re-check every time, mega speed increase
then
if [[ "$OSTYPE" == *darwin* ]];
then
_man_args=$(man "$man_string" | col -bx | grep -F '-' | tr ' ' $'\n')
else
_man_args=$(man -Tascii "$man_string" | col -bx | grep -F '-' | tr ' ' $'\n' )
# This take 0.3 seconds each for the bash page, of which 0.013 is the sorting
# 0.190 IS RIDICULOUS, but also that bc bash's docs are 10,000 pages or smth
# -Tascii take this down by ~0.030 but even then its borderline unusable, all bc of pointless formatting bs
fi
_man_args=$(<<< "${_man_args//[^[:alpha:]]$'\n'/$'\n'}" grep -- '^-'| uniq)
_temp=''
_man_args=$(printf '%s' "$_man_args" | sed -e 's/[^[:alnum:]]$//g')
fi
return_args="$_man_args"
}