Skip to content

Replace Colorama with internal solution for windows legacy mode #1991

@willmcgugan

Description

@willmcgugan

Colorama is a wonderful hack that wraps stdout to process ansi codes. It is problematic today because it is essentially a global setting and interferes with the ability to enable ansi escape sequences with newer Windows.

We should replace Colorama with equivalent functionality within Rich itself that generates the appropriate win32 api calls. This will hopefully be a little faster, and give us much more granular control over when to enable it.

I think we can do this at the Segment level. In Console, there is a _render_buffer method which renders segments in to text with ansi codes. I think we can generate the appropriate calls to the win32 api call here, if self.legacy_windows is True.

Since the win32 calls are only available on Windows, this should probably implemented within a conditional import. Perhaps _windows_renderer.py

See the Win32 docs for a reference on the appropriate API calls. It may also be worth referring to the colorama source.

https://docs.microsoft.com/en-us/windows/console/console-screen-buffers#character-attributes

I suggested we might have to process ansi codes to extract the win32 calls. On reflection I don't think we will need this. There is enough information in the Segment object that we don't need to parse anything. This should make it significantly faster.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions