-
Notifications
You must be signed in to change notification settings - Fork 3.4k
{Core} Add util function highlight_command #16349
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -164,6 +164,40 @@ def format_styled_text(styled_text, theme=None): | |||||||||||
| return ''.join(formatted_parts) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def highlight_command(raw_command): | ||||||||||||
| """Highlight a command to make it colored. | ||||||||||||
|
|
||||||||||||
| The highlighting rules after the command is splitted into args: | ||||||||||||
| 1. Args starting with '-' are regarded as parameter names, and they are with the ACTION style. | ||||||||||||
| 2. Args before the first parameter name are regarded as command name (or positional argument), | ||||||||||||
| and they are with the ACTION style. | ||||||||||||
| 3. Args between the parameter names are regarded as parameter values, and they are with the PRIMARY style. | ||||||||||||
| 4. Args containing '=' are regarded as parameter name-value pairs, and they are with the PRIMARY style. | ||||||||||||
|
|
||||||||||||
| :param raw_command: The command that needs to be colored | ||||||||||||
| :type raw_command: str | ||||||||||||
| :return: The styled command text | ||||||||||||
| :type: list | ||||||||||||
|
houk-ms marked this conversation as resolved.
Outdated
|
||||||||||||
| """ | ||||||||||||
|
|
||||||||||||
| styled_command = [] | ||||||||||||
| argument_begins = False | ||||||||||||
|
|
||||||||||||
| for index, arg in enumerate(raw_command.split()): | ||||||||||||
| spaced_arg = ' {}'.format(arg) if index > 0 else arg | ||||||||||||
| style = Style.PRIMARY | ||||||||||||
|
|
||||||||||||
| if arg.startswith('-') and '=' not in arg: | ||||||||||||
| style = Style.ACTION | ||||||||||||
| argument_begins = True | ||||||||||||
| elif not argument_begins and '=' not in arg: | ||||||||||||
| style = Style.ACTION | ||||||||||||
|
Comment on lines
+191
to
+195
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add comment about the rule used?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made some comments at #16257 (comment) The logic of
until semantic analysis is involved. Let's keep it simple and treat it as part of the command by now.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another possible solution is to utilize azure-cli/linter_exclusions.yml Lines 1181 to 1185 in 2c52cbc
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good proposal, but also would make the function much more complex and harder to maintain. I prefer we keep it simple for now, until we really need to consider the case. |
||||||||||||
|
|
||||||||||||
| styled_command.append((style, spaced_arg)) | ||||||||||||
|
|
||||||||||||
| return styled_command | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def _is_modern_terminal(): | ||||||||||||
| # Windows Terminal: https://github.com/microsoft/terminal/issues/1040 | ||||||||||||
| if 'WT_SESSION' in os.environ: | ||||||||||||
|
|
||||||||||||
Uh oh!
There was an error while loading. Please reload this page.