Skip to content

Conversation

@jiasli
Copy link
Member

@jiasli jiasli commented Dec 14, 2020

Description

Refine the style framework.

  1. Since BLUE and LIGHTBLUE_EX are not very visible/accessible under powershell.exe (see the demo at https://github.com/jiasli/color-demo), they are replaced with LIGHTWHITE_EX.

    ℹ This doesn't apply to PowerShell sessions inside modern terminals, see item 5.

  2. Honor cli_ctx.enable_color so that when color is disabled, it can remove color when formatting styled text. This can eliminate the necessity to make 2 versions of the same string, one with color, one without, like

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

    In the demo, you may easily turn off color with

    az demo style --no-color
    
  3. Change the color of primary and secondary text:

    • Primary color: Use default foreground color instead of BRIGHT WHITE so that at least primary text is visible under both dark/light themes
    • Secondary color: Use BRIGHT BLACK
  4. Add light theme support which uses dark versions of colors. To try it, in light/dark-themed terminals, run

    az demo style --theme light/dark
    
  5. Add function is_modern_terminal to detect if the terminal is a modern terminal like VS Code, Windows Terminal.

  6. print_styled_text wraps print and can pass kwargs.

  7. format_styled_text supports more formats.

Testing Guide

az demo style

powershell.exe:

image

pwsh.exe:

image

@jiasli jiasli self-assigned this Dec 14, 2020
@jiasli jiasli added the style label Dec 14, 2020
@jiasli jiasli requested a review from fengzhou-msft December 14, 2020 07:20
@yonzhan
Copy link
Collaborator

yonzhan commented Dec 14, 2020

Style

@yonzhan yonzhan added this to the S180 milestone Dec 14, 2020
@jiasli jiasli requested a review from evelyn-ys December 14, 2020 07:48
Comment on lines +69 to +72
# dependencies for specific OSes
if not sys.platform.startswith('cygwin'):
DEPENDENCIES.append('psutil~=5.7')

Copy link
Member Author

Choose a reason for hiding this comment

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

Move the dependency of psutil to core as it is used by core, not command modules. @houk-ms

@chenlomis
Copy link

LGTM from the design side as per our latest discussion!

@jiasli jiasli mentioned this pull request Dec 16, 2020
@jiasli jiasli changed the title {Style} Replace BLUE in powershell.exe, honor enable_color {Style} Replace BLUE in powershell.exe, honor enable_color, light theme Dec 16, 2020
@jiasli jiasli changed the title {Style} Replace BLUE in powershell.exe, honor enable_color, light theme {Style} Refine style framework Dec 16, 2020
@yonzhan yonzhan modified the milestones: S180, S181 Dec 26, 2020
@zhoxing-ms
Copy link
Contributor

May I ask could we support end parameter of print() in the print_styled_text() function?

At present, I have a requirement that the message of input() should also be colored, such as:
Screenshot 2020-12-28 161054

My previous implementation is:

print("Please input " + Fore.LIGHTBLUE_EX + param + Fore.RESET + ":", end='')
value = input()

Or do we have other better ways to implement this requirement, such as wrapping a specialized method?

@jiasli
Copy link
Member Author

jiasli commented Dec 29, 2020

@zhoxing-ms, end is already supported. Note that kwargs is passed to print as is.

print(*formatted_list, file=file or sys.stderr, **kwargs)

Let me add some comments to make it clearer.

jiasli added 2 commits January 4, 2021 16:53
# Conflicts:
#	src/azure-cli-core/azure/cli/core/__init__.py
@jiasli jiasli marked this pull request as ready for review January 4, 2021 08:58
@jiasli jiasli requested review from Juliehzl and jsntcy as code owners January 4, 2021 08:58
Comment on lines 101 to 111
# Convert str to the theme dict
if isinstance(theme, str):
if theme.lower() == 'none':
theme = THEME_NONE
elif theme.lower() == 'dark':
theme = THEME_DARK
elif theme.lower() == 'light':
theme = THEME_LIGHT
else:
from azure.cli.core.azclierror import CLIInternalError
raise CLIInternalError("Invalid theme. Supported themes: none, dark, light")
Copy link
Contributor

@zhoxing-ms zhoxing-ms Jan 4, 2021

Choose a reason for hiding this comment

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

May I ask why do we need to support the case that theme is string?

By the way, could we consider setting a theme configuration as a enum for this mapping relationship instead of if-else logic? Such as:

class THEME(Enum):
    none = THEME_NONE,
    dark = THEME_DARK,
    light = THEME_LIGHT

If this enum is used as parameter, I personally think it is more readable and easier maintained~

Copy link
Member Author

Choose a reason for hiding this comment

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

May I ask why do we need to support the case that theme is string?

Because everything passed to CLI via the shell will be treated as str.

Moreover, the values of an Enum are usually basic types like str or int. Using dict as the value, like

class Theme(Enum):
    DARK = THEME_DARK
    LIGHT = THEME_LIGHT
    NONE = THEME_NONE

will result in:

> az demo style -h

Arguments
    --theme            : The theme to format styled text.  Allowed values: {<Style.PRIMARY:
                         'primary'>: '\x1b[39m', <Style.SECONDARY: 'secondary'>: '\x1b[90m',
                         <Style.IMPORTANT: 'important'>: '\x1b[35m', <Style.ACTION: 'action'>:
                         '\x1b[34m', <Style.HYPERLINK: 'hyperlink'>: '\x1b[36m', <Style.ERROR:
                         'error'>: '\x1b[31m', <Style.SUCCESS: 'success'>: '\x1b[32m',
                         <Style.WARNING: 'warning'>: '\x1b[33m'}, {<Style.PRIMARY: 'primary'>:
                         '\x1b[39m', <Style.SECONDARY: 'secondary'>: '\x1b[90m', <Style.IMPORTANT:
                         'important'>: '\x1b[95m', <Style.ACTION: 'action'>: '\x1b[94m',
                         <Style.HYPERLINK: 'hyperlink'>: '\x1b[96m', <Style.ERROR: 'error'>:
                         '\x1b[91m', <Style.SUCCESS: 'success'>: '\x1b[92m', <Style.WARNING:
                         'warning'>: '\x1b[93m'}, {}.  Default: dark.

But the idea of using Enum for Theme is good. Added as requested:

class Theme(str, Enum):
DARK = 'dark'
LIGHT = 'light'
NONE = 'none'
THEME_DEFINITIONS = {
Theme.NONE: THEME_NONE,
Theme.DARK: THEME_DARK,
Theme.LIGHT: THEME_LIGHT
}

Copy link
Contributor

Choose a reason for hiding this comment

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

OK

Comment on lines 439 to 444
# Linux
process.name.return_value = "python"
parent1.name.return_value = "bash"
parent2.name.return_value = "init"
parent3.name.return_value = "init"
self.assertEqual(_get_parent_proc_name(), "bash")
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, I just want to confirm that Mac and Linux are exactly the same, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. Mac is also using bash, but it doesn't really mater as long as no ancestor process is called powershell.exe, pwsh.exe.

However, I think pwsh running on Linux can be a more complex scenario. I need to do more testing on that.

Copy link
Contributor

@zhoxing-ms zhoxing-ms left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@houk-ms houk-ms left a comment

Choose a reason for hiding this comment

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

@jiasli When do you plan to get this PR merged? Is it possible to catch S181?

@yonzhan yonzhan modified the milestones: S181, S182 Jan 16, 2021
@jiasli jiasli merged commit 6f25505 into Azure:dev Jan 20, 2021
@jiasli jiasli deleted the style-color branch July 15, 2021 06:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants