@@ -43,16 +43,20 @@ class Textbox:
4343 def __init__ (self , win , insert_mode = False ):
4444 self .win = win
4545 self .insert_mode = insert_mode
46- (self .maxy , self .maxx ) = win .getmaxyx ()
47- self .maxy = self .maxy - 1
48- self .maxx = self .maxx - 1
46+ self ._update_max_yx ()
4947 self .stripspaces = 1
5048 self .lastcmd = None
5149 win .keypad (1 )
5250
51+ def _update_max_yx (self ):
52+ maxy , maxx = self .win .getmaxyx ()
53+ self .maxy = maxy - 1
54+ self .maxx = maxx - 1
55+
5356 def _end_of_line (self , y ):
5457 """Go to the location of the first blank on the given line,
5558 returning the index of the last non-blank character."""
59+ self ._update_max_yx ()
5660 last = self .maxx
5761 while True :
5862 if curses .ascii .ascii (self .win .inch (y , last )) != curses .ascii .SP :
@@ -64,8 +68,10 @@ def _end_of_line(self, y):
6468 return last
6569
6670 def _insert_printable_char (self , ch ):
71+ self ._update_max_yx ()
6772 (y , x ) = self .win .getyx ()
68- if y < self .maxy or x < self .maxx :
73+ backyx = None
74+ while y < self .maxy or x < self .maxx :
6975 if self .insert_mode :
7076 oldch = self .win .inch ()
7177 # The try-catch ignores the error we trigger from some curses
@@ -75,14 +81,20 @@ def _insert_printable_char(self, ch):
7581 self .win .addch (ch )
7682 except curses .error :
7783 pass
78- if self .insert_mode :
79- (backy , backx ) = self .win .getyx ()
80- if curses .ascii .isprint (oldch ):
81- self ._insert_printable_char (oldch )
82- self .win .move (backy , backx )
84+ if not self .insert_mode or not curses .ascii .isprint (oldch ):
85+ break
86+ ch = oldch
87+ (y , x ) = self .win .getyx ()
88+ # Remember where to put the cursor back since we are in insert_mode
89+ if backyx is None :
90+ backyx = y , x
91+
92+ if backyx is not None :
93+ self .win .move (* backyx )
8394
8495 def do_command (self , ch ):
8596 "Process a single editing command."
97+ self ._update_max_yx ()
8698 (y , x ) = self .win .getyx ()
8799 self .lastcmd = ch
88100 if curses .ascii .isprint (ch ):
@@ -148,6 +160,7 @@ def do_command(self, ch):
148160 def gather (self ):
149161 "Collect and return the contents of the window."
150162 result = ""
163+ self ._update_max_yx ()
151164 for y in range (self .maxy + 1 ):
152165 self .win .move (y , 0 )
153166 stop = self ._end_of_line (y )
0 commit comments