def _insert_printable_char(self, ch):
(y, x) = self.win.getyx()
if y < self.maxy or x < self.maxx:
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 self.insert_mode:
(backy, backx) = self.win.getyx()
if curses.ascii.isprint(oldch):
self._insert_printable_char(oldch)
self.win.move(backy, backx)
python类ascii()的实例源码
def _insert_printable_char(self, ch):
(y, x) = self.win.getyx()
if y < self.maxy or x < self.maxx:
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 self.insert_mode:
(backy, backx) = self.win.getyx()
if curses.ascii.isprint(oldch):
self._insert_printable_char(oldch)
self.win.move(backy, backx)
def _insert_printable_char(self, ch):
(y, x) = self.win.getyx()
if y < self.maxy or x < self.maxx:
# Possible reference before assignment fix
oldch = None
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 self.insert_mode:
(backy, backx) = self.win.getyx()
if curses.ascii.isprint(oldch):
self._insert_printable_char(oldch)
self.win.move(backy, backx)
def _insert_printable_char(self, ch):
(y, x) = self.win.getyx()
if y < self.maxy or x < self.maxx:
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 self.insert_mode:
(backy, backx) = self.win.getyx()
if curses.ascii.isprint(oldch):
self._insert_printable_char(oldch)
self.win.move(backy, backx)
def _insert_printable_char(self, ch):
(y, x) = self.win.getyx()
if y < self.maxy or x < self.maxx:
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 self.insert_mode:
(backy, backx) = self.win.getyx()
if curses.ascii.isprint(oldch):
self._insert_printable_char(oldch)
self.win.move(backy, backx)
def _insert_printable_char(self, ch):
(y, x) = self.win.getyx()
if y < self.maxy or x < self.maxx:
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 self.insert_mode:
(backy, backx) = self.win.getyx()
if curses.ascii.isprint(oldch):
self._insert_printable_char(oldch)
self.win.move(backy, backx)
def _insert_printable_char(self, ch):
(y, x) = self.win.getyx()
if y < self.maxy or x < self.maxx:
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 self.insert_mode:
(backy, backx) = self.win.getyx()
if curses.ascii.isprint(oldch):
self._insert_printable_char(oldch)
self.win.move(backy, backx)
def _insert_printable_char(self, ch):
(y, x) = self.win.getyx()
if y < self.maxy or x < self.maxx:
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 self.insert_mode:
(backy, backx) = self.win.getyx()
if curses.ascii.isprint(oldch):
self._insert_printable_char(oldch)
self.win.move(backy, backx)
def setTextBuffer(self, textBuffer):
app.controller.Controller.setTextBuffer(self, textBuffer)
normalCommandSet = {
ord('^'): textBuffer.cursorStartOfLine,
ord('$'): textBuffer.cursorEndOfLine,
ord('h'): textBuffer.cursorLeft,
ord('i'): self.switchToCommandSetInsert,
ord('j'): textBuffer.cursorDown,
ord('k'): textBuffer.cursorUp,
ord('l'): textBuffer.cursorRight,
}
self.commandSet = normalCommandSet
self.commandSet_Insert = {
curses.ascii.ESC: self.switchToCommandSetNormal,
}
self.commandDefault = self.textBuffer.insertPrintable
def _insert_printable_char(self, ch):
(y, x) = self.win.getyx()
if y < self.maxy or x < self.maxx:
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 self.insert_mode:
(backy, backx) = self.win.getyx()
if curses.ascii.isprint(oldch):
self._insert_printable_char(oldch)
self.win.move(backy, backx)
def CTRL(key):
return curses.ascii.ctrl(key)
def keys_init_noedit(self):
"""Define methods for each key for non-editing mode.
"""
self.keys = {
curses.ascii.ETX: self.close,
curses.KEY_DOWN: self.down_noedit,
CTRL('n'): self.down_noedit,
'j': self.down_noedit,
curses.KEY_F1: self.help,
curses.KEY_NPAGE: self.page_down,
'J': self.page_down,
CTRL('f'): self.page_up,
curses.KEY_PPAGE: self.page_up,
'K': self.page_up,
CTRL('b'): self.page_up,
CTRL('x'): self.quit,
curses.KEY_F2: self.quit,
curses.KEY_F3: self.quit_nosave,
curses.ascii.ESC: self.quit_nosave,
curses.KEY_RESIZE: self.resize,
-1: self.resize,
curses.KEY_UP: self.up_noedit,
CTRL('p'): self.up_noedit,
'k': self.up_noedit,
}
def _end_of_line(self, y):
"""Go to the location of the first blank on the given line,
returning the index of the last non-blank character."""
last = self.maxx
while True:
if curses.ascii.ascii(self.win.inch(y, last)) != curses.ascii.SP:
last = min(self.maxx, last+1)
break
elif last == 0:
break
last = last - 1
return last
def gather(self):
"Collect and return the contents of the window."
result = ""
for y in range(self.maxy+1):
self.win.move(y, 0)
stop = self._end_of_line(y)
if stop == 0 and self.stripspaces:
continue
for x in range(self.maxx+1):
if self.stripspaces and x > stop:
break
result = result + chr(curses.ascii.ascii(self.win.inch(y, x)))
if self.maxy > 0:
result = result + "\n"
return result
def __init__(self, win, insert_mode=False, text=''):
Textbox.__init__(self, win, insert_mode)
for chr in text:
if chr == '\n':
Textbox.do_command(self, curses.ascii.NL)
else:
Textbox.do_command(self, chr)
def _end_of_line(self, y):
"""Go to the location of the first blank on the given line,
returning the index of the last non-blank character."""
last = self.maxx
while True:
if curses.ascii.ascii(self.win.inch(y, last)) != curses.ascii.SP:
last = min(self.maxx, last+1)
break
elif last == 0:
break
last = last - 1
return last
def gather(self):
"Collect and return the contents of the window."
result = ""
for y in range(self.maxy+1):
self.win.move(y, 0)
stop = self._end_of_line(y)
if stop == 0 and self.stripspaces:
continue
for x in range(self.maxx+1):
if self.stripspaces and x > stop:
break
result = result + chr(curses.ascii.ascii(self.win.inch(y, x)))
if self.maxy > 0:
result = result + "\n"
return result
def test_menu_textentryitem__eot_deletion(self):
"""Test that the input_caller returns the expected string value."""
curses.initscr()
curses.ungetch("\n")
curses.ungetch("a")
curses.ungetch(curses.ascii.EOT)
curses.ungetch(curses.ascii.EOT)
curses.ungetch(curses.KEY_LEFT)
curses.ungetch(curses.ascii.EOT)
curses.ungetch(curses.KEY_LEFT)
curses.ungetch("a")
curses.ungetch("a")
self.textentryitem()
self.assertEqual(self.textentryitem.get_value(), "a")
def test_menu_textentryitem__ascii_bel(self):
"""Test that the input_caller returns the expected string value."""
curses.initscr()
curses.ungetch("\n")
curses.ungetch(curses.ascii.BEL)
# BEL is a non-printable character so the return value should be an empty string
self.textentryitem()
self.assertEqual(self.textentryitem.get_value(), "")
def _end_of_line(self, y):
"""Go to the location of the first blank on the given line,
returning the index of the last non-blank character."""
last = self.maxx
while True:
if curses.ascii.ascii(self.win.inch(y, last)) != curses.ascii.SP:
last = min(self.maxx, last + 1)
break
elif last == 0:
break
last -= 1
return last
def gather(self):
"""Collect and return the contents of the window."""
result = ""
for y in range(self.maxy + 1):
self.win.move(y, 0)
stop = self._end_of_line(y)
if stop == 0 and self.stripspaces:
continue
for x in range(self.maxx + 1):
if self.stripspaces and x > stop:
break
result += chr(curses.ascii.ascii(self.win.inch(y, x)))
if self.maxy > 0:
result += "\n"
return result
def _end_of_line(self, y):
"""Go to the location of the first blank on the given line,
returning the index of the last non-blank character."""
last = self.maxx
while True:
if curses.ascii.ascii(self.win.inch(y, last)) != curses.ascii.SP:
last = min(self.maxx, last+1)
break
elif last == 0:
break
last = last - 1
return last
def gather(self):
"Collect and return the contents of the window."
result = ""
for y in range(self.maxy+1):
self.win.move(y, 0)
stop = self._end_of_line(y)
if stop == 0 and self.stripspaces:
continue
for x in range(self.maxx+1):
if self.stripspaces and x > stop:
break
result = result + chr(curses.ascii.ascii(self.win.inch(y, x)))
if self.maxy > 0:
result = result + "\n"
return result
def get_key(self):
code = self.win.getch()
if code > 0:
# Accept newline and carriage return as enter key
if code == curses.ascii.NL or code == curses.ascii.CR:
code = curses.KEY_ENTER
# Accept newline and carriage return as enter key
if code == curses.ascii.BS:
code = curses.KEY_BACKSPACE
return code
def on_key(self, ch):
x = self.pos
# ascii 32-126 (inclusive)
if curses.ascii.isprint(ch):
if len(self.value) < self.max_length:
self.value = self.value[:x] + chr(ch) + self.value[x:]
self.pos += 1
elif ch == curses.KEY_LEFT:
if x > 0:
self.pos -= 1
elif ch == curses.KEY_RIGHT:
if x < len(self.value):
self.pos += 1
elif ch == curses.KEY_BACKSPACE:
if x > 0:
self.value = self.value[:x - 1] + self.value[x:]
self.pos -= 1
elif ch == curses.KEY_DC:
if x < len(self.value):
self.value = self.value[:x] + self.value[x + 1:]
elif ch == curses.KEY_HOME:
self.pos = 0
elif ch == curses.KEY_END:
self.pos = len(self.value)
else:
return False
self.update()
return True
def on_key(self, ch):
if not self.active:
return
if self.selected:
if ch in (curses.ascii.ESC, curses.KEY_BACKSPACE):
self.cancel_selection()
elif ch == curses.KEY_LEFT:
if self.selection > 0 and self.selection < 4 or self.selection > 4:
self.set_selection(self.selection - 1)
elif ch == curses.KEY_RIGHT:
if self.selection < 3 or self.selection > 3 and self.selection < 7:
self.set_selection(self.selection + 1)
elif ch == curses.KEY_UP:
if self.selection > 3:
self.set_selection(self.selection - 4)
elif ch == curses.KEY_DOWN:
if self.selection < 4 and self.selected_source == 'hand':
self.set_selection(self.selection + 4)
elif ch in (curses.KEY_ENTER, curses.ascii.SP):
self.confirm_selection()
else:
if ch == curses.KEY_LEFT:
if self.selection > 0:
self.set_selection(self.selection - 1)
elif ch == curses.KEY_RIGHT:
if self.selection + 1 < len(self.selectable_sources):
self.set_selection(self.selection + 1)
elif ch in (curses.KEY_ENTER, curses.ascii.SP):
self.confirm_selection()
def _end_of_line(self, y):
"""Go to the location of the first blank on the given line,
returning the index of the last non-blank character."""
last = self.maxx
while True:
if curses.ascii.ascii(self.win.inch(y, last)) != curses.ascii.SP:
last = min(self.maxx, last+1)
break
elif last == 0:
break
last = last - 1
return last
def gather(self):
"Collect and return the contents of the window."
result = ""
for y in range(self.maxy+1):
self.win.move(y, 0)
stop = self._end_of_line(y)
if stop == 0 and self.stripspaces:
continue
for x in range(self.maxx+1):
if self.stripspaces and x > stop:
break
result = result + chr(curses.ascii.ascii(self.win.inch(y, x)))
if self.maxy > 0:
result = result + "\n"
return result
def is_escape(self, key):
if len(key) == 1:
return ord(key) == curses.ascii.ESC
return False
def is_backspace(self, key):
if len(key) > 1:
return key == "KEY_BACKSPACE"
elif ord(key) in (curses.ascii.BS, curses.ascii.DEL):
return True
return False