Coverage for apio/common/rich_lib_windows.py: 59%
15 statements
« prev ^ index » next coverage.py v7.14.3, created at 2026-06-24 03:51 +0000
« prev ^ index » next coverage.py v7.14.3, created at 2026-06-24 03:51 +0000
1# -*- coding: utf-8 -*-
2# -- This file is part of the Apio project
3# -- (C) 2016-2018 FPGAwars
4# -- Author Jesús Arroyo
5# -- License GPLv2
6# -- Derived from:
7# ---- Platformio project
8# ---- (C) 2014-2016 Ivan Kravets <me@ikravets.com>
9# ---- License Apache v2
10"""Functions to workaround the rich library bugs when stdout is piped out
11on windows."""
13import io
14import sys
15import platform
16from typing import cast
17import rich.console
20def fix_windows_stdout_encoding() -> bool:
21 """Called on the apio process (parent) to fix its output encoding.
22 Safe to call on non windows platforms. Returns True if fixed."""
23 # -- This takes care of the table graphic box.
24 # -- See https://github.com/Textualize/rich/issues/3625
25 if ( 25 ↛ 29line 25 didn't jump to line 29 because the condition on line 25 was never true
26 platform.system().lower() == "windows"
27 and sys.stdout.encoding != "utf-8"
28 ):
29 cast(io.TextIOWrapper, sys.stdout).reconfigure(encoding="utf-8")
30 # sys.stdout.reconfigure(encoding="utf-8")
31 return True
32 # -- Else.
33 return False
36def apply_workaround():
37 """Called on the scons (child) process side, when running on windows,
38 to apply the the workaround for the rich library."""
40 # For accessing rich.console._windows_console_features
41 # pylint: disable=protected-access
43 # -- This takes care of the table graphic box.
44 # -- See https://github.com/Textualize/rich/issues/3625
45 # sys.stdout.reconfigure(encoding=params.stdout_encoding)
47 # This enables the colors.
48 # See https://github.com/Textualize/rich/issues/3082
50 # -- Make sure that _windows_console_features is initialized with cached
51 # -- values.
52 rich.console.get_windows_console_features()
53 assert rich.console._windows_console_features is not None
55 # -- Apply the patch.
56 rich.console._windows_console_features.vt = True
57 rich.console._windows_console_features.truecolor = True