From be9e5f98b3c2a49b679e88a4497f6350422ddc5d Mon Sep 17 00:00:00 2001 From: houk-ms Date: Wed, 23 Dec 2020 14:46:48 +0800 Subject: [PATCH 1/4] add util function to highlight a command --- src/azure-cli-core/azure/cli/core/style.py | 27 ++++++++++++++ .../azure/cli/core/tests/test_style.py | 36 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/azure-cli-core/azure/cli/core/style.py b/src/azure-cli-core/azure/cli/core/style.py index 4369ed84e8b..c2e5dccadbd 100644 --- a/src/azure-cli-core/azure/cli/core/style.py +++ b/src/azure-cli-core/azure/cli/core/style.py @@ -72,3 +72,30 @@ def format_styled_text(styled_text): # Reset control sequence formatted_parts.append(Fore.RESET) return ''.join(formatted_parts) + + +def highlight_command(raw_command): + """highlight a command to make it colored. + + :param raw_command: The command that needs to be colored + :type raw_command: str + :return: The styled command text + :type: list + """ + + 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 + + styled_command.append((style, spaced_arg)) + + return styled_command diff --git a/src/azure-cli-core/azure/cli/core/tests/test_style.py b/src/azure-cli-core/azure/cli/core/tests/test_style.py index 37bbb97be00..9575ad207ce 100644 --- a/src/azure-cli-core/azure/cli/core/tests/test_style.py +++ b/src/azure-cli-core/azure/cli/core/tests/test_style.py @@ -43,6 +43,42 @@ def test_format_styled_text(self): with self.assertRaisesRegex(CLIInternalError, "Invalid styled text."): format_styled_text(["dummy text"]) + def test_highlight_command(self): + from azure.cli.core.style import Style, highlight_command, format_styled_text + + raw_commands = [ + 'az cmd', + 'az cmd sub-cmd', + 'az cmd --arg val', + 'az cmd --arg=val', + 'az cmd --arg "content with space"', + 'az cmd --arg val1 val2', + 'az cmd sub-cmd --arg1 val1 --arg2=" " --arg3 "val2 val3" --arg4 val4' + ] + + expected = [ + [(Style.ACTION, 'az'), (Style.ACTION, ' cmd')], + [(Style.ACTION, 'az'), (Style.ACTION, ' cmd'), (Style.ACTION, ' sub-cmd')], + [(Style.ACTION, 'az'), (Style.ACTION, ' cmd'), (Style.ACTION, ' --arg'), (Style.PRIMARY, ' val')], + [(Style.ACTION, 'az'), (Style.ACTION, ' cmd'), (Style.PRIMARY, ' --arg=val')], + [(Style.ACTION, 'az'), (Style.ACTION, ' cmd'), (Style.ACTION, ' --arg'), (Style.PRIMARY, ' "content'), + (Style.PRIMARY, ' with'), (Style.PRIMARY, ' space"')], + [(Style.ACTION, 'az'), (Style.ACTION, ' cmd'), (Style.ACTION, ' --arg'), + (Style.PRIMARY, ' val1'), (Style.PRIMARY, ' val2')], + [(Style.ACTION, 'az'), (Style.ACTION, ' cmd'), (Style.ACTION, ' sub-cmd'), (Style.ACTION, ' --arg1'), + (Style.PRIMARY, ' val1'), (Style.PRIMARY, ' --arg2="'), (Style.PRIMARY, ' "'), (Style.ACTION, ' --arg3'), + (Style.PRIMARY, ' "val2'), (Style.PRIMARY, ' val3"'), (Style.ACTION, ' --arg4'), (Style.PRIMARY, ' val4')] + ] + + for cmd_ind, command in enumerate(raw_commands): + styled_command = highlight_command(command) + expected_style = expected[cmd_ind] + + for tpl_ind, style_tuple in enumerate(styled_command): + expected_tuple = expected_style[tpl_ind] + self.assertEqual(style_tuple[0], expected_tuple[0]) + self.assertEqual(style_tuple[1], expected_tuple[1]) + if __name__ == '__main__': unittest.main() From af3383446a73c8d436a3e07d6ef2ad8af91ef83b Mon Sep 17 00:00:00 2001 From: houk-ms Date: Wed, 23 Dec 2020 14:56:45 +0800 Subject: [PATCH 2/4] code style refining --- src/azure-cli-core/azure/cli/core/tests/test_style.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/tests/test_style.py b/src/azure-cli-core/azure/cli/core/tests/test_style.py index 9575ad207ce..f828b33cd35 100644 --- a/src/azure-cli-core/azure/cli/core/tests/test_style.py +++ b/src/azure-cli-core/azure/cli/core/tests/test_style.py @@ -70,12 +70,12 @@ def test_highlight_command(self): (Style.PRIMARY, ' "val2'), (Style.PRIMARY, ' val3"'), (Style.ACTION, ' --arg4'), (Style.PRIMARY, ' val4')] ] - for cmd_ind, command in enumerate(raw_commands): + for cmd_index, command in enumerate(raw_commands): styled_command = highlight_command(command) - expected_style = expected[cmd_ind] + expected_style = expected[cmd_index] - for tpl_ind, style_tuple in enumerate(styled_command): - expected_tuple = expected_style[tpl_ind] + for tpl_index, style_tuple in enumerate(styled_command): + expected_tuple = expected_style[tpl_index] self.assertEqual(style_tuple[0], expected_tuple[0]) self.assertEqual(style_tuple[1], expected_tuple[1]) From 893e1e0d441be1dc77699d4a44402409b511fa25 Mon Sep 17 00:00:00 2001 From: houk-ms Date: Thu, 24 Dec 2020 10:31:48 +0800 Subject: [PATCH 3/4] add more comments --- src/azure-cli-core/azure/cli/core/style.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/azure-cli-core/azure/cli/core/style.py b/src/azure-cli-core/azure/cli/core/style.py index c2e5dccadbd..3cc09ed72d0 100644 --- a/src/azure-cli-core/azure/cli/core/style.py +++ b/src/azure-cli-core/azure/cli/core/style.py @@ -75,7 +75,14 @@ def format_styled_text(styled_text): def highlight_command(raw_command): - """highlight a command to make it colored. + """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 From ff163cad791df2960d17e695ffd02b285c77991f Mon Sep 17 00:00:00 2001 From: Houk <62928370+houk-ms@users.noreply.github.com> Date: Wed, 20 Jan 2021 15:11:02 +0800 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Jiashuo Li --- src/azure-cli-core/azure/cli/core/style.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/style.py b/src/azure-cli-core/azure/cli/core/style.py index be93dbedeee..e61fa61d410 100644 --- a/src/azure-cli-core/azure/cli/core/style.py +++ b/src/azure-cli-core/azure/cli/core/style.py @@ -165,19 +165,20 @@ def format_styled_text(styled_text, theme=None): def highlight_command(raw_command): - """Highlight a command to make it colored. + """Highlight a command with colors. - 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. + For example, for - :param raw_command: The command that needs to be colored + az group create --name myrg --location westus + + The command name 'az group create', argument name '--name', '--location' are marked as ACTION style. + The argument value 'myrg' and 'westus' are marked as PRIMARY style. + If the argument is provided as '--location=westus', it will be marked as PRIMARY style. + + :param raw_command: The command that needs to be highlighted. :type raw_command: str - :return: The styled command text - :type: list + :return: The styled command text. + :rtype: list """ styled_command = []