def example_repl(self, text, example, start_index, continue_flag): """ REPL for interactive tutorials """ if start_index: start_index = start_index + 1 cmd = ' '.join(text.split()[:start_index]) example_cli = CommandLineInterface( application=self.create_application(full_layout=False), eventloop=create_eventloop()) example_cli.buffers['example_line'].reset( initial_document=Document(u'{}\n'.format(add_new_lines( example)))) while start_index < len(text.split()): if self.default_command: cmd = cmd.replace(self.default_command + ' ', '') example_cli.buffers[DEFAULT_BUFFER].reset( initial_document=Document(u'{}'.format(cmd), cursor_position=len(cmd))) example_cli.request_redraw() answer = example_cli.run() if not answer: return "", True answer = answer.text if answer.strip('\n') == cmd.strip('\n'): continue else: if len(answer.split()) > 1: start_index += 1 cmd += " " + answer.split()[-1] + " " +\ u' '.join(text.split()[start_index:start_index + 1]) example_cli.exit() del example_cli else: cmd = text return cmd, continue_flag
def example_repl(self, text, example, start_index): """ REPL for interactive tutorials """ global EXAMPLE_REPL EXAMPLE_REPL = True if start_index: start_index = start_index + 1 cmd = ' '.join(text.split()[:start_index]) example_cli = CommandLineInterface( application=self.create_application( all_layout=False), eventloop=create_eventloop()) example_cli.buffers['example_line'].reset( initial_document=Document(u'%s\n' %example) ) for i in range(len(text.split()) - start_index): example_cli.buffers[DEFAULT_BUFFER].reset( initial_document=Document(u'%s' %cmd,\ cursor_position=len(cmd))) example_cli.request_redraw() answer = example_cli.run() if not answer: return "" answer = answer.text start_index += 1 if len(answer.split()) > 1: cmd += " " + answer.split()[-1] + " " +\ u' '.join(text.split()[start_index:start_index + 1]) example_cli.exit() del example_cli else: cmd = text EXAMPLE_REPL = False return cmd
def example_repl(self, text, example, start_index, continue_flag): """ REPL for interactive tutorials """ from prompt_toolkit.interface import CommandLineInterface if start_index: start_index = start_index + 1 cmd = ' '.join(text.split()[:start_index]) example_cli = CommandLineInterface( application=self.create_application( full_layout=False), eventloop=create_eventloop()) example_cli.buffers['example_line'].reset( initial_document=Document(u'{}\n'.format( add_new_lines(example))) ) while start_index < len(text.split()): if self.default_command: cmd = cmd.replace(self.default_command + ' ', '') example_cli.buffers[DEFAULT_BUFFER].reset( initial_document=Document( u'{}'.format(cmd), cursor_position=len(cmd))) example_cli.request_redraw() answer = example_cli.run() if not answer: return "", True answer = answer.text if answer.strip('\n') == cmd.strip('\n'): continue else: if len(answer.split()) > 1: start_index += 1 cmd += " " + answer.split()[-1] + " " +\ u' '.join(text.split()[start_index:start_index + 1]) example_cli.exit() del example_cli else: cmd = text return cmd, continue_flag
class Tosh: def __init__(self, base_dir, config): for module in config.get('modules'): import_module(module) self.tasks = TaskManager(self) self.window = MainWindow(self) self.style = ToshStyle(config.get('ui', 'style')) self._parser = CommandLineParser(self, base_dir) self.config = config self.variables = {} application = Application( layout=self.window, buffer=Buffer( enable_history_search=True, complete_while_typing=False, is_multiline=False, history=FileHistory(base_dir + "/history"), # validator=validator, completer=CommandLineCompleter(self), auto_suggest=AutoSuggestFromHistory(), accept_action=AcceptAction(self.run_command), ), mouse_support=config.get('ui', 'mouse'), style=self.style, key_bindings_registry=get_key_bindings(self), use_alternate_screen=True) self._cli = CommandLineInterface(application=application, eventloop=create_asyncio_eventloop(), output=create_output(true_color=True)) def refresh(self): self._cli.request_redraw() def _exception_handler(self, loop, context): self._cli.reset() print(context['message']) if 'exception' in context: traceback.print_exc() sys.exit(1) def run(self): for cmd in self.config.get('autostart'): cmd_task = self._parser.parse(cmd) cmd_task.set_cmdline(cmd) self.tasks._tasks.append(cmd_task) asyncio.ensure_future(cmd_task.run()) asyncio.get_event_loop().set_exception_handler(self._exception_handler) try: asyncio.get_event_loop().run_until_complete(self._cli.run_async()) except EOFError: pass def run_command(self, _, document): if not document.text: return try: cmd_task = self._parser.parse(document.text) if isinstance(cmd_task, Statement): cmd_task.set_cmdline(document.text) document.reset(append_to_history=True) self.tasks._tasks.append(cmd_task) asyncio.ensure_future(cmd_task.run()) else: self.tasks._tasks.append( ErrorStatement(self, document.text, "Parser returned no task")) document.reset(append_to_history=False) except BaseException as e: # Last resort error handler self.tasks._tasks.append( ErrorStatement(self, document.text, traceback.format_exc())) document.reset(append_to_history=False)