def do_command(self, ch): "Process a single editing command." (y, x) = self.win.getyx() self.lastcmd = ch if ascii.isprint(ch): if y == self.maxy or x < self.maxx: self.win.addch(ch) elif ch == ascii.SOH: # ^a self.win.move(y, 0) elif ch in (ascii.STX,curses.KEY_LEFT, ascii.BS,curses.KEY_BACKSPACE): if x > 0: self.win.move(y, x-1) elif y == 0: pass elif self.stripspaces: self.win.move(y-1, self._end_of_line(y-1)) else: self.win.move(y-1, self.maxx) if ch in (ascii.BS, curses.KEY_BACKSPACE): self.win.delch() elif ch == ascii.EOT or ch == ascii.LF: # ^d or line feed return 0 #self.win.delch() elif ch == ascii.ENQ: # ^e if self.stripspaces: self.win.move(y, self._end_of_line(y)) else: self.win.move(y, self.maxx) elif ch in (ascii.ACK, curses.KEY_RIGHT): # ^f if x < self.maxx: self.win.move(y, x+1) else: self.win.move(y+1, 0) #elif ch == ascii.VT: # ^k # if x == 0 and self._end_of_line(y) == 0: # self.win.deleteln() # else: # self.win.clrtoeol() #elif ch == ascii.FF: # ^l # self.win.refresh() #elif ch in (ascii.SO, curses.KEY_DOWN): # ^n # if y < self.maxy: # self.win.move(y+1, x) # if x > self._end_of_line(y+1): # self.win.move(y+1, self._end_of_line(y+1)) #elif ch == ascii.SI: # ^o # self.win.insertln() #elif ch in (ascii.DLE, curses.KEY_UP): # ^p # if y > 0: # self.win.move(y-1, x) # if x > self._end_of_line(y-1): # self.win.move(y-1, self._end_of_line(y-1)) if x == 0 and chr(ascii.ascii(ch)) in CommandInterpreter.oneoff: return 0 return 1
def do_command(self, ch): "Process a single editing command." (y, x) = self.win.getyx() self.lastcmd = ch if ascii.isprint(ch): if y < self.maxy or x < self.maxx: # 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 elif ch == ascii.SOH: # ^a self.win.move(y, 0) elif ch in (ascii.STX, curses.KEY_LEFT, ascii.BS, curses.KEY_BACKSPACE): if x > 0: self.win.move(y, x - 1) elif y == 0: pass elif self.stripspaces: self.win.move(y - 1, self._end_of_line(y - 1)) else: self.win.move(y - 1, self.maxx) if ch in (ascii.BS, curses.KEY_BACKSPACE): self.win.delch() elif ch == ascii.EOT: # ^d self.win.delch() elif ch == ascii.ENQ: # ^e if self.stripspaces: self.win.move(y, self._end_of_line(y)) else: self.win.move(y, self.maxx) elif ch in (ascii.ACK, curses.KEY_RIGHT): # ^f if x < self.maxx: self.win.move(y, x + 1) elif y == self.maxy: pass else: self.win.move(y + 1, 0) elif ch == ascii.BEL: # ^g return 0 elif ch == ascii.NL: # ^j if self.maxy == 0: return 0 elif y < self.maxy: self.win.move(y + 1, 0) elif ch == ascii.VT: # ^k if x == 0 and self._end_of_line(y) == 0: self.win.deleteln() else: # first undo the effect of self._end_of_line self.win.move(y, x) self.win.clrtoeol() elif ch == ascii.FF: # ^l self.win.refresh() elif ch in (ascii.SO, curses.KEY_DOWN): # ^n if y < self.maxy: self.win.move(y + 1, x) if x > self._end_of_line(y + 1): self.win.move(y + 1, self._end_of_line(y + 1)) elif ch == ascii.SI: # ^o self.win.insertln() elif ch in (ascii.DLE, curses.KEY_UP): # ^p if y > 0: self.win.move(y - 1, x) if x > self._end_of_line(y - 1): self.win.move(y - 1, self._end_of_line(y - 1)) return 1
def do_command(self, ch): "Process a single editing command." (y, x) = self.win.getyx() self.lastcmd = ch if ascii.isprint(ch): if y < self.maxy or x < self.maxx: # The try-catch ignores the error we trigger from some curses # versions by trying to write into the lowest-rightmost spot # in the self.window. try: self.win.addch(ch) except ERR: pass elif ch == ascii.SOH: # ^a self.win.move(y, 0) elif ch in (ascii.STX,curses.KEY_LEFT, ascii.BS,curses.KEY_BACKSPACE): if x > 0: self.win.move(y, x-1) elif y == 0: pass elif self.stripspaces: self.win.move(y-1, self.firstblank(y-1)) else: self.win.move(y-1, self.maxx) if ch in (ascii.BS, curses.KEY_BACKSPACE): self.win.delch() elif ch == ascii.EOT: # ^d self.win.delch() elif ch == ascii.ENQ: # ^e if self.stripspaces: self.win.move(y, self.firstblank(y)) else: self.win.move(y, self.maxx) elif ch in (ascii.ACK, curses.KEY_RIGHT): # ^f if x < self.maxx: self.win.move(y, x+1) elif y == self.maxy: pass else: self.win.move(y+1, 0) elif ch == ascii.BEL: # ^g return 0 elif ch == ascii.NL: # ^j if self.maxy == 0: return 0 elif y < self.maxy: self.win.move(y+1, 0) elif ch == ascii.VT: # ^k if x == 0 and self.firstblank(y) == 0: self.win.deleteln() else: self.win.clrtoeol() elif ch == ascii.FF: # ^l self.win.refresh() elif ch in (ascii.SO, curses.KEY_DOWN): # ^n if y < self.maxy: self.win.move(y+1, x) elif ch == ascii.SI: # ^o self.win.insertln() elif ch in (ascii.DLE, curses.KEY_UP): # ^p if y > 0: self.win.move(y-1, x) return 1
"""Simple textbox editing widget with Emacs-like keybindings."""
def do_command(self, ch): "Process a single editing command." (y, x) = self.win.getyx() self.lastcmd = ch if ascii.isprint(ch): if y < self.maxy or x < self.maxx: # 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.text_insert(y, x, ch) self.win.addstr(y, 0, ''.join([chr(ascii.ascii(ch)) for ch in self.text[y]])) self.win.move(y, x+1) except curses.error: pass elif ch == ascii.SOH: # ^a self.win.move(y, 0) elif ch in (ascii.STX,curses.KEY_LEFT, ascii.BS, curses.KEY_BACKSPACE): if x > 0: self.win.move(y, x-1) elif y == 0: pass elif self.stripspaces: self.win.move(y-1, self._end_of_line(y-1)) else: self.win.move(y-1, self.maxx) if ch in (ascii.BS, curses.KEY_BACKSPACE): self.win.delch() y, x = self.win.getyx() self.text_delete(y, x) elif ch == ascii.EOT: # ^d self.win.delch() self.text_delete(y, x) elif ch == ascii.ENQ: # ^e if self.stripspaces: self.win.move(y, self._end_of_line(y)) else: self.win.move(y, self.maxx) elif ch in (ascii.ACK, curses.KEY_RIGHT): # ^f if x < self.maxx: self.win.move(y, x+1) elif y == self.maxy: pass else: self.win.move(y+1, 0) elif ch == ascii.BEL: # ^g return True elif ch in (ascii.NL, ascii.CR): # ^j ^m if self.maxy == 0: return True elif y < self.maxy: self.win.move(y+1, 0) elif ch == ascii.VT: # ^k if x == 0 and self._end_of_line(y) == 0: self.win.deleteln() self.text_delete_line(y) else: # first undo the effect of self._end_of_line self.win.move(y, x) self.win.clrtoeol() self.text_delete_line(y, x) elif ch == ascii.FF: # ^l self.win.refresh() elif ch in (ascii.SO, curses.KEY_DOWN): # ^n if y < self.maxy: self.win.move(y+1, x) if x > self._end_of_line(y+1): self.win.move(y+1, self._end_of_line(y+1)) elif ch == ascii.SI: # ^o self.win.insertln() self.text_insert_line(y) elif ch in (ascii.DLE, curses.KEY_UP): # ^p if y > 0: self.win.move(y-1, x) if x > self._end_of_line(y-1): self.win.move(y-1, self._end_of_line(y-1)) return False