-
Notifications
You must be signed in to change notification settings - Fork 161
feat(cli): add autocomplete module #644
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
Open
shreejaykurhade
wants to merge
10
commits into
QuantConnect:master
Choose a base branch
from
shreejaykurhade:feat/autocomplete-aliasing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d5cb8a7
feat(cli): add prefix-based command matching and autocomplete module
shreejaykurhade ca34013
feat(cli): add Click-native shell completion and prefix-based command…
shreejaykurhade 0883232
feat(cli): add Click-native shell completion with simple on/off commands
shreejaykurhade 573d4c1
feat(cli): improve Click-native shell completion
shreejaykurhade 4376bb8
fix(cli): stop PowerShell completion from using history predictions
shreejaykurhade 6a3eaa8
fix(cli): handle denied shell profile updates
shreejaykurhade 795c667
fix(cli): add current-session completion cleanup
shreejaykurhade 569aced
refactor(cli): rename completion command to autocomplete
shreejaykurhade 7c6afe9
refactor(cli): make autocomplete the primary command
shreejaykurhade 862f51d
Fixed the failure path.
shreejaykurhade File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
| # Lean CLI v1.0. Copyright 2021 QuantConnect Corporation. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import Optional | ||
|
|
||
| from click import Choice, ClickException, Context, echo, group, option, pass_context | ||
|
|
||
| from lean.components.util.click_aliased_command_group import AliasedCommandGroup | ||
|
|
||
|
|
||
| SHELL_OPTION = option("--shell", | ||
| "-s", | ||
| type=Choice(["powershell", "bash", "zsh", "fish"], case_sensitive=False), | ||
| default=None, | ||
| help="Target shell. Auto-detected if not specified.") | ||
|
|
||
|
|
||
| def _profile_permission_error(exception: PermissionError) -> ClickException: | ||
| path = exception.filename or "the shell profile" | ||
| return ClickException( | ||
| f"Unable to update {path}. " | ||
| "The current PowerShell session is still disabled if Lean autocomplete was loaded there. " | ||
| "To remove it permanently, close any editor or terminal using that profile, or remove the Lean autocomplete block manually." | ||
| ) | ||
|
|
||
|
|
||
| @group(cls=AliasedCommandGroup, invoke_without_command=True) | ||
| @SHELL_OPTION | ||
| @pass_context | ||
| def autocomplete(ctx: Context, shell: Optional[str]) -> None: | ||
| """Print the native shell autocomplete script for your shell. | ||
|
|
||
| \b | ||
| PowerShell (current session): | ||
| lean autocomplete --shell powershell | Out-String | Invoke-Expression | ||
|
|
||
| \b | ||
| Bash or Zsh (current session): | ||
| eval "$(lean autocomplete --shell bash)" | ||
|
|
||
| \b | ||
| Fish (current session): | ||
| lean autocomplete --shell fish | source | ||
| """ | ||
| if ctx.invoked_subcommand is None: | ||
| from lean.components.util.click_shell_autocomplete import get_autocomplete_script | ||
| echo(get_autocomplete_script(shell)) | ||
|
|
||
|
|
||
| @autocomplete.command(name="show", help="Print the native shell autocomplete script for your shell") | ||
| @SHELL_OPTION | ||
| def show(shell: Optional[str]) -> None: | ||
| from lean.components.util.click_shell_autocomplete import get_autocomplete_script | ||
| echo(get_autocomplete_script(shell)) | ||
|
|
||
|
|
||
| @autocomplete.command(name="on", help="Enable shell autocomplete in your shell profile") | ||
| @SHELL_OPTION | ||
| def on(shell: Optional[str]) -> None: | ||
| from lean.components.util.click_shell_autocomplete import install_autocomplete | ||
| try: | ||
| profile_path = install_autocomplete(shell) | ||
| except PermissionError as exception: | ||
| raise _profile_permission_error(exception) | ||
|
|
||
| echo(f"Enabled shell autocomplete in {profile_path}") | ||
| echo("Open a new terminal session for the change to take effect.") | ||
|
|
||
|
|
||
| @autocomplete.command(name="off", help="Disable shell autocomplete in your shell profile") | ||
| @option("--current-session", is_flag=True, help="Print a script that disables autocomplete in the current shell session.") | ||
| @SHELL_OPTION | ||
| def off(shell: Optional[str], current_session: bool) -> None: | ||
| from lean.components.util.click_shell_autocomplete import get_autocomplete_cleanup_script, uninstall_autocomplete | ||
|
|
||
| if current_session: | ||
| echo(get_autocomplete_cleanup_script(shell)) | ||
| return | ||
|
|
||
| try: | ||
| profile_path, removed = uninstall_autocomplete(shell) | ||
| except PermissionError as exception: | ||
| raise _profile_permission_error(exception) | ||
|
|
||
| if removed: | ||
| echo(f"Disabled shell autocomplete in {profile_path}") | ||
| echo("Open a new terminal session for the change to take effect.") | ||
| echo("To disable it in this PowerShell session, run `lean autocomplete off` again after reloading the updated script.") | ||
| else: | ||
| echo(f"Shell autocomplete was not enabled in {profile_path}") |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.