Coverage for apio/common/rich_lib_windows.py: 53%

13 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-06 10:20 +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.""" 

12 

13import sys 

14import platform 

15import rich.console 

16 

17 

18def fix_windows_stdout_encoding() -> bool: 

19 """Called on the apio process (parent) to fix its output encoding. 

20 Safe to call on non windows platforms. Returns True if fixed.""" 

21 # -- This takes care of the table graphic box. 

22 # -- See https://github.com/Textualize/rich/issues/3625 

23 if ( 23 ↛ 27line 23 didn't jump to line 27 because the condition on line 23 was never true

24 platform.system().lower() == "windows" 

25 and sys.stdout.encoding != "utf-8" 

26 ): 

27 sys.stdout.reconfigure(encoding="utf-8") 

28 return True 

29 # -- Else. 

30 return False 

31 

32 

33def apply_workaround(): 

34 """Called on the scons (child) process side, when running on windows, 

35 to apply the the workaround for the rich library.""" 

36 

37 # For accessing rich.console._windows_console_features 

38 # pylint: disable=protected-access 

39 

40 # -- This takes care of the table graphic box. 

41 # -- See https://github.com/Textualize/rich/issues/3625 

42 # sys.stdout.reconfigure(encoding=params.stdout_encoding) 

43 

44 # This enables the colors. 

45 # See https://github.com/Textualize/rich/issues/3082 

46 

47 # -- Make sure that _windows_console_features is initialized with cached 

48 # -- values. 

49 rich.console.get_windows_console_features() 

50 assert rich.console._windows_console_features is not None 

51 

52 # -- Apply the patch. 

53 rich.console._windows_console_features.vt = True 

54 rich.console._windows_console_features.truecolor = True