Created
February 7, 2021 15:37
-
-
Save malys/7309fffba093067987b32b89f4bf1224 to your computer and use it in GitHub Desktop.
[Nyagos + Bash ] Include bash completion in nyagos #nyagos #bast #completion #windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Author: Brian Beffa <brbsix@gmail.com> | |
# Original source: https://brbsix.github.io/2015/11/29/accessing-tab-completion-programmatically-in-bash/ | |
# License: LGPLv3 (http://www.gnu.org/licenses/lgpl-3.0.txt) | |
# | |
get_completions(){ | |
local completion COMP_CWORD COMP_LINE COMP_POINT COMP_WORDS COMPREPLY=() | |
# load bash-completion if necessary | |
# declare -F _completion_loader &>/dev/null || { | |
# source /usr/share/bash-completion/bash_completion | |
# } | |
COMP_LINE=$* | |
COMP_POINT=${#COMP_LINE} | |
eval set -- "$@" | |
COMP_WORDS=("$@") | |
# add '' to COMP_WORDS if the last character of the command line is a space | |
[[ ${COMP_LINE[@]: -1} = ' ' ]] && COMP_WORDS+=('') | |
# index of the last word | |
COMP_CWORD=$(( ${#COMP_WORDS[@]} - 1 )) | |
# determine completion function | |
completion=$(complete -p "$1" 2>/dev/null | awk '{print $(NF-1)}') | |
# run _completion_loader only if necessary | |
[[ -n $completion ]] || { | |
# load completion | |
_completion_loader "$1" | |
# detect completion | |
completion=$(complete -p "$1" 2>/dev/null | awk '{print $(NF-1)}') | |
} | |
# ensure completion was detected | |
[[ -n $completion ]] || return 1 | |
# execute completion function | |
"$completion" | |
# print completions to stdout | |
printf '%s\n' "${COMPREPLY[@]}" | LC_ALL=C sort | |
} | |
source <(/b/prog/kompose.exe completion bash) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nyagos.complete_for["git"] = function(args) | |
if #args == 2 then | |
hghelp= nyagos.eval('bash -c -i "source ~/.bashrc;get_completions \'' .. table.concat(args, " ") .. ' \'"',"r") | |
local hgcmds={} | |
for line in string.gmatch(hghelp,"[^\n]+") do | |
for word in string.gmatch(line,"[a-z]+") do | |
hgcmds[#hgcmds+1] = word | |
end | |
end | |
return hgcmds | |
else | |
return nil -- files completion | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment