Skip to content

Commit 49a982c

Browse files
committed
fix encoding issues on windows cli
1 parent 3cf0d7d commit 49a982c

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

video_cli.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@
3838
# Ctrl+Z to exit
3939
eof = '\x1A'
4040

41-
def term_echo(on=True):
41+
def init():
42+
if not check_tty():
43+
return False
44+
# on windows, clear the screen or we get a messy console.
45+
wconio.clrscr()
46+
return True
47+
48+
def close():
4249
pass
4350

4451
def getc():
@@ -47,9 +54,9 @@ def getc():
4754
return ''
4855
return msvcrt.getch()
4956

50-
def replace_scancodes(s):
57+
def get_scancode(s):
5158
# windows scancodes should be the same as gw-basic ones
52-
if len(s) == 0 and s[0] in ('\xe0', '\0'):
59+
if len(s) > 1 and s[0] in ('\xe0', '\0'):
5360
return ord(s[1])
5461
else:
5562
raise KeyError
@@ -84,8 +91,9 @@ def putc_at(pagenum, row, col, c, for_keys=False):
8491
if for_keys:
8592
return
8693
update_position(row, col)
87-
# Windows CMD doesn't do UTF8, output raw & set codepage with CHCP
88-
wconio.putch(c)
94+
# output in cli codepage
95+
uc = unicodepage.UTF8Converter().to_utf8(c).decode('utf-8')
96+
wconio.putch(uc.encode(sys.stdout.encoding, 'replace'))
8997
last_col += 1
9098

9199
def putwc_at(pagenum, row, col, c, d, for_keys=False):
@@ -94,8 +102,9 @@ def putwc_at(pagenum, row, col, c, d, for_keys=False):
94102
return
95103
update_position(row, col)
96104
# Windows CMD doesn't do UTF8, output raw & set codepage with CHCP
97-
wconio.putch(c)
98-
wconio.putch(d)
105+
# output in cli codepage
106+
uc = unicodepage.UTF8Converter().to_utf8(c+d).decode('utf-8')
107+
wconio.putch(uc.encode(sys.stdout.encoding, 'replace'))
99108
last_col += 2
100109

101110
term = WinTerm()
@@ -168,28 +177,33 @@ def putwc_at(pagenum, row, col, c, d, for_keys=False):
168177
term.flush()
169178
last_col += 2
170179

171-
def prepare(args):
172-
pass
180+
def init():
181+
if not check_tty():
182+
return False
183+
term_echo(False)
184+
term.flush()
185+
return True
186+
187+
def close():
188+
term_echo()
189+
term.flush()
173190

174-
def init():
191+
def check_tty():
175192
if not plat.stdin_is_tty:
176193
logging.warning('Input device is not a terminal. '
177194
'Could not initialise cli interface.')
178195
return False
179-
term_echo(False)
180-
term.flush()
181196
return True
182-
197+
198+
def prepare(args):
199+
pass
200+
183201
def supports_graphics_mode(mode_info):
184202
return False
185203

186204
def init_screen_mode(mode_info):
187205
pass
188206

189-
def close():
190-
term_echo()
191-
term.flush()
192-
193207
def idle():
194208
time.sleep(0.024)
195209

@@ -258,7 +272,7 @@ def check_keyboard():
258272
except KeyError:
259273
# replace utf-8 with codepage
260274
# convert into unicode codepoints
261-
u = s.decode('utf-8')
275+
u = s.decode(sys.stdin.encoding)
262276
# then handle these one by one as UTF-8 sequences
263277
c = ''
264278
for uc in u:

0 commit comments

Comments
 (0)