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

15 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-07 05:08 +0000

1# This file is part of the Apio project 

2# (C) 2016-present FPGAwars 

3# Project authors: 

4# * Jesús Arroyo (Jesus89) (2016-2019) 

5# * Juan Gonzalez (obijuan) (2019-2024) 

6# * The Apio Team (2024-present) 

7# License: GPLv2 

8 

9"""Functions to workaround the rich library bugs when stdout is piped out 

10on windows.""" 

11 

12import io 

13import sys 

14import platform 

15from typing import cast 

16import rich.console 

17 

18 

19def fix_windows_stdout_encoding() -> bool: 

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

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

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

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

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

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

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

27 ): 

28 cast(io.TextIOWrapper, sys.stdout).reconfigure(encoding="utf-8") 

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

30 return True 

31 # -- Else. 

32 return False 

33 

34 

35def apply_workaround(): 

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

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

38 

39 # For accessing rich.console._windows_console_features 

40 # pylint: disable=protected-access 

41 

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

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

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

45 

46 # This enables the colors. 

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

48 

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

50 # -- values. 

51 rich.console.get_windows_console_features() 

52 assert rich.console._windows_console_features is not None 

53 

54 # -- Apply the patch. 

55 rich.console._windows_console_features.vt = True 

56 rich.console._windows_console_features.truecolor = True