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
python类ascii()的实例源码
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 _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 _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 _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 keys_init(self):
"""Define methods for each key.
"""
self.keys = {
curses.KEY_BACKSPACE: self.backspace,
CTRL('h'): self.backspace,
curses.ascii.BS: self.backspace,
curses.ascii.DEL: self.backspace,
curses.ascii.ETX: self.close,
curses.KEY_DC: self.del_char,
CTRL('d'): self.del_char,
CTRL('u'): self.del_to_bol,
CTRL('k'): self.del_to_eol,
curses.KEY_DOWN: self.down,
CTRL('n'): self.down,
curses.KEY_END: self.end,
CTRL('e'): self.end,
curses.KEY_F1: self.help,
curses.KEY_HOME: self.home,
CTRL('a'): self.home,
curses.KEY_ENTER: self.insert_line_or_quit,
curses.ascii.NL: self.insert_line_or_quit,
curses.ascii.LF: self.insert_line_or_quit,
"\n": self.insert_line_or_quit,
curses.KEY_LEFT: self.left,
CTRL('b'): self.left,
curses.KEY_NPAGE: self.page_down,
curses.KEY_PPAGE: self.page_up,
CTRL('v'): self.paste,
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_RIGHT: self.right,
CTRL('f'): self.right,
curses.KEY_UP: self.up,
CTRL('p'): self.up,
}
def on_key(self, ch):
if not self.active or self.disabled:
return
if not self.active.on_key(ch):
y, x = self.position(self.active)
if ch == curses.KEY_UP:
if y > 0:
self.set_active(self.rows[y - 1][self.defaults[y - 1]])
elif ch in (curses.KEY_DOWN, curses.KEY_ENTER):
if y < len(self.rows) - 1:
self.set_active(self.rows[y + 1][self.defaults[y + 1]])
elif ch == curses.KEY_LEFT:
if x > 0:
self.set_active(self.rows[y][x - 1])
elif ch == curses.KEY_RIGHT:
if x < len(self.rows[y]) - 1:
self.set_active(self.rows[y][x + 1])
elif ch == curses.ascii.TAB:
# Right
if x < len(self.rows[y]) - 1:
self.set_active(self.rows[y][x + 1])
# Down, ignoring defaults
elif y < len(self.rows) - 1:
self.set_active(self.rows[y + 1][0])
else:
self.set_active(self.rows[0][0])
elif ch == curses.KEY_BTAB:
# Left
if x > 0:
self.set_active(self.rows[y][x - 1])
# Up
elif y > 0:
col = len(self.rows[y - 1]) - 1
self.set_active(self.rows[y - 1][col])
else:
row = len(self.rows) - 1
col = len(self.rows[row]) - 1
self.set_active(self.rows[row][col])