38
38
# Ctrl+Z to exit
39
39
eof = '\x1A '
40
40
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 ():
42
49
pass
43
50
44
51
def getc ():
@@ -47,9 +54,9 @@ def getc():
47
54
return ''
48
55
return msvcrt .getch ()
49
56
50
- def replace_scancodes (s ):
57
+ def get_scancode (s ):
51
58
# 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 ' ):
53
60
return ord (s [1 ])
54
61
else :
55
62
raise KeyError
@@ -84,8 +91,9 @@ def putc_at(pagenum, row, col, c, for_keys=False):
84
91
if for_keys :
85
92
return
86
93
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' ))
89
97
last_col += 1
90
98
91
99
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):
94
102
return
95
103
update_position (row , col )
96
104
# 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' ))
99
108
last_col += 2
100
109
101
110
term = WinTerm ()
@@ -168,28 +177,33 @@ def putwc_at(pagenum, row, col, c, d, for_keys=False):
168
177
term .flush ()
169
178
last_col += 2
170
179
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 ()
173
190
174
- def init ():
191
+ def check_tty ():
175
192
if not plat .stdin_is_tty :
176
193
logging .warning ('Input device is not a terminal. '
177
194
'Could not initialise cli interface.' )
178
195
return False
179
- term_echo (False )
180
- term .flush ()
181
196
return True
182
-
197
+
198
+ def prepare (args ):
199
+ pass
200
+
183
201
def supports_graphics_mode (mode_info ):
184
202
return False
185
203
186
204
def init_screen_mode (mode_info ):
187
205
pass
188
206
189
- def close ():
190
- term_echo ()
191
- term .flush ()
192
-
193
207
def idle ():
194
208
time .sleep (0.024 )
195
209
@@ -258,7 +272,7 @@ def check_keyboard():
258
272
except KeyError :
259
273
# replace utf-8 with codepage
260
274
# convert into unicode codepoints
261
- u = s .decode ('utf-8' )
275
+ u = s .decode (sys . stdin . encoding )
262
276
# then handle these one by one as UTF-8 sequences
263
277
c = ''
264
278
for uc in u :
0 commit comments