def interrupt(self): 'Handle ^C, exit from Python sesson' # raw mode terminal doesn't respond to ^C, must handle here util.putstr('^C') terminal.set_line_mode() # on new line... print() # ... otherwise traceback is a mess raise KeyboardInterrupt
def update(buf): if session.session.foreground == session.eden and ed.buf == buf: terminal.set_line_mode() edsel.win.update(open_line=(not ed.command_mode)) # open line in insert mode # restore cursor to eden cmd line (bottom of scroll region), edit point display.put_cursor(edsel.cmd_n, 1) # FIXME just get it running for now terminal.set_char_mode() # FIXME should restore whatever was before
def main(): 'Demonstrate Key class' key = Key() util.putstr('> ') terminal.set_char_mode() line = '' k = '' # anything but cr while k != keyboard.cr: k = key() line += k util.putstr(k) terminal.set_line_mode() print() print([ c for c in line ]) # show any esc or other unprintables
def main(): import terminal # only needed for this test def edit(): 'event loop for testing inputline' while True: # reads single chars, arrow keys won't work key = terminal.getchar() if key in printing_chars or key in command.keymap: command.handler(key) else: break print('Enter and edit a fresh line:') util.putstr(command.prompt) terminal.set_char_mode() # inputline event loop edit() # based on Console restore method terminal.set_line_mode() print() # advance to next line command.redraw() # echo so we can see what we edited print() # Caller can reassign prompt, line to edit, and point print('Now edit a previously entered line:') command.reinit(prompt='>> ', line='Here is some text to edit.', point=0) util.putstr(command.prompt) # command.redraw() # no good - resets point util.putstr(command.line) # preserves point command.move_to_point() # restore cursor to point terminal.set_char_mode() edit() terminal.set_line_mode() print() print(command.line)
def update(op, sourcebuf=None, buffer=None, origin=0, destination=0, start=0, end=0, column=1): # display column numbers are 1-based 'Update the display: one window, several, or the entire frame.' global mode, win, ifocus, cmd_h # Clear display, redraw all the windows and scrolling command region. if op == Op.refresh: refresh(column=column) # Restore full screen scrolling, cursor to bottom elif op == Op.restore: display.set_scroll_all() display.put_cursor(nlines,1) # Rescale frame and window sizes, then refresh. elif op == Op.rescale: cmd_h = start if start else cmd_h rescale() # Create new buffer, ed B, Op.insert case will display its contents. elif op == Op.create: win.focus = True win.buf = buffer win.locate_segment(win.buf.dot) win.saved_dot = win.buf.dot win.reupdate() # Change buffer in current window, ed b E D elif op == Op.select: win.focus = True win.buf = buffer win.reupdate() # Delete current buffer, ed D elif op == Op.remove: for w in windows: if w.buf == sourcebuf: # deleted buffer w.buf = buffer # new current buffer w.reupdate() # Switch to ed input mode, for ed a i c commands elif op == Op.input: mode = Mode.input window.show_marker = False win.update_for_input() wdot = win.wline(win.buf.dot) display.put_cursor(wdot+1,1) # Switch to ed command mode, ed . while in input mode elif op == Op.command: mode = Mode.command window.show_marker = True # Overwrite '.' line on display, and lines below. win.update_from(win.buf.dot + 1) win.set_marker(win.buf.dot) # Switch to eden display mode elif op == Op.display: mode = Mode.display window.show_marker = False put_display_cursor(column=column) # Dot moved, ed l command elif op == Op.locate: win.locate(origin, destination) # Insert text: ed a i c m r t y commands # start, end are after insert, start == destination, end == win.buf.dot elif op == Op.insert and origin != background_task: if mode != Mode.input: # ed commands m r t y win.insert(origin, start, end) elif mode == Mode.input: # input mode after ed commands a i c # Text at dot is already up-to-date on display, open next line. win.update_for_input() for w in windows: if w.samebuf(win): w.adjust_insert(start, end, destination) if mode == Mode.input: # can't put input cursor til other windows done win.put_cursor_for_input(column=1) # Background task inserts text by calling buffer write() method. # Search for windows (if any) which displays that buffer. elif op == Op.insert and origin == background_task: for w in windows: if w.buf == buffer: w.saved_dot = w.buf.dot terminal.set_line_mode() w.insert(origin, start, end) terminal.set_char_mode() if mode == Mode.input: # can't put input cursor til other windows done win.put_cursor_for_input(column=column) elif mode == Mode.display: put_display_cursor(column=column) else: pass # Mode.commmand handled at the end of this fcn # Delete text: ed d m command # start,end are line numbers before delete, destination == win.buf.dot elif op == Op.delete: win.delete(destination) for w in windows: if w.samebuf(win): w.adjust_delete(start, end, destination) # Change text: ed s command # Update all lines in start..destination, don't know which lines changed elif op == Op.mutate: win.mutate(origin, start, destination) for w in windows: if w.samebuf(win): if w.intersects(start, destination): w.mutate_lines(start, destination) # Switch to next window, edsel o command elif op == Op.next: w0 = win w0.release_focus() w0.update_status() ifocus = ifocus+1 if ifocus+1 < len(windows) else 0 win = windows[ifocus] win.set_focus() win.update_status() # Delete all but current window, edsel o1 cmd elif op == Op.single: windows[:] = [win] ifocus = 0 win.resize(frame_top, windows_h-1, ncols) # one window, -1 excl status win.reupdate() # Split window, new window above becomes current window, edsel o2 command elif op == Op.hsplit: win_top = win.top win_nlines = win.nlines // 2 w0 = win w0.release_focus() w0.resize(win_top + win_nlines, w0.nlines - win_nlines, ncols) w0.move_update(w0.saved_dot) win = window.Window(win.buf,win_top,win_nlines-1,ncols) #-1 excl status win.focus = True windows.insert(ifocus, win) win.reupdate() # Update status line for given buffer in all of its windows elif op == Op.status: for w in windows: if w.buf == buffer: w.update_status() # In command mode put ed command cursor back in scrolling command region. # Then we can call standard Python input() or Piety Console restart(). if mode == Mode.command: put_command_cursor(column=column) # background task can set column # Each Op... case handles other modes, see refresh() and Op.refresh # Is that necessary? I recall it's because some cases use default column=1 return win # caller might need to know which window was selected
def restore(self): 'Restore terminal line mode, prepare to print on new line' terminal.set_line_mode() print()