2021-05-16 18:00:23 +02:00
|
|
|
"""Fork of Python's standard library curses.textpad module.
|
|
|
|
|
|
|
|
I guess it requires some license header?
|
|
|
|
|
|
|
|
Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
|
|
|
|
2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021
|
|
|
|
Python Software Foundation;
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
This version fixes a few quirks of the standard module, namely:
|
|
|
|
|
|
|
|
- Discard multi-lines mode: only one line is supported.
|
2021-05-16 19:42:05 +02:00
|
|
|
- Moving in line more reasonably: no more going alone rightward.
|
2021-05-18 12:15:10 +02:00
|
|
|
- Handle Unicode
|
2021-05-16 18:00:23 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import curses
|
|
|
|
import curses.ascii
|
|
|
|
|
|
|
|
|
|
|
|
class Textbox:
|
|
|
|
"""Editing widget using the interior of a window object.
|
|
|
|
|
|
|
|
- Ctrl-A: Go to left edge of window.
|
|
|
|
- Ctrl-B: Cursor left, wrapping to previous line if appropriate.
|
|
|
|
- Ctrl-D: Delete character under cursor.
|
|
|
|
- Ctrl-E: Go to right edge (stripspaces off) or end of line (stripspaces on).
|
|
|
|
- Ctrl-F: Cursor right, wrapping to next line when appropriate.
|
|
|
|
- Ctrl-H: Delete character backward.
|
|
|
|
- Ctrl-K: If line is blank, delete it, otherwise clear to end of line.
|
|
|
|
- Ctrl-L: Refresh screen.
|
|
|
|
|
|
|
|
Move operations do nothing if the cursor is at an edge where the movement
|
|
|
|
is not possible. The following synonyms are supported where possible:
|
|
|
|
|
|
|
|
- KEY_LEFT: Ctrl-B
|
|
|
|
- KEY_RIGHT: Ctrl-F
|
|
|
|
- KEY_UP: Ctrl-P
|
|
|
|
- KEY_DOWN: Ctrl-N
|
|
|
|
- KEY_BACKSPACE: Ctrl-H
|
|
|
|
"""
|
|
|
|
def __init__(self, win, insert_mode=False):
|
|
|
|
self.win = win
|
|
|
|
self.insert_mode = insert_mode
|
2021-05-16 19:27:54 +02:00
|
|
|
self._update_maxx()
|
2021-05-16 18:00:23 +02:00
|
|
|
self.stripspaces = True
|
|
|
|
win.keypad(1)
|
|
|
|
|
2021-05-16 19:27:54 +02:00
|
|
|
def _update_maxx(self):
|
|
|
|
_, maxx = self.win.getmaxyx()
|
2021-05-16 18:00:23 +02:00
|
|
|
self.maxx = maxx - 1
|
|
|
|
|
|
|
|
def _end_of_line(self):
|
2021-05-16 19:27:54 +02:00
|
|
|
"""Return the index of the last non-blank character."""
|
|
|
|
self._update_maxx()
|
2021-05-16 18:00:23 +02:00
|
|
|
last = self.maxx
|
|
|
|
while True:
|
2021-05-16 19:27:54 +02:00
|
|
|
last_ch = curses.ascii.ascii(self.win.inch(0, last))
|
|
|
|
if last_ch != curses.ascii.SP:
|
2021-05-16 18:00:23 +02:00
|
|
|
last = min(self.maxx, last + 1)
|
|
|
|
break
|
|
|
|
elif last == 0:
|
|
|
|
break
|
|
|
|
last = last - 1
|
|
|
|
return last
|
|
|
|
|
|
|
|
def _insert_printable_char(self, ch):
|
2021-05-16 19:27:54 +02:00
|
|
|
self._update_maxx()
|
|
|
|
_, x = self.win.getyx()
|
|
|
|
backx = None
|
2021-05-16 18:00:23 +02:00
|
|
|
while x < self.maxx:
|
|
|
|
oldch = 0
|
|
|
|
if self.insert_mode:
|
|
|
|
oldch = self.win.inch()
|
|
|
|
# The try-catch ignores the error we trigger from some curses
|
|
|
|
# versions by trying to write into the lowest-rightmost spot
|
|
|
|
# in the window.
|
|
|
|
try:
|
|
|
|
self.win.addch(ch)
|
|
|
|
except curses.error:
|
|
|
|
pass
|
|
|
|
if not self.insert_mode or not curses.ascii.isprint(oldch):
|
|
|
|
break
|
|
|
|
ch = oldch
|
2021-05-16 19:27:54 +02:00
|
|
|
_, x = self.win.getyx()
|
|
|
|
# Remember where to put the cursor back since we are in insert_mode.
|
|
|
|
if backx is None:
|
|
|
|
backx = x
|
2021-05-16 18:00:23 +02:00
|
|
|
|
2021-05-16 19:27:54 +02:00
|
|
|
if backx is not None:
|
|
|
|
self.win.move(0, backx)
|
2021-05-16 18:00:23 +02:00
|
|
|
|
2021-05-18 12:15:10 +02:00
|
|
|
COMMAND_KEYS = (curses.KEY_LEFT, curses.KEY_RIGHT, curses.KEY_BACKSPACE)
|
|
|
|
|
2021-05-16 18:00:23 +02:00
|
|
|
def do_command(self, ch):
|
|
|
|
"""Process a single editing command."""
|
2021-05-16 19:27:54 +02:00
|
|
|
self._update_maxx()
|
2021-05-16 18:00:23 +02:00
|
|
|
_, x = self.win.getyx()
|
2021-05-18 12:15:10 +02:00
|
|
|
if curses.ascii.iscntrl(ch) or ch in self.COMMAND_KEYS:
|
|
|
|
return self.do_control(ch, x)
|
|
|
|
if x < self.maxx:
|
|
|
|
curses.ungetch(ch)
|
|
|
|
ch = self.win.get_wch()
|
|
|
|
self._insert_printable_char(ch)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def do_control(self, ch, x):
|
|
|
|
"""Process a control character."""
|
|
|
|
if ch == curses.ascii.NL:
|
2021-05-16 18:00:23 +02:00
|
|
|
return 0
|
|
|
|
elif ch == curses.ascii.SOH: # ^a
|
|
|
|
self.win.move(0, 0)
|
|
|
|
elif ch in (
|
|
|
|
curses.ascii.STX,
|
|
|
|
curses.KEY_LEFT,
|
|
|
|
curses.ascii.BS,
|
|
|
|
curses.KEY_BACKSPACE
|
|
|
|
):
|
|
|
|
if x > 0:
|
|
|
|
self.win.move(0, x - 1)
|
|
|
|
if ch in (curses.ascii.BS, curses.KEY_BACKSPACE):
|
|
|
|
self.win.delch()
|
|
|
|
elif ch == curses.ascii.EOT: # ^d
|
|
|
|
self.win.delch()
|
|
|
|
elif ch == curses.ascii.ENQ: # ^e
|
|
|
|
if self.stripspaces:
|
|
|
|
self.win.move(0, self._end_of_line())
|
|
|
|
else:
|
|
|
|
self.win.move(0, self.maxx)
|
|
|
|
elif ch in (curses.ascii.ACK, curses.KEY_RIGHT): # ^f
|
|
|
|
if x < self.maxx:
|
2021-05-16 19:42:05 +02:00
|
|
|
self.win.move(0, min(x + 1, self._end_of_line()))
|
2021-05-16 18:00:23 +02:00
|
|
|
elif ch == curses.ascii.VT: # ^k
|
|
|
|
if x == 0 and self._end_of_line() == 0:
|
|
|
|
self.win.deleteln()
|
|
|
|
else:
|
|
|
|
# First undo the effect of self._end_of_line.
|
|
|
|
self.win.move(0, x)
|
|
|
|
self.win.clrtoeol()
|
|
|
|
elif ch == curses.ascii.FF: # ^l
|
|
|
|
self.win.refresh()
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def gather(self):
|
|
|
|
"""Collect and return the contents of the window."""
|
|
|
|
result = ""
|
2021-05-16 19:27:54 +02:00
|
|
|
self._update_maxx()
|
2021-05-16 18:00:23 +02:00
|
|
|
stop = self._end_of_line()
|
|
|
|
for x in range(self.maxx + 1):
|
|
|
|
if self.stripspaces and x > stop:
|
|
|
|
break
|
2021-05-18 12:15:10 +02:00
|
|
|
result += chr(self.win.inch(0, x))
|
2021-05-16 18:00:23 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
def edit(self, validate=None):
|
|
|
|
"""Edit in the widget window and collect the results."""
|
|
|
|
while True:
|
|
|
|
ch = self.win.getch()
|
|
|
|
if validate:
|
|
|
|
ch = validate(ch)
|
|
|
|
if not ch:
|
|
|
|
continue
|
|
|
|
if not self.do_command(ch):
|
|
|
|
break
|
|
|
|
self.win.refresh()
|
|
|
|
return self.gather()
|