Skip to content

Added ZSH Completion Shim Script #1643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/dotnet-suggest.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ On the machine where you'd like to enable completion, you'll need to do two thin

* For bash, add the contents of [dotnet-suggest-shim.bash](https://github.com/dotnet/command-line-api/blob/master/src/System.CommandLine.Suggest/dotnet-suggest-shim.bash) to `~/.bash_profile`.

* For zsh, add the contents of [dotnet-suggest-shim.zsh](https://github.com/dotnet/command-line-api/blob/master/src/System.CommandLine.Suggest/dotnet-suggest-shim.zsh) to `~/.zshrc`.

* For PowerShell, add the contents of [dotnet-suggest-shim.ps1](https://github.com/dotnet/command-line-api/blob/master/src/System.CommandLine.Suggest/dotnet-suggest-shim.ps1) to your PowerShell profile. You can find the expected path to your PowerShell profile by running the following in your console:

```console
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.CommandLine.Invocation;
using System.CommandLine.IO;
using System.CommandLine.Parsing;
using System.Threading.Tasks;
Expand Down Expand Up @@ -53,5 +52,15 @@ await _parser.InvokeAsync(

_console.Out.ToString().Should().Contain("Register-ArgumentCompleter");
}

[Fact]
public async Task It_should_print_zsh_shell_script()
{
await _parser.InvokeAsync(
"script zsh",
_console);

_console.Out.ToString().Should().Contain("_dotnet_zsh_complete()");
}
}
}
3 changes: 2 additions & 1 deletion src/System.CommandLine.Suggest/ShellType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace System.CommandLine.Suggest
public enum ShellType
{
Bash,
PowerShell
PowerShell,
Zsh
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public static void Handle(IConsole console, ShellType shellType)
case ShellType.PowerShell:
PrintToConsoleFrom(console, "dotnet-suggest-shim.ps1");
break;
case ShellType.Zsh:
PrintToConsoleFrom(console, "dotnet-suggest-shim.zsh");
break;
default:
throw new SuggestionShellScriptException($"Shell '{shellType}' is not supported.");
}
Expand Down
39 changes: 39 additions & 0 deletions src/System.CommandLine.Suggest/dotnet-suggest-shim.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# dotnet suggest shell complete script start
_dotnet_zsh_complete()
{
# debug lines, uncomment to get state variables passed to this function
# echo "\n\n\nstate:\t'$state'"
# echo "line:\t'$line'"
# echo "words:\t$words"

# Get full path to script because dotnet-suggest needs it
# NOTE: this requires a command registered with dotnet-suggest be
# on the PATH
full_path=`which ${words[1]}` # zsh arrays are 1-indexed
# Get the full line
# $words array when quoted like this gets expanded out into the full line
full_line="$words"

# Get the completion results, will be newline-delimited
completions=$(dotnet suggest get --executable "$full_path" -- "$full_line")
# explode the completions by linefeed instead of by spaces into the descriptions for the
# _values helper function.

exploded=(${(f)completions})
# for later - once we have descriptions from dotnet suggest, we can stitch them
# together like so:
# described=()
# for i in {1..$#exploded}; do
# argument="${exploded[$i]}"
# description="hello description $i"
# entry=($argument"["$description"]")
# described+=("$entry")
# done
_values 'suggestions' $exploded
}

# apply this function to each command the dotnet-suggest knows about
compdef _dotnet_zsh_complete $(dotnet-suggest list)

export DOTNET_SUGGEST_SCRIPT_VERSION="1.0.0"
# dotnet suggest shell complete script end
4 changes: 4 additions & 0 deletions src/System.CommandLine.Suggest/dotnet-suggest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<Content Include="dotnet-suggest-shim.bash">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Remove="dotnet-suggest-shim.zsh" />
<Content Include="dotnet-suggest-shim.zsh">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<!-- Workaround https://github.com/dotnet/arcade/issues/2233 -->
Expand Down