Skip to content

Commit d4d7a2d

Browse files
committed
Cached is_unicode_supported result, condensed RGB fallback
1 parent f2276bb commit d4d7a2d

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

  • is-unicode-supported/src/is_unicode_supported

is-unicode-supported/src/is_unicode_supported/api.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
import blessed
44

55
def 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

Comments
 (0)