Skip to content

Instantly share code, notes, and snippets.

@erichiller
Last active November 27, 2023 14:03
Show Gist options
  • Save erichiller/03f2f3315cc54c3a863b8528b4113fa4 to your computer and use it in GitHub Desktop.
Save erichiller/03f2f3315cc54c3a863b8528b4113fa4 to your computer and use it in GitHub Desktop.
Bash Completions should go into `~/.bash_completion`
# dotnet suggest shell start
#$env:PATH+=":~/.dotnet/tools";
if (Get-Command "dotnet-suggest" -errorAction SilentlyContinue)
{
$availableToComplete = (dotnet-suggest list) | Out-String
$availableToCompleteArray = $availableToComplete.Split([Environment]::NewLine, [System.StringSplitOptions]::RemoveEmptyEntries) | Where-Object { $_ -ne "dotnet" }
Register-ArgumentCompleter -Native -CommandName $availableToCompleteArray -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
$fullpath = (Get-Command $commandAst.CommandElements[0]).Source
$arguments = $commandAst.Extent.ToString().Replace('"', '\"')
dotnet-suggest get -e $fullpath --position $cursorPosition -- "$arguments" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
}
else
{
"Unable to provide System.CommandLine tab completion support unless the [dotnet-suggest] tool is first installed."
"See the following for tool installation: https://www.nuget.org/packages/dotnet-suggest"
}
$env:DOTNET_SUGGEST_SCRIPT_VERSION = "1.0.2"
# dotnet suggest script end
# dotnet cli tool shell complete script start
_dotnet_bash_complete()
{
local word=${COMP_WORDS[COMP_CWORD]} ;
local completions ;
completions="$(dotnet complete --position "${COMP_POINT}" "${COMP_LINE}" 2>/dev/null)" ;
if [ $? -ne 0 ]; then
completions=""
fi ;
local only_options=true;
IFS=$'\n' completions_array=($completions);
for i in "${completions_array[@]}"; do if [[ $i =~ ^[^/-] ]]; then
only_options=false;
break;
fi; done
if [[ $only_options == true ]]; then
dotnet_completions=( $(compgen -W "$completions" -- "$word") ) ;
file_completions=( $(compgen -o filenames -f -- "$word" ) ) ;
COMPREPLY=(${dotnet_completions[@]} ${file_completions[@]}) ;
else
COMPREPLY=( $(compgen -W "$completions" -- "$word") ) ;
fi;
}
complete -o nosort -F _dotnet_bash_complete dotnet;
# dotnet cli tool shell complete script end
# dotnet suggest shell complete script start
_dotnet_suggest_bash_complete()
{
local fullpath=`type -p ${COMP_WORDS[0]}`
local escaped_comp_line=$(echo "$COMP_LINE" | sed s/\"/'\\\"'/g)
local completions=`~/.dotnet/tools/dotnet-suggest get --executable "${fullpath}" --position ${COMP_POINT} -- "${escaped_comp_line}"`
if [ "${#COMP_WORDS[@]}" != "2" ]; then
return
fi
local IFS=$'\n'
local suggestions=($(compgen -W "$completions"))
if [ "${#suggestions[@]}" == "1" ]; then
local number="${suggestions[0]/%\ */}"
COMPREPLY=("$number")
else
for i in "${!suggestions[@]}"; do
suggestions[$i]="$(printf '%*s' "-$COLUMNS" "${suggestions[$i]}")"
done
COMPREPLY=("${suggestions[@]}")
fi
}
_dotnet_suggest_bash_register_complete()
{
local IFS=$'\n' # should this be removed so that all the completions aren't on a single line?
complete -F _dotnet_suggest_bash_complete `~/.dotnet/tools/dotnet-suggest list`
}
_dotnet_suggest_bash_register_complete
export DOTNET_SUGGEST_SCRIPT_VERSION="1.0.1"
# dotnet suggest shell complete script end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment