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

Fixing Windows color regression #5327

Merged
merged 8 commits into from
Jun 7, 2022
Merged

Fixing Windows color regression #5327

merged 8 commits into from
Jun 7, 2022

Conversation

leahwicz
Copy link
Contributor

@leahwicz leahwicz commented Jun 1, 2022

resolves #5191

Description

With recent changes to Colorama logic in PR #4792, a regression was introduced that stopped logs from having color for Windows. This PR fixes that and reworks some logic that seems duplicated in 2 places but not really needed. The original fix was to keep color in logs when piped for MacOS and Linux which I verified still is happening with the changes

ℹ️ The original logic for Colorama in logger.py is 5 yrs old. I'm not sure why we did it the way we did it but I simplified to initialize Colorama only on Windows for non-Unix based terminals. Originally we tried to initialize it for all OS's but this led to the color not being preserved in the stdout that was addressed in #4792. My test matrix is below on all the combos I tried out and saw the same colored output.

This is what I tested and screenshots of my output:

  • Windows CMD:
    image

  • Windows Git Bash:
    image

  • Windows Git Bash with piping output:
    image

  • Mac iTerm:

  • Mac iTerm with piping output:
    image

Checklist

@cla-bot cla-bot bot added the cla:yes label Jun 1, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Jun 1, 2022

Thank you for your pull request! We could not find a changelog entry for this change. For details on how to document a change, see the contributing guide.

@leahwicz leahwicz marked this pull request as ready for review June 2, 2022 00:05
@leahwicz leahwicz requested a review from a team June 2, 2022 00:05
@leahwicz leahwicz requested review from a team as code owners June 2, 2022 00:05
@leahwicz leahwicz requested review from iknox-fa and removed request for iknox-fa June 2, 2022 00:05
@@ -50,14 +49,6 @@
format_json = False
invocation_id: Optional[str] = None

# Colorama needs some help on windows because we're using logger.info
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nathaniel-may I know you added this in PR ##4474. I don't see a difference with it included or not in my testing. Can you think of scenario for me to test that would make code actually important here? It looks like we always call the logger.py that has the initialization we want, but I wanted to check with you first

Copy link
Contributor

Choose a reason for hiding this comment

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

On Windows, calling init() will filter ANSI escape sequences out of any text sent to stdout or stderr, and replace them with equivalent Win32 calls.

from the colorama readme.

You could test by logging a message that includes an ansi escape sequence such as ESC[31m which should make the following characters have a red background. Without calling init on Windows with that message it might have odd behavior.

logging_stdout = sys.stdout
if sys.platform == "win32":
if not os.getenv("TERM"):
logging_stdout = colorama.AnsiToWin32(sys.stdout).stream
Copy link
Contributor Author

Choose a reason for hiding this comment

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

colorama.AnsiToWin32(sys.stdout).stream is only used when wrap=False. When changing to hit this for Windows only terminals, we can set wrap=True and drop the AnsiToWin32 call

@@ -455,7 +454,7 @@ def emit(self, record: logbook.LogRecord):


class LogManager(logbook.NestedSetup):
def __init__(self, stdout=logging_stdout, stderr=sys.stderr):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed logging_stdout above

@leahwicz leahwicz merged commit e48f7ab into main Jun 7, 2022
@leahwicz leahwicz deleted the leahwicz/winColor branch June 7, 2022 13:06
github-actions bot pushed a commit that referenced this pull request Jun 7, 2022
* Fixing Windows color regression

* Cleaning up logic

* Consolidating logic to the logger

* Cleaning up vars

* Updating comment

* Removing unused import

* Fixing whitespace

* Adding changelog

(cherry picked from commit e48f7ab)
leahwicz added a commit that referenced this pull request Jun 7, 2022
* Fixing Windows color regression

* Cleaning up logic

* Consolidating logic to the logger

* Cleaning up vars

* Updating comment

* Removing unused import

* Fixing whitespace

* Adding changelog

(cherry picked from commit e48f7ab)

Co-authored-by: leahwicz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CT-582] stdout colouring broken on dbt-core on Windows
4 participants