@@ -9,9 +9,10 @@ class SimpleCursesLineEdit(object):
9
9
""" Class to insert one line of text
10
10
Python 3 supports all chars
11
11
Python 2 supports ascii only
12
-
12
+
13
13
"""
14
14
string = ''
15
+ _string = ''
15
16
16
17
""" windows """
17
18
parent_win = None
@@ -57,6 +58,8 @@ def __init__(self, parent, begin_y, begin_x, **kwargs):
57
58
for key , value in kwargs .items ():
58
59
if key == 'boxed' :
59
60
self ._boxed = value
61
+ elif key == 'string' :
62
+ self ._string = value
60
63
elif key == 'string_len' :
61
64
self ._string_len = value
62
65
elif key == 'caption' :
@@ -109,6 +112,15 @@ def focused(self, val):
109
112
self ._focused = val
110
113
self .refreshEditWindow ()
111
114
115
+ @property
116
+ def string (self ):
117
+ return self ._string
118
+
119
+ @string .setter
120
+ def string (self , val ):
121
+ self ._string = val
122
+ self ._curs_pos = len (self ._string )
123
+
112
124
def getmaxyx (self ):
113
125
return self ._caption_win .getmaxyx ()
114
126
@@ -159,8 +171,8 @@ def refreshEditWindow(self):
159
171
active_cursor_color = self .caption_color
160
172
self ._edit_win .erase ()
161
173
#self._edit_win.bkgd('-', curses.A_REVERSE)
162
- if self .string :
163
- self ._edit_win .addstr (0 , 0 , self .string , active_edit_color )
174
+ if self ._string :
175
+ self ._edit_win .addstr (0 , 0 , self ._string , active_edit_color )
164
176
else :
165
177
self ._curs_pos = 0
166
178
if self .log is not None :
@@ -202,7 +214,7 @@ def keypress(self, win, char):
202
214
if char in (curses .KEY_ENTER , ord ('\n ' ), ord ('\r ' )):
203
215
""" ENTER """
204
216
if self ._has_history :
205
- self ._input_history .add_to_history (self .string )
217
+ self ._input_history .add_to_history (self ._string )
206
218
return 0
207
219
elif char in (curses .KEY_EXIT , 27 ):
208
220
self ._edit_win .nodelay (True )
@@ -212,16 +224,16 @@ def keypress(self, win, char):
212
224
self ._edit_win .nodelay (False )
213
225
if char == - 1 :
214
226
""" ESCAPE """
215
- self .string = ''
227
+ self ._string = ''
216
228
self ._curs_pos = 0
217
229
return - 1
218
230
else :
219
231
return 1
220
232
elif char in (curses .KEY_RIGHT , curses .ascii .ACK ):
221
233
""" KEY_RIGHT, Alt-F """
222
234
self ._curs_pos += 1
223
- if len (self .string ) < self ._curs_pos :
224
- self ._curs_pos = len (self .string )
235
+ if len (self ._string ) < self ._curs_pos :
236
+ self ._curs_pos = len (self ._string )
225
237
elif char in (curses .KEY_LEFT , ):
226
238
""" KEY_LEFT """
227
239
self ._curs_pos -= 1
@@ -232,15 +244,15 @@ def keypress(self, win, char):
232
244
self ._curs_pos = 0
233
245
elif char in (curses .KEY_END , curses .ascii .ENQ ):
234
246
""" KEY_END, ^E """
235
- self ._curs_pos = len (self .string )
247
+ self ._curs_pos = len (self ._string )
236
248
elif char in (curses .KEY_DC , curses .ascii .EOT ):
237
249
""" DEL key, ^D """
238
- if self ._curs_pos < len (self .string ):
239
- self .string = self .string [:self ._curs_pos ] + self .string [self ._curs_pos + 1 :]
250
+ if self ._curs_pos < len (self ._string ):
251
+ self ._string = self ._string [:self ._curs_pos ] + self ._string [self ._curs_pos + 1 :]
240
252
elif char in (curses .KEY_BACKSPACE , curses .ascii .BS ,127 ):
241
253
""" KEY_BACKSPACE """
242
254
if self ._curs_pos > 0 :
243
- self .string = self .string [:self ._curs_pos - 1 ] + self .string [self ._curs_pos :]
255
+ self ._string = self ._string [:self ._curs_pos - 1 ] + self ._string [self ._curs_pos :]
244
256
self ._curs_pos -= 1
245
257
elif char in (curses .KEY_UP , curses .ascii .DLE ):
246
258
""" KEY_UP, ^N """
@@ -301,27 +313,27 @@ def keypress(self, win, char):
301
313
curses .ungetch (char )
302
314
elif char in (curses .ascii .VT , ):
303
315
""" Ctrl-K - delete to end of line """
304
- self .string = self .string [:self ._curs_pos ]
316
+ self ._string = self ._string [:self ._curs_pos ]
305
317
elif 0 <= char <= 31 :
306
318
pass
307
319
else :
308
- if len (self .string ) + 1 == self ._max_width :
320
+ if len (self ._string ) + 1 == self ._max_width :
309
321
return 1
310
322
if version_info < (3 , 0 ):
311
323
if 32 <= char < 127 :
312
324
# accept only ascii characters
313
- if len (self .string ) == self ._curs_pos :
314
- self .string += chr (char )
325
+ if len (self ._string ) == self ._curs_pos :
326
+ self ._string += chr (char )
315
327
self ._curs_pos += 1
316
328
else :
317
- self .string = self .string [:self ._curs_pos ] + chr (char ) + self .string [self ._curs_pos :]
329
+ self ._string = self ._string [:self ._curs_pos ] + chr (char ) + self ._string [self ._curs_pos :]
318
330
else :
319
331
char = self ._get_char (win , char )
320
- if len (self .string ) == self ._curs_pos :
321
- self .string += char
332
+ if len (self ._string ) == self ._curs_pos :
333
+ self ._string += char
322
334
self ._curs_pos += 1
323
335
else :
324
- self .string = self .string [:self ._curs_pos ] + char + self .string [self ._curs_pos :]
336
+ self ._string = self ._string [:self ._curs_pos ] + char + self ._string [self ._curs_pos :]
325
337
326
338
self .refreshEditWindow ()
327
339
return 1
@@ -362,9 +374,34 @@ def get_check_next_byte():
362
374
out = '' .join ([chr (b ) for b in bytes ])
363
375
else :
364
376
buf = bytearray (bytes )
365
- out = buf .decode ('utf-8' )
377
+ out = self ._decode_string (buf )
378
+ #out = buf.decode('utf-8')
366
379
return out
367
380
381
+ def _encode_string (self , data ):
382
+ encodings = ['utf-8' , locale .getpreferredencoding (False ), 'latin1' ]
383
+ for enc in encodings :
384
+ try :
385
+ data = data .encode (enc )
386
+ except :
387
+ continue
388
+ break
389
+
390
+ assert type (data ) != bytes # Latin1 should have worked.
391
+ return data
392
+
393
+ def _decode_string (self , data ):
394
+ encodings = ['utf-8' , locale .getpreferredencoding (False ), 'latin1' ]
395
+ for enc in encodings :
396
+ try :
397
+ data = data .decode (enc )
398
+ except :
399
+ continue
400
+ break
401
+
402
+ assert type (data ) != bytes # Latin1 should have worked.
403
+ return data
404
+
368
405
def _log (self , msg ):
369
406
with open (self ._log_file , 'a' ) as log_file :
370
407
log_file .write (msg )
0 commit comments