Skip to content

Enable command line completion with complete command - #521

Merged
JohnMcPMS merged 12 commits into
microsoft:masterfrom
JohnMcPMS:complete
Aug 3, 2020
Merged

JohnMcPMS merged 12 commits into
microsoft:masterfrom
JohnMcPMS:complete

Conversation

@JohnMcPMS

@JohnMcPMS JohnMcPMS commented Jul 30, 2020 •

Copy link
Copy Markdown
Member

This year as part of Microsoft's Hackathon, I decided I wanted to explore how to implement a complete command following the pattern of the dotnet CLI's complete command, as referenced in #331 and #146.

Change

The goal of the change was to add the complete command, but it ended up having many side effects and becoming quite large. It may have also fixed a few bugs along the way. After I cover the complete command portion, I will detail the other side effects and bug fixes changes.

The MD file that I added to describe tab completion and the command can be seen here. The basic premise is that we take in the current command line, cursor position, and "word" to be completed.

From this we construct a CompletionData object, which breaks the arguments into our standard command line processing structures. These are segregated into before and after the word to complete. We then create a new Context object, attach the CompletionData and use the same parsing methods as standard inputs to determine which command is being targeted. Finally, we pass the Context to the new Command::Complete method.

The Complete method breaks down the remaining CompletionData to generate three different potential sets of options:

  1. A sub command :: The cursor is located just after a command and there are sub commands available.
  2. An argument specifier :: The cursor is not positioned after an argument specifier that expects a value, and there are arguments available.
  3. An argument value :: The cursor is positioned after an argument specifier that expects a value, or a positional argument is expected.

If an argument value is a potential option, a virtual function on the command is called to allow it to provide context sensitive results. It receives the Context created originally, which has had it's Args filled out during processing. Using this Context, existing Workflow functionality and paradigms can be leveraged to construct potential completions.

Examples

These examples assume that the tab completion in your shell works similar to PowerShell; repeated presses of tab (⇥) will result in cycling through the possible values.

Input Result Reason
winget ⇥ winget install install is the first command below the root
winget sh⇥ winget show show is the first command that starts with sh
winget source l⇥ winget source list list is the first sub-command of source that starts with l
winget -⇥ winget --version --version is the first argument defined for the root
winget install power⇥ winget install "Power Toys" "Power Toys" is the first package whose Id, Name, or Moniker starts with power
winget install "Power Toys" --version ⇥ winget install "Power Toys" --version 0.19.2 0.19.2 is the highest version of Power Toys at the time of writing

Other Changes

In addition to the complete command, these other changes are included:

  • Refactored the OutputStream out of Reporter, which should result in more consistent --no-vt behavior.
  • Added the concept of channels to Reporter, which enables us to select which type of output is allowed.
  • Refactored the argument parsing to enable re-use for completion.
  • Refactored Reporter to make it more of an instance; this meant making ConsoleModeRestore a singleton as it should have been.
  • Added log cleanup functionality; currently deletes any log files older than 7 days when winget runs.
  • Fixed UTF8Substring when count causes the substring to extend past the end of the input.
  • Added an RAII type to disable telemetry while it exists, and use it while running Command::Complete to prevent any errors in common code from showing up as noise.
  • Added a StartsWith match type to the search interface.

Validation

Tests are added for CompletionData and Command::Complete handling of said data. Tests are also added for StartsWith searches and string comparisons.

Microsoft Reviewers: Open in CodeFlow

@JohnMcPMS
JohnMcPMS requested a review from a team as a code owner July 30, 2020 22:14
Comment thread doc/Completion.md

WinGet offers a `complete` command that can be leveraged by your shell to provide context sensitive tab completion. It allows for completion of command names, argument names, and argument values, dependent on the current command line state.

> Note, this feature has not yet been released. Hold tight, it is coming soon!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you going to be able to remember this line when it's released?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the plan, but if not I'm sure someone will poke us.

{
using namespace std::string_view_literals;

using namespace Execution;

@msftrubengu Ruben Guerrero (msftrubengu) Jul 31, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit #Pending

// be context sensitive in their data output.
struct CompleteCommand final : public Command
{
CompleteCommand(std::string_view parent) : Command("complete", parent, Visibility::Hidden) {}

@msftrubengu Ruben Guerrero (msftrubengu) Jul 31, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CompleteCommand(std::string_view parent) : Command("complete", parent, Visibility::Hidden) {} [](start = 8, length = 93)

So we are not going to release it as an experimental feature? #ByDesign

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, since you have to opt in by setting up your shell to call into this, and it is otherwise out of the way, it isn't really necessary.


In reply to: 463771454 [](ancestors = 463771454)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

}

// Reporter should be the central place to show workflow status to user.
// Todo: need to implement actual console output to show progress bar, etc

@yao-msft yao-msft Jul 31, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Todo: need to implement actual console output to show progress bar, etc [](start = 4, length = 74)

nit: this could be removed #Pending

int32_t i = 0;

while (i != UBRK_DONE)
// Count past end, convert to npos to get all of string

@yao-msft yao-msft Aug 1, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Count past end [](start = 11, length = 14)

curious why do we not treat this as a bad input? #ByDesign

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with std::string::substr


In reply to: 463899310 [](ancestors = 463899310)

@yao-msft yao-msft left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@JohnMcPMS
JohnMcPMS merged commit ce587d4 into microsoft:master Aug 3, 2020
@JohnMcPMS
JohnMcPMS deleted the complete branch August 3, 2020 18:50
@Wolf1098

Wolf1098 (Wolf1098) commented Sep 20, 2021 •

Copy link
Copy Markdown

Maybe look at making it work with

# Shows navigable menu for all module's and their options when hitting Tab

Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete

edit: It is compatible while being installed along side with the above, but the above alone isn't sufficient to cause it to auto-complete win-get

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants