Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# e.g. an arrow key is an escape sequence.
chars = []
paste_event = None
while True:
# XXX shouldn't these return-cases first put anything in chars back into self.in_buffer?
if self.sigwinch_counter < _SIGWINCH_COUNTER:
self.sigwinch_counter = _SIGWINCH_COUNTER
self.in_buffer = chars + self.in_buffer
return events.WindowChangeEvent(*self.get_screen_size())
if self.sigint_queued:
self.sigint_queued = False
return events.SigIntEvent()
if self.refresh_queued:
self.refresh_queued = False
return events.RefreshRequestEvent('terminal control')
if len(chars) > 6: #debugging tool - eventually detect all key sequences!
raise ValueError("Key sequence not detected at some point: %r" % ''.join(chars))
logging.debug('getting key for %r', chars)
logging.debug('self.in_buffer %r', self.in_buffer)
if chars == ["\x1b"]:
# This also won't work on Windows I think
if self.in_buffer or self.nonblocking_read():
chars.append(self.in_buffer.pop(0))
continue
else:
c = '\x1b'
chars = []
else:
c = events.get_key(chars, keynames=keynames)
# c is a keyname or None for an incomplete prefix.
self.coderunner.interp = self.interp
self.completer = Autocomplete(self.interp.locals, self.config)
self.completer.autocomplete_mode = 'simple'
self.buffer = []
self.display_buffer = []
self.highlighted_paren = None
self.reevaluating = True
sys.stdin = ReevaluateFakeStdin(self.stdin, self)
for line in old_logical_lines:
self._current_line = line
self.on_enter(insert_into_history=insert_into_history)
while self.fake_refresh_request:
self.fake_refresh_request = False
self.process_event(events.RefreshRequestEvent())
sys.stdin = self.stdin
self.reevaluating = False
self.cursor_offset_in_line = 0
self._current_line = ''
elif self.stdin.has_focus:
result = self.stdin.process_event(e)
elif isinstance(e, events.SigIntEvent):
logging.debug('received sigint event')
self.keyboard_interrupt()
self.update_completion()
return
elif isinstance(e, events.PasteEvent):
self.paste_mode = True
for ee in e.events:
if ee in ("\n", "\r", "PAD_ENTER"):
self.on_enter()
while self.fake_refresh_request:
self.fake_refresh_request = False
self.process_event(events.RefreshRequestEvent())
elif isinstance(ee, events.Event):
pass # ignore events in a paste
else:
self.add_normal_character(ee if len(ee) == 1 else ee[-1]) #strip control seq
self.paste_mode = False
self.update_completion()
elif e in self.rl_char_sequences:
self.cursor_offset_in_line, self._current_line = self.rl_char_sequences[e](self.cursor_offset_in_line, self._current_line)
self.update_completion()
# readline history commands
elif e in ("KEY_UP",) + key_dispatch[self.config.up_one_line_key]:
self.rl_history.enter(self._current_line)
self._current_line = self.rl_history.back(False)
self.cursor_offset_in_line = len(self._current_line)
def dumb_input(self, requested_refreshes=[]):
chars = list(self.orig_stdin.readline()[:-1])
while chars or requested_refreshes:
if requested_refreshes:
requested_refreshes.pop()
self.process_event(events.RefreshRequestEvent())
continue
c = chars.pop(0)
if c in '/':
c = '\n'
elif c in '\\':
c = ''
elif c in '|':
def r(): raise Exception('errors in other threads should look like this')
t = threading.Thread(target=r)
t.daemon = True
t.start()
elif c in '$':
c = '[19~'
self.process_event(c)
def process_event(self, e):
"""Returns True if shutting down, otherwise mutates state of Repl object"""
# event names uses here are curses compatible, or the full names
# for a full list of what should have pretty names, see curtsies.events.CURSES_TABLE
if not isinstance(e, events.Event):
self.last_events.append(e)
self.last_events.pop(0)
result = None
logging.debug("processing event %r", e)
if isinstance(e, events.RefreshRequestEvent):
if self.status_bar.has_focus:
self.status_bar.process_event(e)
else:
assert self.coderunner.code_is_waiting
self.run_code_and_maybe_finish()
elif isinstance(e, events.WindowChangeEvent):
logging.debug('window change to %d %d', e.width, e.height)
self.width, self.height = e.width, e.height
elif self.status_bar.has_focus:
result = self.status_bar.process_event(e)
elif self.stdin.has_focus:
result = self.stdin.process_event(e)
elif isinstance(e, events.SigIntEvent):
logging.debug('received sigint event')
self.keyboard_interrupt()