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 write_plain(self, text, attr=None): '''write text at current cursor position.''' 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 check_key(): if msvcrt is None: return False else: if msvcrt.kbhit(): q = ensure_unicode(msvcrt.getch()) return q return ""
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 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("console.attr:%s" % (attr)) log("console.chunk:%s" % (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 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 write(self, text): text = ensure_unicode(text) log('write("%s")' % text) return self.write_color(text)
def read_history_file(self, filename=None): '''Load a readline history file. The default filename is ~/.history.''' 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)