Skip to content

Commit 6f60f10

Browse files
committed
Make width test work on Windows
1 parent f88a7f0 commit 6f60f10

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

apsw/unicode.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,8 +1689,37 @@ def finish():
16891689

16901690
tty.setraw(tty_in)
16911691
else:
1692-
tty_in = os.open("CONIN$", os.O_RDONLY)
1693-
tty_out = os.open("CONOUT$", os.WRONLY)
1692+
1693+
import ctypes, msvcrt
1694+
1695+
kernel32 = ctypes.windll.kernel32
1696+
1697+
os_tty_out = kernel32.GetStdHandle(-11)
1698+
1699+
# enable ansi processing
1700+
res = kernel32.SetConsoleMode(os_tty_out, 7)
1701+
assert res # zero means failure
1702+
1703+
# make i/o interface
1704+
class tty_out:
1705+
def write(data):
1706+
kernel32.WriteConsoleW(os_tty_out, data, len(data), None, None)
1707+
1708+
def flush():
1709+
pass
1710+
1711+
class tty_in:
1712+
def read(howmuch):
1713+
# it took several attempts to get this to work.
1714+
# if I used any other api to read from the console
1715+
# then it just blocked and the ansi position
1716+
# response was rendered on the screen
1717+
res = ""
1718+
while len(res) < howmuch:
1719+
res += msvcrt.getwch()
1720+
return res
1721+
1722+
# fake out wcwidth
16941723
class dummy:
16951724
def wcswidth(*args):
16961725
return 1

0 commit comments

Comments
 (0)