Skip to content
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

Init colorama only in Windows legacy terminal #238

Merged
merged 3 commits into from
Mar 22, 2021

Conversation

jiasli
Copy link
Member

@jiasli jiasli commented Mar 10, 2021

Init colomara only on Windows, because

  • Bash of Linux and MacOS already supports control sequences
  • Removing the colomara wrapper increases the output performance and can avoid possible colorama bugs ([Color] Detect isatty when enabling color #209)
  • _should_init_colorama can be furtherly disabled by Azure CLI

@jiasli jiasli changed the title Init colomara only on Windows Init colorama only in Windows legacy terminal Mar 11, 2021
Base automatically changed from master to dev March 12, 2021 07:02
@@ -101,6 +101,8 @@ def __init__(self,

self.only_show_errors = self.config.getboolean('core', 'only_show_errors', fallback=False)
self.enable_color = self._should_enable_color()
# Init colorama only in Windows legacy terminal
self._should_init_colorama = self.enable_color and os.name == 'nt' and not is_modern_terminal()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not (self.enable_color or _KNACK_TEST_FORCE_ENABLE_COLOR) and ...? Does that mean knack test always use colorama no matter it's modern terminal or legacy terminal?

Copy link
Member Author

Choose a reason for hiding this comment

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

Why not (self.enable_color or _KNACK_TEST_FORCE_ENABLE_COLOR) and ...?

The action scope of mocked _KNACK_TEST_FORCE_ENABLE_COLOR is CLI.invoke. If _KNACK_TEST_FORCE_ENABLE_COLOR is used in CLI.__invoke__, CLI.invoke won't be affected and won't have colorama enabled.

knack/tests/util.py

Lines 36 to 37 in cd59031

with mock.patch("knack.cli._KNACK_TEST_FORCE_ENABLE_COLOR", True):
func(self)

Does that mean knack test always use colorama no matter it's modern terminal or legacy terminal?

Yes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Dropped _KNACK_TEST_FORCE_ENABLE_COLOR. Now strip control sequences by knack itself:

knack/tests/util.py

Lines 57 to 58 in e5e0529

def _remove_control_sequence(string):
return re.sub(r'\x1b[^m]+m', '', string)

Copy link
Contributor

Choose a reason for hiding this comment

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

huge work😂

Comment on lines +65 to +67
def assert_in_multi_line(sub_string, string):
# assert sub_string is in string, with all whitespaces, line breaks and control sequences ignored
assert _remove_whitespace(sub_string) in _remove_control_sequence(_remove_whitespace(string))
Copy link
Member Author

Choose a reason for hiding this comment

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

This function does the job of removing control sequences which are previously removed by colorama.

@jiasli jiasli mentioned this pull request Mar 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants