33import blessed
44
55def is_unicode_supported () -> bool :
6+ if hasattr (is_unicode_supported , '_cached' ):
7+ return is_unicode_supported ._cached
8+
69 if sys .platform == 'win32' and not os .environ .get ('WT_SESSION' ):
10+ is_unicode_supported ._cached = False
711 return False # legacy Win consoles don't suport wide chars
12+
13+ # Measure rendered width of wide CJK char
814 terminal = blessed .Terminal ()
9- with terminal .cbreak (), terminal .hidden_cursor (): # measure rendered width of wide CJK char
15+ with terminal .cbreak (), terminal .hidden_cursor ():
1016 r ,g ,b = terminal .get_bgcolor ()
11- if (r ,g ,b ) == (- 1 ,- 1 ,- 1 ) : ( r , g , b ) = ( 0 , 0 , 0 ) # get bg failed, init to black
17+ if (r ,g ,b ) == (- 1 ,- 1 ,- 1 ) : r = g = b = 0 # get bg failed, init to black
1218 sys .stdout .write (terminal .color_rgb (r ,g ,b )) # make text invisible
1319 _ , x1 = terminal .get_location ()
1420 sys .stdout .write ('𠀀' )
@@ -17,4 +23,6 @@ def is_unicode_supported() -> bool:
1723 sys .stdout .write ('\b ' * (x2 - x1 ) + ' ' * (x2 - x1 ) + '\b ' * (x2 - x1 ))
1824 sys .stdout .flush ()
1925 sys .stdout .write (terminal .normal ) # restore text colors
20- return x2 - x1 > 1 # terminal rendered wide char as 2 col
26+ result = x2 - x1 > 1 # terminal rendered wide char as 2 col
27+ is_unicode_supported ._cached = result
28+ return result
0 commit comments