Skip to content

Commit feaed7c

Browse files
authored
Support older windows with colorama (#85)
Fixes #79
1 parent b000bde commit feaed7c

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Fixes
66
- Fix ansi escape codes on legacy Windows console
7-
* Issue #79, PR #80
7+
* Issue #79, PR #80, PR #85
88

99
## 1.2.0 - 2023-07-02
1010

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ changes to the code.
3030
* [Subparsers](#working-with-subparsers)
3131
* [Third party formatters](#working-with-third-party-formatters) (ft. django)
3232
* [Optparse](#optparse-support) (experimental)
33+
* [Legacy Windows](#legacy-windows-support)
3334

3435
## Installation
3536

@@ -239,3 +240,19 @@ Syntax highlighting works the same as `argparse`.
239240
Colors in the `usage` are not supported yet.
240241

241242
Customizing the group name format is not supported. optparse uses Title Case format by default.
243+
244+
## Legacy Windows support
245+
246+
If your application still runs on legacy Windows versions (older than Windows 10), you'll need to
247+
enable ANSI escape sequences by calling `colorama.init()` otherwise colors will be disabled:
248+
249+
```python
250+
import argparse
251+
import colorama
252+
from rich_argparse import RichHelpFormatter
253+
254+
colorama.init()
255+
parser = argparse.ArgumentParser(..., formatter_class=RichHelpFormatter)
256+
...
257+
```
258+
This is **not** required on Windows 10 and newer or on other operating systems.

rich_argparse/_common.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ def _initialize_win_colors() -> bool: # pragma: no cover
3434
if _windows_console_fixed is None:
3535
winver = sys.getwindowsversion() # type: ignore[attr-defined]
3636
if winver.major < 10 or winver.build < 10586:
37-
_windows_console_fixed = False
37+
try:
38+
import colorama
39+
40+
_windows_console_fixed = isinstance(sys.stdout, colorama.ansitowin32.StreamWrapper)
41+
except Exception:
42+
_windows_console_fixed = False
3843
else:
3944
import ctypes
4045

0 commit comments

Comments
 (0)