desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Get next character from queue.'
def getchar(self):
Cevent = INPUT_RECORD() count = c_int(0) while 1: status = self.ReadConsoleInputA(self.hin, byref(Cevent), 1, byref(count)) if (status and (count.value == 1) and (Cevent.EventType == 1) and Cevent.Event.KeyEvent.bKeyDown): sym = keysym(Cevent.Event.KeyEvent.wVirtualKeyCode) ...
'Check event queue.'
def peek(self):
Cevent = INPUT_RECORD() count = c_int(0) status = self.PeekConsoleInputA(self.hin, byref(Cevent), 1, byref(count)) log_sock(('%s %s %s' % (status, count, Cevent))) if (status and (count == 1)): return event(self, Cevent)
'Set/get title.'
def title(self, txt=None):
if txt: self.SetConsoleTitleA(txt) else: buffer = c_buffer(200) n = self.GetConsoleTitleA(buffer, 200) if (n > 0): return buffer.value[:n]
'Set/get window size.'
def size(self, width=None, height=None):
info = CONSOLE_SCREEN_BUFFER_INFO() status = self.GetConsoleScreenBufferInfo(self.hout, byref(info)) if (not status): return None if ((width is not None) and (height is not None)): wmin = ((info.srWindow.Right - info.srWindow.Left) + 1) hmin = ((info.srWindow.Bottom - info.srWind...
'Set cursor on or off.'
def cursor(self, visible=None, size=None):
info = CONSOLE_CURSOR_INFO() if self.GetConsoleCursorInfo(self.hout, byref(info)): if (visible is not None): info.bVisible = visible if (size is not None): info.dwSize = size self.SetConsoleCursorInfo(self.hout, byref(info))
'Get next event serial number.'
def next_serial(self):
self.serial += 1 return self.serial
'Initialize an event from the Windows input structure.'
def __init__(self, console, input):
self.type = '??' self.serial = console.next_serial() self.width = 0 self.height = 0 self.x = 0 self.y = 0 self.char = '' self.keycode = 0 self.keysym = '??' self.keyinfo = None self.width = None if (input.EventType == KEY_EVENT): if input.Event.KeyEvent.bKeyDown: ...
'Initialize the Console object. newbuffer=1 will allocate a new buffer so the old content will be restored on exit.'
def __init__(self, newbuffer=0):
self.serial = 0 self.attr = System.Console.ForegroundColor self.saveattr = winattr[str(System.Console.ForegroundColor).lower()] self.savebg = System.Console.BackgroundColor log(('initial attr=%s' % self.attr)) log_sock(('%s' % self.saveattr))
'Cleanup the console when finished.'
def __del__(self):
pass
'Move or query the window cursor.'
def pos(self, x=None, y=None):
if (x is not None): System.Console.CursorLeft = x else: x = System.Console.CursorLeft if (y is not None): System.Console.CursorTop = y else: y = System.Console.CursorTop return (x, y)
'Move to home.'
def home(self):
self.pos(0, 0)
'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 windo...
def write_scrolling(self, text, attr=None):
(x, y) = self.pos() (w, h) = self.size() scroll = 0 chunks = self.motion_char_re.split(text) for chunk in chunks: log(('C:' + chunk)) n = self.write_color(chunk, attr) if (len(chunk) == 1): if (chunk[0] == '\n'): x = 0 y += 1 ...
'write text at current cursor position and interpret color escapes. return the number of characters written.'
def write_color(self, text, attr=None):
log(('write_color("%s", %s)' % (text, attr))) chunks = self.terminal_escape.split(text) log(('chunks=%s' % repr(chunks))) bg = self.savebg n = 0 if (attr is None): attr = self.attr try: fg = self.trtable[(15 & attr)] bg = self.trtable[((240 & attr) >> 4)] excep...
'write text at current cursor position.'
def write_plain(self, text, attr=None):
log(('write("%s", %s)' % (text, attr))) if (attr is None): attr = self.attr n = c_int(0) self.SetConsoleTextAttribute(self.hout, attr) self.WriteConsoleA(self.hout, text, len(text), byref(n), None) return len(text)
'Fill the entire screen.'
def page(self, attr=None, fill=' '):
System.Console.Clear()
'Write text at the given position.'
def text(self, x, y, text, attr=None):
self.pos(x, y) self.write_color(text, attr)
'Fill Rectangle.'
def rectangle(self, rect, attr=None, fill=' '):
pass oldtop = self.WindowTop oldpos = self.pos() (x0, y0, x1, y1) = rect if (attr is None): attr = self.attr if fill: rowfill = (fill[:1] * abs((x1 - x0))) else: rowfill = (' ' * abs((x1 - x0))) for y in range(y0, y1): System.Console.SetCursorPosition(x...
'Scroll a rectangle.'
def scroll(self, rect, dx, dy, attr=None, fill=' '):
pass raise NotImplementedError
'Scroll the window by the indicated number of lines.'
def scroll_window(self, lines):
top = (self.WindowTop + lines) if (top < 0): top = 0 if ((top + System.Console.WindowHeight) > System.Console.BufferHeight): top = System.Console.BufferHeight self.WindowTop = top
'Return next key press event from the queue, ignoring others.'
def getkeypress(self):
ck = System.ConsoleKey while 1: e = System.Console.ReadKey(True) if (e.Key == System.ConsoleKey.PageDown): self.scroll_window(12) elif (e.Key == System.ConsoleKey.PageUp): self.scroll_window((-12)) elif (str(e.KeyChar) == '\x00'): log_sock(('De...
'Set/get title.'
def title(self, txt=None):
if txt: System.Console.Title = txt else: return System.Console.Title
'Set/get window size.'
def size(self, width=None, height=None):
sc = System.Console if ((width is not None) and (height is not None)): (sc.BufferWidth, sc.BufferHeight) = (width, height) else: return (sc.BufferWidth, sc.BufferHeight) if ((width is not None) and (height is not None)): (sc.WindowWidth, sc.WindowHeight) = (width, height) els...
'Set cursor on or off.'
def cursor(self, visible=True, size=None):
System.Console.CursorVisible = visible
'Get next event serial number.'
def next_serial(self):
self.serial += 1 return self.serial
'Initialize an event from the Windows input structure.'
def __init__(self, console, input):
self.type = '??' self.serial = console.next_serial() self.width = 0 self.height = 0 self.x = 0 self.y = 0 self.char = str(input.KeyChar) self.keycode = input.Key self.state = input.Modifiers log_sock(('%s,%s,%s' % (input.Modifiers, input.Key, input.KeyChar)), 'console') self....
'write text at current cursor position and interpret color escapes. return the number of characters written.'
def write_color(self, text, attr=None):
if isinstance(attr, AnsiState): defaultstate = attr elif (attr is None): attr = self.defaultstate.copy() else: defaultstate = AnsiState() defaultstate.winattr = attr attr = defaultstate chunks = terminal_escape.split(text) n = 0 res = [] for chunk in c...
'Display an event for debugging.'
def __repr__(self):
if (self.type in ['KeyPress', 'KeyRelease']): s = ("%s char='%s'%d keysym='%s' keycode=%d:%x state=%x keyinfo=%s" % (self.type, self.char, ord(self.char), self.keysym, self.keycode, self.keycode, self.state, self.keyinfo)) elif (self.type in ['Motion', 'Button']): s = ('%s x=%d...
'Move or query the window cursor.'
def pos(self, x=None, y=None):
raise NotImplementedError
'Fill Rectangle.'
def rectangle(self, rect, attr=None, fill=' '):
raise NotImplementedError
'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 windo...
def write_scrolling(self, text, attr=None):
raise NotImplementedError
'Return next key press event from the queue, ignoring others.'
def getkeypress(self):
raise NotImplementedError
'Fill the entire screen.'
def page(self, attr=None, fill=' '):
raise NotImplementedError
'Parse and execute single line of a readline init file.'
def parse_and_bind(self, string):
try: log(('parse_and_bind("%s")' % string)) if string.startswith('#'): return if string.startswith('set'): m = re.compile('set\\s+([-a-zA-Z0-9]+)\\s+(.+)\\s*$').match(string) if m: var_name = m.group(1) val = m.group(2) ...
'Return the current contents of the line buffer.'
def get_line_buffer(self):
return self.l_buffer.get_line_text()
'Insert text into the command line.'
def insert_text(self, string):
self.l_buffer.insert_text(string)
'Parse a readline initialization file. The default filename is the last filename used.'
def read_init_file(self, filename=None):
log(('read_init_file("%s")' % filename))
'Append a line to the history buffer, as if it was the last line typed.'
def add_history(self, line):
self._history.add_history(line)
'Return the desired length of the history file. Negative values imply unlimited history file size.'
def get_history_length(self):
return self._history.get_history_length()
'Set the number of lines to save in the history file. write_history_file() uses this value to truncate the history file when saving. Negative values imply unlimited history file size.'
def set_history_length(self, length):
self._history.set_history_length(length)
'Clear readline history'
def clear_history(self):
self._history.clear_history()
'Load a readline history file. The default filename is ~/.history.'
def read_history_file(self, filename=None):
self._history.read_history_file(filename)
'Save a readline history file. The default filename is ~/.history.'
def write_history_file(self, filename=None):
self._history.write_history_file(filename)
'Set or remove the completer function. If function is specified, it will be used as the new completer function; if omitted or None, any completer function already installed is removed. The completer function is called as function(text, state), for state in 0, 1, 2, ..., until it returns a non-string value. It should re...
def set_completer(self, function=None):
log('set_completer') self.completer = function
'Get the completer function.'
def get_completer(self):
log('get_completer') return self.completer
'Get the beginning index of the readline tab-completion scope.'
def get_begidx(self):
return self.begidx
'Get the ending index of the readline tab-completion scope.'
def get_endidx(self):
return self.endidx
'Set the readline word delimiters for tab-completion.'
def set_completer_delims(self, string):
self.completer_delims = string
'Get the readline word delimiters for tab-completion.'
def get_completer_delims(self):
return self.completer_delims
'Set or remove the startup_hook function. If function is specified, it will be used as the new startup_hook function; if omitted or None, any hook function already installed is removed. The startup_hook function is called with no arguments just before readline prints the first prompt.'
def set_startup_hook(self, function=None):
self.startup_hook = function
'Set or remove the pre_input_hook function. If function is specified, it will be used as the new pre_input_hook function; if omitted or None, any hook function already installed is removed. The pre_input_hook function is called with no arguments after the first prompt has been printed and just before readline starts re...
def set_pre_input_hook(self, function=None):
self.pre_input_hook = function
'ring the bell if requested.'
def _bell(self):
if (self.bell_style == 'none'): pass elif (self.bell_style == 'visible'): raise NotImplementedError('Bellstyle visible is not implemented yet.') elif (self.bell_style == 'audible'): self.console.bell() else: raise ReadlineError(('Bellstyle %s unknown....
'Insert text into the command line.'
def insert_text(self, string):
self.l_buffer.insert_text(string)
'motions: lowercase mode is alpha, digit and _, uppercase is delim by spaces w/W: forward short/long word'
def test_motion_word(self):
r = ViModeTest() r._set_line('abc_123 def--456.789 x') r.input('Escape') r.input('"0"') r.input('"w"') self.assertEqual(9, r.line_cursor) r.input('"w"') self.assertEqual(12, r.line_cursor) r.input('"w"') self.assertEqual(14, r.line_cursor) r.input('"W"') sel...
'motions: lowercase mode is alpha, digit and _, uppercase is delim by spaces e/E: to end of short/long word'
def test_motion_end(self):
r = ViModeTest() r._set_line(' abc_123 --def--456.789 x') r.input('Escape') r.input('"0"') r.input('"e"') self.assertEqual(8, r.line_cursor) r.input('"e"') self.assertEqual(12, r.line_cursor) r.input('"e"') self.assertEqual(15, r.line_cursor) r.input('"E"...
'motions: lowercase mode is alpha, digit and _, uppercase is delim by spaces b/B: backward short/long word'
def test_motion_backward(self):
r = ViModeTest() r._set_line('abc_123 def--456.789 x') r.input('Escape') r.input('"$"') self.assertEqual(23, r.line_cursor) r.input('"b"') self.assertEqual(18, r.line_cursor) r.input('"b"') self.assertEqual(17, r.line_cursor) r.input('"B"') self.assertEqual(9, r...
'Return the visible width of the text in line buffer up to position.'
def visible_line_width(self, position=Point):
return (len(self[:position].quoted_text()) + (self[:position].line_buffer.count(' DCTB ') * 7))
'Kills to next word ending'
def kill_word(self):
del self[Point:NextWordEnd]
'Kills to next word ending'
def backward_kill_word(self):
if (not self.delete_selection()): del self[PrevWordStart:Point] self.selection_mark = (-1)
'Kills to next word ending'
def forward_kill_word(self):
if (not self.delete_selection()): del self[Point:NextWordEnd] self.selection_mark = (-1)
'Copy the text in the region to the windows clipboard.'
def copy_region_to_clipboard(self):
if self.enable_win32_clipboard: mark = min(self.mark, len(self.line_buffer)) cursor = min(self.point, len(self.line_buffer)) if (self.mark == (-1)): return begin = min(cursor, mark) end = max(cursor, mark) toclipboard = ''.join(self.line_buffer[begin:end])...
'Copy the text in the region to the windows clipboard.'
def copy_selection_to_clipboard(self):
if (self.enable_win32_clipboard and self.enable_selection and (self.selection_mark >= 0)): selection_mark = min(self.selection_mark, len(self.line_buffer)) cursor = min(self.point, len(self.line_buffer)) if (self.selection_mark == (-1)): return begin = min(cursor, selecti...
'Clear readline history.'
def clear_history(self):
self.history[:] = [] self.history_cursor = 0
'Load a readline history file.'
def read_history_file(self, filename=None):
if (filename is None): filename = self.history_filename try: for line in open(filename, 'r'): self.add_history(lineobj.ReadLineTextBuffer(line.rstrip())) except IOError: self.history = [] self.history_cursor = 0
'Save a readline history file.'
def write_history_file(self, filename=None):
if (filename is None): filename = self.history_filename fp = open(filename, 'wb') for line in self.history[(- self.history_length):]: fp.write(line.get_line_text()) fp.write('\n') fp.close()
'Append a line to the history buffer, as if it was the last line typed.'
def add_history(self, 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)
'Move back through the history list, fetching the previous command.'
def previous_history(self, current):
if (self.history_cursor == len(self.history)): self.history.append(current.copy()) if (self.history_cursor > 0): self.history_cursor -= 1 current.set_line(self.history[self.history_cursor].get_line_text()) current.point = lineobj.EndOfLine
'Move forward through the history list, fetching the next command.'
def next_history(self, current):
if (self.history_cursor < (len(self.history) - 1)): self.history_cursor += 1 current.set_line(self.history[self.history_cursor].get_line_text())
'Move to the first line in the history.'
def beginning_of_history(self):
self.history_cursor = 0 if (len(self.history) > 0): self.l_buffer = self.history[0]
'Move to the end of the input history, i.e., the line currently being entered.'
def end_of_history(self, current):
self.history_cursor = len(self.history) current.set_line(self.history[(-1)].get_line_text())
'Search backward starting at the current line and moving up through the history as necessary using a non-incremental search for a string supplied by the user.'
def non_incremental_reverse_search_history(self, current):
return self._non_i_search((-1), current)
'Search forward starting at the current line and moving down through the the history as necessary using a non-incremental search for a string supplied by the user.'
def non_incremental_forward_search_history(self, current):
return self._non_i_search(1, current)
'Search forward through the history for the string of characters between the start of the current line and the point. This is a non-incremental search. By default, this command is unbound.'
def history_search_forward(self, partial):
q = self._search(1, partial) return q
'Search backward through the history for the string of characters between the start of the current line and the point. This is a non-incremental search. By default, this command is unbound.'
def history_search_backward(self, partial):
q = self._search((-1), partial) return q
'logfun should be function that takes disp_fun and line_buffer object'
def add_key_logger(self, logfun):
self._keylog = logfun
'Try to act like GNU readline.'
def readline(self, prompt=''):
self.ctrl_c_timeout = time.time() self.l_buffer.selection_mark = (-1) if self.first_prompt: self.first_prompt = False if self.startup_hook: try: self.startup_hook() except: print 'startup hook failed' traceback.pri...
'Move back through the history list, fetching the previous command.'
def previous_history(self, e):
self._history.previous_history(self.l_buffer) self.l_buffer.point = lineobj.EndOfLine
'Move forward through the history list, fetching the next command.'
def next_history(self, e):
self._history.next_history(self.l_buffer)
'Move to the first line in the history.'
def beginning_of_history(self, e):
self._history.beginning_of_history()
'Move to the end of the input history, i.e., the line currently being entered.'
def end_of_history(self, e):
self._history.end_of_history(self.l_buffer)
'Search backward starting at the current line and moving up through the history as necessary. This is an incremental search.'
def reverse_search_history(self, e):
self._i_search(self._history.reverse_search_history, (-1), e)
'Search forward starting at the current line and moving down through the the history as necessary. This is an incremental search.'
def forward_search_history(self, e):
self._i_search(self._history.forward_search_history, 1, e)
'Search backward starting at the current line and moving up through the history as necessary using a non-incremental search for a string supplied by the user.'
def non_incremental_reverse_search_history(self, e):
q = self._history.non_incremental_reverse_search_history(self.l_buffer) self.l_buffer = q
'Search forward starting at the current line and moving down through the the history as necessary using a non-incremental search for a string supplied by the user.'
def non_incremental_forward_search_history(self, e):
q = self._history.non_incremental_reverse_search_history(self.l_buffer) self.l_buffer = q
'Search forward through the history for the string of characters between the start of the current line and the point. This is a non-incremental search. By default, this command is unbound.'
def history_search_forward(self, e):
if (self.previous_func and hasattr(self._history, self.previous_func.__name__)): self._history.lastcommand = getattr(self._history, self.previous_func.__name__) else: self._history.lastcommand = None q = self._history.history_search_forward(self.l_buffer) self.l_buffer = q self.l_buf...
'Search backward through the history for the string of characters between the start of the current line and the point. This is a non-incremental search. By default, this command is unbound.'
def history_search_backward(self, e):
if (self.previous_func and hasattr(self._history, self.previous_func.__name__)): self._history.lastcommand = getattr(self._history, self.previous_func.__name__) else: self._history.lastcommand = None q = self._history.history_search_backward(self.l_buffer) self.l_buffer = q self.l_bu...
'Insert the first argument to the previous command (usually the second word on the previous line) at point. With an argument n, insert the nth word from the previous command (the words in the previous command begin with word 0). A negative argument inserts the nth word from the end of the previous command.'
def yank_nth_arg(self, e):
pass
'Insert last argument to the previous command (the last word of the previous history entry). With an argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last argument of each line in turn.'
def yank_last_arg(self, e):
pass
'Delete the character under the cursor, unless the cursor is at the end of the line, in which case the character behind the cursor is deleted. By default, this is not bound to a key.'
def forward_backward_delete_char(self, e):
pass
'Add the next character typed to the line verbatim. This is how to insert key sequences like C-q, for example.'
def quoted_insert(self, e):
e = self.console.getkeypress() self.insert_text(e.char)
'Insert a tab character.'
def tab_insert(self, e):
ws = (' ' * (self.tabstop - (self.line_cursor % self.tabstop))) self.insert_text(ws)
'Drag the character before the cursor forward over the character at the cursor, moving the cursor forward as well. If the insertion point is at the end of the line, then this transposes the last two characters of the line. Negative arguments have no effect.'
def transpose_chars(self, e):
self.l_buffer.transpose_chars()
'Drag the word before point past the word after point, moving point past that word as well. If the insertion point is at the end of the line, this transposes the last two words on the line.'
def transpose_words(self, e):
self.l_buffer.transpose_words()
'Toggle overwrite mode. With an explicit positive numeric argument, switches to overwrite mode. With an explicit non-positive numeric argument, switches to insert mode. This command affects only emacs mode; vi mode does overwrite differently. Each call to readline() starts in insert mode. In overwrite mode, characters ...
def overwrite_mode(self, e):
pass
'Kill the text from point to the end of the line.'
def kill_line(self, e):
self.l_buffer.kill_line()
'Kill backward to the beginning of the line.'
def backward_kill_line(self, e):
self.l_buffer.backward_kill_line()
'Kill backward from the cursor to the beginning of the current line.'
def unix_line_discard(self, e):
self.l_buffer.unix_line_discard()
'Kill all characters on the current line, no matter where point is. By default, this is unbound.'
def kill_whole_line(self, e):
self.l_buffer.kill_whole_line()
'Kill from point to the end of the current word, or if between words, to the end of the next word. Word boundaries are the same as forward-word.'
def kill_word(self, e):
self.l_buffer.kill_word()
'Kill the word behind point. Word boundaries are the same as backward-word.'
def backward_kill_word(self, e):
self.l_buffer.backward_kill_word()
'Kill the word behind point, using white space as a word boundary. The killed text is saved on the kill-ring.'
def unix_word_rubout(self, e):
self.l_buffer.unix_word_rubout()