def on_history_modified(history): try: f = open(HISTORY_PATH + '.tmp', 'w') for item in history: f.write(item + '\n') f.close() os.rename(HISTORY_PATH + '.tmp', HISTORY_PATH) except (IOError, OSError) as err: log('writing history failed %r' % err)
def run(self): self._init() try: self._run() except IOError as err: # when master tty is closed, errno 5 is raised log(err) finally: self._finish()
def switch(self, new): log('switching to new transport %s' % new, level=1) if Transport in self.state: values = { k:self.state[k] for k in self.save_fields } self.stack.append(values) else: values = {} self.state[Transport] = new self.switch_hook.call(self.state, values)
def finish((results, prefix)): log("competion request ->", results, prefix, level=1) if len(results) > 1: if self.key_history[-2:] == ['\t', '\t']: self.finish_line() self.terminal.write_normal('\n'.join(results) + '\n') self.start_line() self.set(prefix + self.buff[pos:]) self.move_to(len(prefix))
def switch(self, new): log('switching to new transport %s' % new, level=1) if Transport in self.state: values = {k: self.state[k] for k in self.save_fields} self.stack.append(values) else: values = {} self.state[Transport] = new self.switch_hook.call(self.state, values)
def show(self): log('rendering prompt', level=1) self.make_whole_line() ps1 = self.state[PS1] environ = self.state[EnvCache].environ prompt_text = format_with_default_params(ps1, path=self.state[CurrentDirectory], host=self.state[Transport].get_hostname(), user=environ.get('USER')) self.terminal.write_normal(prompt_text)
def _consume(self, data): ''' Called with current buffer. If it doesn't constitute single keystroke raises _NotReady. ''' log("Terminal._consume", repr(data), level=4) if data[0] == '\x0b': self._post(KeyEvent('kill', char='\x0b')) elif data[0] == '\x1b' and data[1:2] in '[O': if len(data) <= 2 or data[-1] in '1234567890[;\x1b': raise _NotReady else: code = data[-1] self._handle_code(code, data[2:-1], data[1]) else: self._post(KeyEvent(char=data[0]))
def _post(self, ev): log('KeyEvent', ev, '->', self.key_event, level=2) self.key_event.post(ev)
def get_cursor_position(self): log('get_cursor_position', level=1) self.get_cursor_position_event = task.Queue() self.write('\x1b[6n') data = self.get_cursor_position_event.get() return map(int, data.split(';'))[::-1]
def __finish(self): log("finishing %r" % self, level=3)
def start(self, daemon=True): log("starting %r" % self, level=3) self.thread = threading.Thread(target=self.__run) self.thread.daemon = daemon self.thread.start()