def _get_completions(self): """Return a list of possible completions for the string ending at the point. Also set begidx and endidx in the process. """ completions = [] self.begidx = self.l_buffer.point self.endidx = self.l_buffer.point buf = self.l_buffer.line_buffer if self.completer: # get the string to complete while self.begidx > 0: self.begidx -= 1 if buf[self.begidx] in self.completer_delims: self.begidx += 1 break text = ensure_str("".join(buf[self.begidx: self.endidx])) log('complete text="%s"' % ensure_unicode(text)) i = 0 while 1: try: r = self.completer(ensure_unicode(text), i) except IndexError: break i += 1 if r is None: break elif r and r not in completions: completions.append(r) else: pass log("text completions=<%s>" % list(map(ensure_unicode, completions))) if (self.complete_filesystem == "on") and not completions: # get the filename to complete while self.begidx > 0: self.begidx -= 1 if buf[self.begidx] in " \t\n": self.begidx += 1 break text = ensure_str("".join(buf[self.begidx: self.endidx])) log('file complete text="%s"' % ensure_unicode(text)) completions = list( map( ensure_unicode, glob.glob(os.path.expanduser(text) + "*".encode("ascii")), ) ) if self.mark_directories == "on": mc = [] for f in completions: if os.path.isdir(f): mc.append(f + os.sep) else: mc.append(f) completions = mc log("fnames=<%s>" % list(map(ensure_unicode, completions))) return completions
def _get_completions(self): """Return a list of possible completions for the string ending at the point. Also set begidx and endidx in the process.""" completions = [] self.begidx = self.l_buffer.point self.endidx = self.l_buffer.point buf = self.l_buffer.line_buffer if self.completer: # get the string to complete while self.begidx > 0: self.begidx -= 1 if buf[self.begidx] in self.completer_delims: self.begidx += 1 break text = ensure_str(''.join(buf[self.begidx:self.endidx])) log('complete text="%s"' % ensure_unicode(text)) i = 0 while 1: try: r = self.completer(ensure_unicode(text), i) except IndexError: break except AttributeError: # This is a quick hotfix, completer throws an error whenever # it finds nothing. break i += 1 if r is None: break elif r and r not in completions: completions.append(r) else: pass log('text completions=<%s>' % list(map(ensure_unicode, completions))) if (self.complete_filesystem == "on") and not completions: # get the filename to complete while self.begidx > 0: self.begidx -= 1 if buf[self.begidx] in ' \t\n': self.begidx += 1 break text = ensure_str(''.join(buf[self.begidx:self.endidx])) log('file complete text="%s"' % ensure_unicode(text)) completions = list( map(ensure_unicode, glob.glob(os.path.expanduser(text) + '*'.encode('ascii')))) if self.mark_directories == 'on': mc = [] for f in completions: if os.path.isdir(f): mc.append(f + os.sep) else: mc.append(f) completions = mc log('fnames=<%s>' % list(map(ensure_unicode, completions))) return completions
def write_plain(self, text, attr=None): u"""write text at current cursor position.""" text = ensure_unicode(text) log(u'write("%s", %s)' % (text, attr)) if attr is None: attr = self.attr junk = DWORD(0) self.SetConsoleTextAttribute(self.hout, attr) for short_chunk in split_block(chunk): self.WriteConsoleW(self.hout, ensure_unicode(short_chunk), len(short_chunk), byref(junk), None) return len(text)
def write_plain(self, text, attr=None): text = ensure_unicode(text) log('write("%s", %s)' % (text, attr)) if attr is None: attr = self.attr junk = DWORD(0) self.SetConsoleTextAttribute(self.hout, attr) for short_chunk in split_block(chunk): self.WriteConsoleW(self.hout, ensure_unicode(short_chunk), len(short_chunk), byref(junk), None) return len(text)
def write_plain(self, text, attr=None): u'''write text at current cursor position.''' text = ensure_unicode(text) log(u'write("%s", %s)' % (text, attr)) if attr is None: attr = self.attr n = c_int(0) self.SetConsoleTextAttribute(self.hout, attr) for short_chunk in split_block(chunk): self.WriteConsoleW(self.hout, ensure_unicode(short_chunk), len(short_chunk), byref(junk), None) return len(text)
def _get_completions(self): """Return a list of possible completions for the string ending at the point. Also set begidx and endidx in the process.""" completions = [] self.begidx = self.l_buffer.point self.endidx = self.l_buffer.point buf=self.l_buffer.line_buffer if self.completer: # get the string to complete while self.begidx > 0: self.begidx -= 1 if buf[self.begidx] in self.completer_delims: self.begidx += 1 break text = ensure_str(''.join(buf[self.begidx:self.endidx])) log('complete text="%s"' % ensure_unicode(text)) i = 0 while 1: try: r = self.completer(ensure_unicode(text), i) except IndexError: break except TypeError: break i += 1 if r is None: break elif r and r not in completions: completions.append(r) else: pass log('text completions=<%s>' % list(map(ensure_unicode, completions))) if (self.complete_filesystem == "on") and not completions: # get the filename to complete while self.begidx > 0: self.begidx -= 1 if buf[self.begidx] in ' \t\n': self.begidx += 1 break text = ensure_str(''.join(buf[self.begidx:self.endidx])) log('file complete text="%s"' % ensure_unicode(text)) completions = list(map(ensure_unicode, glob.glob(os.path.expanduser(text) + '*'.encode('ascii')))) if self.mark_directories == 'on': mc = [] for f in completions: if os.path.isdir(f): mc.append(f + os.sep) else: mc.append(f) completions = mc log('fnames=<%s>' % list(map(ensure_unicode, completions))) return completions
def _get_completions(self): """Return a list of possible completions for the string ending at the point. Also set begidx and endidx in the process.""" completions = [] self.begidx = self.l_buffer.point self.endidx = self.l_buffer.point buf = self.l_buffer.line_buffer if self.completer: # get the string to complete while self.begidx > 0: self.begidx -= 1 if buf[self.begidx] in self.completer_delims: self.begidx += 1 break text = ensure_str(u''.join(buf[self.begidx:self.endidx])) log(u'complete text="%s"' % ensure_unicode(text)) i = 0 while 1: try: r = ensure_unicode(self.completer(text, i)) except: break i += 1 if r and r not in completions: completions.append(r) else: break log(u'text completions=<%s>' % map(ensure_unicode, completions)) if not completions: # get the filename to complete while self.begidx > 0: self.begidx -= 1 if buf[self.begidx] in u' \t\n': self.begidx += 1 break text = ensure_str(u''.join(buf[self.begidx:self.endidx])) log(u'file complete text="%s"' % ensure_unicode(text)) completions = map(ensure_unicode, glob.glob(os.path.expanduser(text) + '*')) if self.mark_directories == u'on': mc = [] for f in completions: if os.path.isdir(f): mc.append(f + os.sep) else: mc.append(f) completions = mc log(u'fnames=<%s>' % map(ensure_unicode, completions)) return completions
def _get_completions(self): """Return a list of possible completions for the string ending at the point. Also set begidx and endidx in the process.""" completions = [] self.begidx = self.l_buffer.point self.endidx = self.l_buffer.point buf=self.l_buffer.line_buffer if self.completer: # get the string to complete while self.begidx > 0: self.begidx -= 1 if buf[self.begidx] in self.completer_delims: self.begidx += 1 break text = ensure_str(u''.join(buf[self.begidx:self.endidx])) log(u'complete text="%s"' % ensure_unicode(text)) i = 0 while 1: try: r = ensure_unicode(self.completer(text, i)) except: break i += 1 if r and r not in completions: completions.append(r) else: break log(u'text completions=<%s>' % map(ensure_unicode, completions)) if not completions: # get the filename to complete while self.begidx > 0: self.begidx -= 1 if buf[self.begidx] in u' \t\n': self.begidx += 1 break text = ensure_str(u''.join(buf[self.begidx:self.endidx])) log(u'file complete text="%s"' % ensure_unicode(text)) completions = map(ensure_unicode, glob.glob(os.path.expanduser(text) + '*')) if self.mark_directories == u'on': mc = [] for f in completions: if os.path.isdir(f): mc.append(f + os.sep) else: mc.append(f) completions = mc log(u'fnames=<%s>' % map(ensure_unicode, completions)) return completions
def add_item(self, line, force=False): # Kludge. pyreadline is a pain in the ass. from pyreadline import lineobj from pyreadline.unicode_helper import ensure_unicode line = ensure_unicode(line.rstrip()) readline.add_history(lineobj.ReadLineTextBuffer(line))
def parse_history_from_string(self, string=None): '''Create a readline history from a string. Each history item must be separated by a newline character (\n)''' if not string: return for line in string.split("\n"): self.add_history(ensure_unicode(line.rstrip()))
def add_history(self, line): '''Append a line to the history buffer, as if it was the last line typed. Empty lines are skipped. Repeated line, i.e. line equal to the last one is skipped as well modified history nformation is discarded ''' line = ensure_unicode(line) log('add_history line="%s" currentlen is %d' % (line.get_line_text(), len(self.history))) if not hasattr(line, "get_line_text"): line = lineobj.ReadLineTextBuffer(line) else: line.end_of_line() if not line.get_line_text(): if len(self.history) > 0 and len( self.history[-1].get_line_text()) == 0: self.history = self.history[:-1] pass elif len(self.history) > 0 and self.history[-1].get_line_text( ) == line.get_line_text(): pass elif len(self.history) > 0 and len( self.history[-1].get_line_text()) == 0: self.history[-1] = line else: self.history.append(line) self.history_cursor = len(self.history) self.clear_modified_history()
def check_key(): if msvcrt is None: return False else: if msvcrt.kbhit(): q = ensure_unicode(msvcrt.getch()) return q return ""
def write_history_file(self, filename = None): '''Save a readline history file.''' if filename is None: filename = self.history_filename fp = open(filename, 'w', encoding='utf-8') for line in self.history[-self.history_length:]: fp.write(ensure_unicode(line.get_line_text())) fp.write('\n') fp.close()
def write_plain(self, text, attr=None): '''write text at current cursor position.''' log('write("%s", %s)' %(text,attr)) if attr is None: attr = self.attr n = c_int(0) self.SetConsoleTextAttribute(self.hout, attr) self.WriteConsoleW(self.hout, ensure_unicode(chunk), len(chunk), byref(junk), None) return len(text)
def _readline_from_keyboard(self): c = self.console def nop(e): pass while 1: self._update_line() lbuf = self.l_buffer log_sock("point:%d mark:%d selection_mark:%d" % (lbuf.point, lbuf.mark, lbuf.selection_mark)) try: event = c.getkeypress() log_sock(u">>%s" % event) except KeyboardInterrupt: from pyreadline.keysyms.common import KeyPress from pyreadline.console.event import Event event = Event(0, 0) event.char = "c" event.keyinfo = KeyPress("c", shift=False, control=True, meta=False, keyname=None) log_sock("KBDIRQ") if self.allow_ctrl_c: now = time.time() if (now - self.ctrl_c_timeout) < self.ctrl_c_tap_time_interval: raise else: self.ctrl_c_timeout = now pass else: raise if self.next_meta: self.next_meta = False control, meta, shift, code = event.keyinfo event.keyinfo = (control, True, shift, code) # Process exit keys. Only exit on empty line keyinfo = event.keyinfo.tuple() if keyinfo in self.exit_dispatch: if lineobj.EndOfLine(self.l_buffer) == 0: raise EOFError if len(keyinfo[-1]) > 1: default = nop else: default = self.self_insert dispatch_func = self.key_dispatch.get(keyinfo, default) log("readline from keyboard:%s,%s" % (keyinfo, dispatch_func)) log_sock((u"%s|%s" % (ensure_unicode(format(keyinfo)), dispatch_func.__name__)), "bound_function") r = None if dispatch_func: r = dispatch_func(event) self._keylog(dispatch_func, self.l_buffer) self.l_buffer.push_undo() self.previous_func = dispatch_func if r: self._update_line() break
def write_scrolling(self, text, attr=None): '''write text at current cursor position while watching for scrolling. If the window scrolls because you are at the bottom of the screen buffer, all positions that you are storing will be shifted by the scroll amount. For example, I remember the cursor position of the prompt so that I can redraw the line but if the window scrolls, the remembered position is off. This variant of write tries to keep track of the cursor position so that it will know when the screen buffer is scrolled. It returns the number of lines that the buffer scrolled. ''' text = ensure_unicode(text) x, y = self.pos() w, h = self.size() scroll = 0 # the result # split the string into ordinary characters and funny characters chunks = self.motion_char_re.split(text) for chunk in chunks: n = self.write_color(chunk, attr) if len(chunk) == 1: # the funny characters will be alone if chunk[0] == '\n': # newline x = 0 y += 1 elif chunk[0] == '\r': # carriage return x = 0 elif chunk[0] == '\t': # tab x = 8 * (int(x / 8) + 1) if x > w: # newline x -= w y += 1 elif chunk[0] == '\007': # bell pass elif chunk[0] == '\010': x -= 1 if x < 0: y -= 1 # backed up 1 line else: # ordinary character x += 1 if x == w: # wrap x = 0 y += 1 if y == h: # scroll scroll += 1 y = h - 1 else: # chunk of ordinary characters x += n l = int(x / w) # lines we advanced x = x % w # new x value y += l if y >= h: # scroll scroll += y - h + 1 y = h - 1 return scroll
def read_history_file(self, filename=None): '''Load a readline history file.''' if filename is None: filename = self.history_filename try: for line in open(filename, 'r'): self.add_history(lineobj.ReadLineTextBuffer(ensure_unicode(line.rstrip()))) except IOError: self.history = [] self.history_cursor = 0
def read_history_file(self, filename=None): '''Load a readline history file.''' if filename is None: filename = self.history_filename try: for line in open(filename, 'r', encoding='utf-8', errors='replace'): self.add_history(lineobj.ReadLineTextBuffer(ensure_unicode(line.rstrip()))) except IOError: self.history = [] self.history_cursor = 0
def write_plain(self, text, attr=None): '''write text at current cursor position.''' log('write("%s", %s)' % (text, attr)) if attr is None: attr = self.attr n = c_int(0) self.SetConsoleTextAttribute(self.hout, attr) self.WriteConsoleW(self.hout, ensure_unicode(chunk), len(chunk), byref(junk), None) return len(text)
def write_color(self, text, attr=None): text = ensure_unicode(text) n, res = self.ansiwriter.write_color(text, attr) junk = c_int(0) for attr, chunk in res: log(unicode(attr)) log(unicode(chunk)) self.SetConsoleTextAttribute(self.hout, attr.winattr) self.WriteConsoleW(self.hout, chunk, len(chunk), byref(junk), None) return n
def write_color(self, text, attr=None): text = ensure_unicode(text) n,res= self.ansiwriter.write_color(text,attr) junk = c_int(0) for attr,chunk in res: log(unicode(attr)) log(unicode(chunk)) self.SetConsoleTextAttribute(self.hout, attr.winattr) self.WriteConsoleW(self.hout, chunk, len(chunk), byref(junk), None) return n
def GetClipboardText(): text = "" if OpenClipboard(0): hClipMem = GetClipboardData(CF_TEXT) if hClipMem: GlobalLock.restype = c_char_p text = GlobalLock(hClipMem) GlobalUnlock(hClipMem) CloseClipboard() return ensure_unicode(text)
def GetClipboardText(): text = u"" if OpenClipboard(0): hClipMem = GetClipboardData(CF_TEXT) if hClipMem: GlobalLock.restype = c_char_p text = GlobalLock(hClipMem) GlobalUnlock(hClipMem) CloseClipboard() return ensure_unicode(text)
def write_color(self, text, attr=None): text = ensure_unicode(text) n, res = self.ansiwriter.write_color(text, attr) junk = DWORD(0) for attr, chunk in res: log(u"console.attr:%s" % unicode(attr)) log(u"console.chunk:%s" % unicode(chunk)) self.SetConsoleTextAttribute(self.hout, attr.winattr) for short_chunk in split_block(chunk): self.WriteConsoleW(self.hout, short_chunk, len(short_chunk), byref(junk), None) return n
def read_history_file(self, filename=None): """Load a readline history file.""" if filename is None: filename = self.history_filename try: for line in open(filename, "r"): self.add_history( lineobj.ReadLineTextBuffer(ensure_unicode(line.rstrip()))) except IOError: self.history = [] self.history_cursor = 0
def write_color(self, text, attr=None): text = ensure_unicode(text) n, res = self.ansiwriter.write_color(text, attr) junk = c_int(0) for attr, chunk in res: log(u"console.attr:%s" % unicode(attr)) log(u"console.chunk:%s" % unicode(chunk)) self.SetConsoleTextAttribute(self.hout, attr.winattr) for short_chunk in split_block(chunk): self.WriteConsoleW(self.hout, short_chunk, len(short_chunk), byref(junk), None) return n
def SetClipboardText(text): buffer = create_unicode_buffer(ensure_unicode(text)) bufferSize = sizeof(buffer) hGlobalMem = GlobalAlloc(c_int(GHND), c_int(bufferSize)) GlobalLock.restype = c_void_p lpGlobalMem = GlobalLock(c_int(hGlobalMem)) memcpy(lpGlobalMem, addressof(buffer), c_int(bufferSize)) GlobalUnlock(c_int(hGlobalMem)) if OpenClipboard(0): EmptyClipboard() SetClipboardData(c_int(CF_UNICODETEXT), c_int(hGlobalMem)) CloseClipboard()
def add_history(self, line): '''Append a line to the history buffer, as if it was the last line typed.''' line = ensure_unicode(line) if not hasattr(line, "get_line_text"): line = lineobj.ReadLineTextBuffer(line) if not line.get_line_text(): pass elif len(self.history) > 0 and self.history[-1].get_line_text() == line.get_line_text(): pass else: self.history.append(line) self.history_cursor = len(self.history)
def SetClipboardText(text): buffer = create_unicode_buffer(ensure_unicode(text)) bufferSize = sizeof(buffer) hGlobalMem = GlobalAlloc(GHND, c_size_t(bufferSize)) GlobalLock.restype = c_void_p lpGlobalMem = GlobalLock(hGlobalMem) _strncpy(cast(lpGlobalMem, c_wchar_p), cast(addressof(buffer), c_wchar_p), c_size_t(bufferSize)) GlobalUnlock(c_int(hGlobalMem)) if OpenClipboard(0): EmptyClipboard() SetClipboardData(CF_UNICODETEXT, hGlobalMem) CloseClipboard()
def get(self): '''Get next event from queue.''' inputHookFunc = c_int.from_address(self.inputHookPtr).value Cevent = INPUT_RECORD() count = c_int(0) while 1: if inputHookFunc: call_function(inputHookFunc, ()) status = self.ReadConsoleInputW(self.hin, byref(Cevent), 1, byref(count)) if status and count.value == 1: e = event(self, Cevent) log_sock(ensure_unicode(e.keyinfo),"keypress") return e
def get(self): '''Get next event from queue.''' inputHookFunc = c_int.from_address(self.inputHookPtr).value Cevent = INPUT_RECORD() count = c_int(0) while 1: if inputHookFunc: call_function(inputHookFunc, ()) status = self.ReadConsoleInputW(self.hin, byref(Cevent), 1, byref(count)) if status and count.value == 1: e = event(self, Cevent) log_sock(ensure_unicode(e.keyinfo), "keypress") return e
def hook_wrapper_23(stdin, stdout, prompt): '''Wrap a Python readline so it behaves like GNU readline.''' try: # call the Python hook res = ensure_str(readline_hook(ensure_unicode(prompt))) # make sure it returned the right sort of thing if res and not isinstance(res, bytes): raise TypeError('readline must return a string.') except KeyboardInterrupt: # GNU readline returns 0 on keyboard interrupt return 0 except EOFError: # It returns an empty string on EOF res = ensure_str('') except: print('Readline internal error', file=sys.stderr) traceback.print_exc() res = ensure_str('\n') # we have to make a copy because the caller expects to free the result n = len(res) p = Console.PyMem_Malloc(n + 1) _strncpy(cast(p, c_char_p), res, n + 1) return p
def add_history(self, line): '''Append a line to the history buffer, as if it was the last line typed. Empty lines are skipped. Repeated line, i.e. line equal to the last one is skipped as well modified history nformation is discarded ''' line = ensure_unicode(line) log('add_history line="%s" currentlen is %d' % (line.get_line_text(), len(self.history))) if not hasattr(line, "get_line_text"): line = lineobj.ReadLineTextBuffer(line) else: line.end_of_line() if not line.get_line_text(): if len(self.history) > 0 and len(self.history[-1].get_line_text()) == 0: self.history = self.history[:-1] pass elif len(self.history) > 0 and self.history[-1].get_line_text() == line.get_line_text(): pass elif len(self.history) > 0 and len(self.history[-1].get_line_text()) == 0: self.history[-1] = line else: self.history.append(line) self.history_cursor = len(self.history) self.clear_modified_history()
def read_history_file(self, filename=None): u'''Load a readline history file. The default filename is ~/.history.''' if filename is None: filename = self.mode._history.history_filename log(u"read_history_file from %s"%ensure_unicode(filename)) self.mode._history.read_history_file(filename)
def write(self, text): text = ensure_unicode(text) log(u'write("%s")' % text) return self.write_color(text)
def write(self, text): text = ensure_unicode(text) log('write("%s")' % text) return self.write_color(text)
def _readline_from_keyboard(self): c = self.console def nop(e): pass while 1: self._update_line() lbuf = self.l_buffer log_sock("point:%d mark:%d selection_mark:%d" % (lbuf.point, lbuf.mark, lbuf.selection_mark)) try: event = c.getkeypress() log_sock(u">>%s" % event) except KeyboardInterrupt: from pyreadline.keysyms.common import KeyPress from pyreadline.console.event import Event event = Event(0, 0) event.char = "c" event.keyinfo = KeyPress("c", shift=False, control=True, meta=False, keyname=None) log_sock("KBDIRQ") if self.allow_ctrl_c: now = time.time() if (now - self.ctrl_c_timeout ) < self.ctrl_c_tap_time_interval: raise else: self.ctrl_c_timeout = now pass else: raise if self.next_meta: self.next_meta = False control, meta, shift, code = event.keyinfo event.keyinfo = (control, True, shift, code) #Process exit keys. Only exit on empty line keyinfo = event.keyinfo.tuple() if keyinfo in self.exit_dispatch: if lineobj.EndOfLine(self.l_buffer) == 0: raise EOFError if len(keyinfo[-1]) > 1: default = nop else: default = self.self_insert dispatch_func = self.key_dispatch.get(keyinfo, default) log("readline from keyboard:%s,%s" % (keyinfo, dispatch_func)) log_sock( (u"%s|%s" % (ensure_unicode(format(keyinfo)), dispatch_func.__name__)), "bound_function") r = None if dispatch_func: r = dispatch_func(event) self._keylog(dispatch_func, self.l_buffer) self.l_buffer.push_undo() self.previous_func = dispatch_func if r: self._update_line() break
def read_history_file(self, filename=None): if filename is None: filename = self.mode._history.history_filename log('read_history_file from %s' % ensure_unicode(filename)) self.mode._history.read_history_file(filename) return