textbox: fix double delete of trailing spaces
This commit is contained in:
parent
d07d4b3388
commit
5829e7e267
|
@ -103,9 +103,10 @@ class CommandLine:
|
||||||
to handle the keys above.
|
to handle the keys above.
|
||||||
"""
|
"""
|
||||||
if ch == curses.KEY_BACKSPACE: # Cancel input if all line is cleaned.
|
if ch == curses.KEY_BACKSPACE: # Cancel input if all line is cleaned.
|
||||||
text = self.gather()
|
_, x = self.textbox.win.getyx()
|
||||||
if len(text) == 0:
|
if x == 1:
|
||||||
raise EscapeCommandInterrupt()
|
raise EscapeCommandInterrupt()
|
||||||
|
pass
|
||||||
elif ch == curses.ascii.ESC: # Could be ESC or ALT
|
elif ch == curses.ascii.ESC: # Could be ESC or ALT
|
||||||
self.window.nodelay(True)
|
self.window.nodelay(True)
|
||||||
ch = self.window.getch()
|
ch = self.window.getch()
|
||||||
|
|
|
@ -41,24 +41,23 @@ class Textbox:
|
||||||
def __init__(self, win, insert_mode=False):
|
def __init__(self, win, insert_mode=False):
|
||||||
self.win = win
|
self.win = win
|
||||||
self.insert_mode = insert_mode
|
self.insert_mode = insert_mode
|
||||||
self._update_max_yx()
|
self._update_maxx()
|
||||||
self.stripspaces = True
|
self.stripspaces = True
|
||||||
self.lastcmd = None
|
|
||||||
win.keypad(1)
|
win.keypad(1)
|
||||||
|
|
||||||
def _update_max_yx(self):
|
def _update_maxx(self):
|
||||||
maxy, maxx = self.win.getmaxyx()
|
_, maxx = self.win.getmaxyx()
|
||||||
self.maxy = maxy - 1
|
|
||||||
self.maxx = maxx - 1
|
self.maxx = maxx - 1
|
||||||
|
|
||||||
def _end_of_line(self):
|
def _end_of_line(self):
|
||||||
"""Go to the location of the first blank."""
|
"""Return the index of the last non-blank character."""
|
||||||
logging.debug(f"_end_of_line")
|
logging.debug(f"_end_of_line stripspaces {self.stripspaces}")
|
||||||
self._update_max_yx()
|
self._update_maxx()
|
||||||
logging.debug(f"_end_of_line {self.win.getyx()} {(self.maxx, self.maxy)}")
|
|
||||||
last = self.maxx
|
last = self.maxx
|
||||||
while True:
|
while True:
|
||||||
if curses.ascii.ascii(self.win.inch(0, last)) != curses.ascii.SP:
|
last_ch = curses.ascii.ascii(self.win.inch(0, last))
|
||||||
|
logging.debug("last_ch " + str(last_ch))
|
||||||
|
if last_ch != curses.ascii.SP:
|
||||||
last = min(self.maxx, last + 1)
|
last = min(self.maxx, last + 1)
|
||||||
break
|
break
|
||||||
elif last == 0:
|
elif last == 0:
|
||||||
|
@ -68,9 +67,9 @@ class Textbox:
|
||||||
|
|
||||||
def _insert_printable_char(self, ch):
|
def _insert_printable_char(self, ch):
|
||||||
logging.debug(f"_insert_printable_char {ch}")
|
logging.debug(f"_insert_printable_char {ch}")
|
||||||
self._update_max_yx()
|
self._update_maxx()
|
||||||
y, x = self.win.getyx()
|
_, x = self.win.getyx()
|
||||||
backyx = None
|
backx = None
|
||||||
while x < self.maxx:
|
while x < self.maxx:
|
||||||
oldch = 0
|
oldch = 0
|
||||||
if self.insert_mode:
|
if self.insert_mode:
|
||||||
|
@ -85,20 +84,19 @@ class Textbox:
|
||||||
if not self.insert_mode or not curses.ascii.isprint(oldch):
|
if not self.insert_mode or not curses.ascii.isprint(oldch):
|
||||||
break
|
break
|
||||||
ch = oldch
|
ch = oldch
|
||||||
y, x = self.win.getyx()
|
_, x = self.win.getyx()
|
||||||
# Remember where to put the cursor back since we are in insert_mode
|
# Remember where to put the cursor back since we are in insert_mode.
|
||||||
if backyx is None:
|
if backx is None:
|
||||||
backyx = y, x
|
backx = x
|
||||||
|
|
||||||
if backyx is not None:
|
if backx is not None:
|
||||||
self.win.move(*backyx)
|
self.win.move(0, backx)
|
||||||
|
|
||||||
def do_command(self, ch):
|
def do_command(self, ch):
|
||||||
"""Process a single editing command."""
|
"""Process a single editing command."""
|
||||||
logging.debug(f"do_command {ch}")
|
logging.debug(f"do_command {ch}")
|
||||||
self._update_max_yx()
|
self._update_maxx()
|
||||||
_, x = self.win.getyx()
|
_, x = self.win.getyx()
|
||||||
self.lastcmd = ch
|
|
||||||
if curses.ascii.isprint(ch):
|
if curses.ascii.isprint(ch):
|
||||||
if x < self.maxx:
|
if x < self.maxx:
|
||||||
self._insert_printable_char(ch)
|
self._insert_printable_char(ch)
|
||||||
|
@ -141,15 +139,13 @@ class Textbox:
|
||||||
def gather(self):
|
def gather(self):
|
||||||
"""Collect and return the contents of the window."""
|
"""Collect and return the contents of the window."""
|
||||||
result = ""
|
result = ""
|
||||||
self._update_max_yx()
|
self._update_maxx()
|
||||||
self.win.move(0, 0)
|
self.win.move(0, 0)
|
||||||
stop = self._end_of_line()
|
stop = self._end_of_line()
|
||||||
for x in range(self.maxx + 1):
|
for x in range(self.maxx + 1):
|
||||||
if self.stripspaces and x > stop:
|
if self.stripspaces and x > stop:
|
||||||
break
|
break
|
||||||
result = result + chr(curses.ascii.ascii(self.win.inch(0, x)))
|
result = result + chr(curses.ascii.ascii(self.win.inch(0, x)))
|
||||||
if self.maxy > 0:
|
|
||||||
result = result + "\n"
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def edit(self, validate=None):
|
def edit(self, validate=None):
|
||||||
|
|
Reference in a new issue