-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_completions.bash
74 lines (59 loc) · 1.77 KB
/
.bash_completions.bash
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
# Bring up history selection
__cmp_history() {
local cur="$2"
local prev="$3"
local selected_command=$(HISTTIMEFORMAT="" history | fzf --exact --tac --no-sort | sed 's/^[ ]*[0-9]*[ ]*//')
if [[ -n "$selected_command" ]]; then
COMPREPLY=( "$selected_command" )
else
COMPREPLY=()
fi
}
# Complete --target of cmake command
__cmp_cmake() {
local cur="$2"
local prev="$3"
if [[ "$prev" != "--target" ]]; then
return 1
fi
local currdir=${PWD##*/}
currdir=${currdir:-/}
# Scan the line for build directory
local build_dir="build"
if [[ "$currdir" == "build" ]]; then
build_dir="."
fi
for (( i=0; i < ${#COMP_WORDS[@]}; i++)); do
if [[ "${COMP_WORDS[i]}" == "--build" ]]; then
if [[ $((i + 1)) -lt ${#COMP_WORDS[@]} ]]; then
build_dir="${COMP_WORDS[i + 1]}"
fi
break
fi
done
local target=$(cmake --build "$build_dir" --target help 2>/dev/null | awk '/^[a-zA-Z0-9_-]+:/ {print $1}' | sed 's/://' | fzf --query "$cur")
if [[ -n "$target" ]]; then
COMPREPLY=( "$target" )
else
COMPREPLY=()
fi
}
__cmp_exec() {
local curr="$2"
case "$curr" in
./*)
local selected=$(find "." ! -type d -executable | fzf --query "$curr")
if [[ -n "$selected" ]]; then
COMPREPLY=( "$selected" )
else
COMPREPLY=()
fi
esac
}
# The -I specifier can only be used from bash 5.2 (https://stackoverflow.com/questions/79025685/bash-complete-i-invalid-option/79025820)
if [ "${BASH_VERSINFO[0]}" -ge 5 ] && [ "${BASH_VERSINFO[1]}" -ge 0 ]; then
complete -I -F __cmp_exec -o bashdefault
fi
complete -o nospace -F __cmp_cmake cmake
complete -F __cmp_history -E
complete -F __cmp_exec bash