Skip to content

Commit 54aba72

Browse files
authored
Allow disabling color (#12601)
1 parent e6c3bdc commit 54aba72

5 files changed

Lines changed: 19 additions & 29 deletions

File tree

src/azure-cli-core/azure/cli/core/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ def get_cli_version(self):
8585

8686
def show_version(self):
8787
from azure.cli.core.util import get_az_version_string
88-
from azure.cli.core.commands.constants import SURVEY_PROMPT
89-
import colorama
88+
from azure.cli.core.commands.constants import SURVEY_PROMPT, SURVEY_PROMPT_COLOR
89+
9090
ver_string, updates_available = get_az_version_string()
9191
print(ver_string)
9292
if updates_available == -1:
@@ -98,9 +98,7 @@ def show_version(self):
9898
else:
9999
print('Your CLI is up-to-date.')
100100

101-
colorama.init() # This could be removed when knack fix is released
102-
print('\n' + SURVEY_PROMPT)
103-
colorama.deinit() # This could be removed when knack fix is released
101+
print('\n' + (SURVEY_PROMPT_COLOR if self.enable_color else SURVEY_PROMPT))
104102

105103
def exception_handler(self, ex): # pylint: disable=no-self-use
106104
from azure.cli.core.util import handle_exception
@@ -448,6 +446,7 @@ def command_group(self, group_name, command_type=None, **kwargs):
448446
kwargs['deprecate_info'].target = group_name
449447
if kwargs.get('is_preview', False):
450448
kwargs['preview_info'] = PreviewItem(
449+
cli_ctx=self.cli_ctx,
451450
target=group_name,
452451
object_type='command group'
453452
)

src/azure-cli-core/azure/cli/core/_help.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import argparse
88

99
from azure.cli.core.commands import ExtensionCommandSource
10-
from azure.cli.core.commands.constants import SURVEY_PROMPT
10+
from azure.cli.core.commands.constants import SURVEY_PROMPT, SURVEY_PROMPT_COLOR
1111

1212
from knack.help import (HelpFile as KnackHelpFile, CommandHelpFile as KnackCommandHelpFile,
1313
GroupHelpFile as KnackGroupHelpFile, ArgumentGroupRegistry as KnackArgumentGroupRegistry,
@@ -64,6 +64,7 @@ def _print_header(self, cli_name, help_file):
6464
def _print_detailed_help(self, cli_name, help_file):
6565
CLIPrintMixin._print_extensions_msg(help_file)
6666
super(CLIPrintMixin, self)._print_detailed_help(cli_name, help_file)
67+
self._print_az_find_message(help_file.command, self.cli_ctx.enable_color)
6768

6869
@staticmethod
6970
def _get_choices_defaults_sources_str(p):
@@ -75,7 +76,6 @@ def _get_choices_defaults_sources_str(p):
7576

7677
@staticmethod
7778
def _print_examples(help_file):
78-
from colorama import Style
7979
indent = 0
8080
_print_indent('Examples', indent)
8181
for e in help_file.examples:
@@ -86,9 +86,15 @@ def _print_examples(help_file):
8686
_print_indent(u'{0}'.format(e.long_summary), indent)
8787
_print_indent(u'{0}'.format(e.command), indent)
8888
print('')
89+
90+
@staticmethod
91+
def _print_az_find_message(command, enable_color):
92+
from colorama import Style
8993
indent = 0
90-
message = 'For more specific examples, use: az find "az {}"'.format(help_file.command)
91-
_print_indent(Style.BRIGHT + message + Style.RESET_ALL + '\n', indent)
94+
message = 'For more specific examples, use: az find "az {}"'.format(command)
95+
if enable_color:
96+
message = Style.BRIGHT + message + Style.RESET_ALL
97+
_print_indent(message + '\n', indent)
9298

9399
@staticmethod
94100
def _process_value_sources(p):
@@ -150,8 +156,6 @@ def new_normalize_text(s):
150156
def show_help(self, cli_name, nouns, parser, is_group):
151157
self.update_loaders_with_help_file_contents(nouns)
152158

153-
import colorama
154-
colorama.init(autoreset=True)
155159
delimiters = ' '.join(nouns)
156160
help_file = self.command_help_cls(self, delimiters, parser) if not is_group \
157161
else self.group_help_cls(self, delimiters, parser)
@@ -162,7 +166,8 @@ def show_help(self, cli_name, nouns, parser, is_group):
162166
AzCliHelp.update_examples(help_file)
163167
self._print_detailed_help(cli_name, help_file)
164168

165-
print(SURVEY_PROMPT)
169+
from colorama import Fore, Style
170+
print(SURVEY_PROMPT_COLOR if self.cli_ctx.enable_color else SURVEY_PROMPT)
166171

167172
def _register_help_loaders(self):
168173
import azure.cli.core._help_loaders as help_loaders

src/azure-cli-core/azure/cli/core/commands/__init__.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -686,8 +686,6 @@ def resolve_warnings(self, cmd, parsed_args):
686686
self._resolve_extension_override_warning(cmd)
687687

688688
def _resolve_preview_and_deprecation_warnings(self, cmd, parsed_args):
689-
import colorama
690-
691689
deprecations = [] + getattr(parsed_args, '_argument_deprecations', [])
692690
if cmd.deprecate_info:
693691
deprecations.append(cmd.deprecate_info)
@@ -724,12 +722,10 @@ def _resolve_preview_and_deprecation_warnings(self, cmd, parsed_args):
724722
del preview_kwargs['_get_message']
725723
previews.append(ImplicitPreviewItem(**preview_kwargs))
726724

727-
colorama.init()
728725
for d in deprecations:
729726
print(d.message, file=sys.stderr)
730727
for p in previews:
731728
print(p.message, file=sys.stderr)
732-
colorama.deinit()
733729

734730
def _resolve_extension_override_warning(self, cmd): # pylint: disable=no-self-use
735731
if isinstance(cmd.command_source, ExtensionCommandSource) and cmd.command_source.overrides_command:
@@ -864,12 +860,8 @@ def _generate_template_progress(self, correlation_id): # pylint: disable=no-sel
864860
logger.info(result)
865861

866862
def __call__(self, poller):
867-
import colorama
868863
from msrest.exceptions import ClientException
869864

870-
# https://github.com/azure/azure-cli/issues/3555
871-
colorama.init()
872-
873865
correlation_message = ''
874866
self.cli_ctx.get_progress_controller().begin()
875867
correlation_id = None
@@ -910,7 +902,6 @@ def __call__(self, poller):
910902
handle_long_running_operation_exception(client_exception)
911903

912904
self.cli_ctx.get_progress_controller().end()
913-
colorama.deinit()
914905

915906
return result
916907

src/azure-cli-core/azure/cli/core/commands/constants.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131

3232
BLACKLISTED_MODS = ['context', 'shell', 'documentdb', 'component']
3333

34-
SURVEY_PROMPT = Fore.YELLOW + Style.BRIGHT + 'Please let us know how we are doing: ' + Fore.BLUE \
35-
+ 'https://aka.ms/clihats' + Style.RESET_ALL
34+
SURVEY_PROMPT = 'Please let us know how we are doing: https://aka.ms/clihats'
35+
SURVEY_PROMPT_COLOR = Fore.YELLOW + Style.BRIGHT + 'Please let us know how we are doing: ' + Fore.BLUE \
36+
+ 'https://aka.ms/clihats' + Style.RESET_ALL

src/azure-cli-core/azure/cli/core/extension/operations.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ def _validate_whl_extension(ext_file):
7777

7878

7979
def _add_whl_ext(cmd, source, ext_sha256=None, pip_extra_index_urls=None, pip_proxy=None): # pylint: disable=too-many-statements
80-
import colorama
81-
colorama.init() # Required for displaying the spinner correctly on windows issue #9140
8280
cmd.cli_ctx.get_progress_controller().add(message='Analyzing')
8381
if not source.endswith('.whl'):
8482
raise ValueError('Unknown extension type. Only Python wheels are supported.')
@@ -155,7 +153,6 @@ def _add_whl_ext(cmd, source, ext_sha256=None, pip_extra_index_urls=None, pip_pr
155153
dst = os.path.join(extension_path, whl_filename)
156154
shutil.copyfile(ext_file, dst)
157155
logger.debug('Saved the whl to %s', dst)
158-
colorama.deinit()
159156

160157

161158
def is_valid_sha256sum(a_file, expected_sum):
@@ -201,10 +198,7 @@ def add_extension(cmd, source=None, extension_name=None, index_url=None, yes=Non
201198
pip_extra_index_urls=None, pip_proxy=None):
202199
ext_sha256 = None
203200
if extension_name:
204-
import colorama
205-
colorama.init() # Required for displaying the spinner correctly on windows issue #9140
206201
cmd.cli_ctx.get_progress_controller().add(message='Searching')
207-
colorama.deinit()
208202
ext = None
209203
try:
210204
ext = get_extension(extension_name)

0 commit comments

Comments
 (0)