Python prompt_toolkit.history.InMemoryHistory() Examples

The following are 11 code examples of prompt_toolkit.history.InMemoryHistory(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module prompt_toolkit.history , or try the search function .
Example #1
Source File: __init__.py    From click-repl with MIT License 7 votes vote down vote up
def bootstrap_prompt(prompt_kwargs, group):
    """
    Bootstrap prompt_toolkit kwargs or use user defined values.

    :param prompt_kwargs: The user specified prompt kwargs.
    """
    prompt_kwargs = prompt_kwargs or {}

    defaults = {
        "history": InMemoryHistory(),
        "completer": ClickCompleter(group),
        "message": u"> ",
    }

    for key in defaults:
        default_value = defaults[key]
        if key not in prompt_kwargs:
            prompt_kwargs[key] = default_value

    return prompt_kwargs 
Example #2
Source File: main.py    From kafka-shell with Apache License 2.0 6 votes vote down vote up
def main():
    settings = Settings()
    bindings = get_bindings(settings)
    executor = Executor(settings)
    toolbar = Toolbar(settings)
    completer = KafkaCompleter(settings) if settings.enable_auto_complete else None
    suggester = AutoSuggestFromHistory() if settings.enable_auto_suggest else None
    history = ThreadedHistory(FileHistory(get_user_history_path())) if settings.enable_history else InMemoryHistory()
    session = PromptSession(completer=completer, style=style, bottom_toolbar=toolbar.handler,
                            key_bindings=bindings, history=history, include_default_pygments_style=False)
    while True:
        try:
            command = session.prompt([("class:operator", "> ")], auto_suggest=suggester)
        except KeyboardInterrupt:
            continue
        except EOFError:
            break
        else:
            executor.execute(command)

    settings.save_settings() 
Example #3
Source File: app.py    From azure-cli-shell with MIT License 6 votes vote down vote up
def __init__(self, completer=None, styles=None,
                 lexer=None, history=InMemoryHistory(),
                 app=None, input_custom=sys.stdout, output_custom=None):
        self.styles = styles
        if styles:
            self.lexer = lexer or AzLexer
        else:
            self.lexer = None
        self.app = app
        self.completer = completer
        self.history = history
        self._cli = None
        self.refresh_cli = False
        self.layout = None
        self.description_docs = u''
        self.param_docs = u''
        self.example_docs = u''
        self._env = os.environ
        self.last = None
        self.last_exit = 0
        self.input = input_custom
        self.output = output_custom
        self.config_default = ""
        self.default_command = "" 
Example #4
Source File: Prompt.py    From topydo with GNU General Public License v3.0 5 votes vote down vote up
def run(self):
        """ Main entry function. """
        history = InMemoryHistory()
        self._load_file()

        while True:
            # (re)load the todo.txt file (only if it has been modified)

            try:
                user_input = prompt(u'topydo> ', history=history,
                                    completer=self.completer,
                                    complete_while_typing=False)
                user_input = shlex.split(user_input)
            except EOFError:
                sys.exit(0)
            except KeyboardInterrupt:
                continue
            except ValueError as verr:
                error('Error: ' + str(verr))
                continue

            try:
                (subcommand, args) = get_subcommand(user_input)
            except ConfigError as ce:
                error('Error: ' + str(ce) + '. Check your aliases configuration')
                continue

            try:
                if self._execute(subcommand, args) != False:
                    self._post_execute()
            except TypeError:
                print(GENERIC_HELP) 
Example #5
Source File: shell.py    From we-get with MIT License 5 votes vote down vote up
def shell(self, items, pargs):
        self.pargs = pargs
        self.items = items
        stdout.write(
            '\n')  # When the fetching messege ends need to add \n after \r.
        self.prompt_show_items()
        history = InMemoryHistory()

        session = PromptSession() if PROMPT_TOOLKIT_V2 else None
        while True:
            kwargs = dict(
                history=history,
                auto_suggest=AutoSuggestFromHistory(),
                completer=WGCompleter(list(self.items.keys())),
                style=we_get_prompt_style
            )
            try:
                p = prompt(u'we-get > ', **kwargs)
            except TypeError as e:
                log.debug('{}:{}'.format(type(e), e))
                kwargs.pop('history')
                if PROMPT_TOOLKIT_V2:
                    p = session.prompt(u'we-get > ', **kwargs)
                else:
                    p = prompt(u'we-get > ', **kwargs)

            if self.prompt_no_command(p):
                continue
            elif self.prompt_is_single_command(p):
                command = p
                args = None
            else:
                _ = p.split()
                command = _[0]
                _.pop(0)
                args = ' '.join(_)
            if not self.prompt_verify_command(command, args):
                continue
            elif not self.prompt_parse_command(command, args):
                break 
Example #6
Source File: auto-suggestion.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def main():
    # Create some history first. (Easy for testing.)
    history = InMemoryHistory()
    history.append_string("import os")
    history.append_string('print("hello")')
    history.append_string('print("world")')
    history.append_string("import path")

    # Print help.
    print("This CLI has fish-style auto-suggestion enable.")
    print('Type for instance "pri", then you\'ll see a suggestion.')
    print("Press the right arrow to insert the suggestion.")
    print("Press Control-C to retry. Control-D to exit.")
    print()

    session = PromptSession(
        history=history,
        auto_suggest=AutoSuggestFromHistory(),
        enable_history_search=True,
    )

    while True:
        try:
            text = session.prompt("Say something: ")
        except KeyboardInterrupt:
            pass  # Ctrl-C pressed. Try again.
        else:
            break

    print("You said: %s" % text) 
Example #7
Source File: up-arrow-partial-string-matching.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def main():
    # Create some history first. (Easy for testing.)
    history = InMemoryHistory()
    history.append_string("import os")
    history.append_string('print("hello")')
    history.append_string('print("world")')
    history.append_string("import path")

    # Print help.
    print("This CLI has up-arrow partial string matching enabled.")
    print('Type for instance "pri" followed by up-arrow and you')
    print('get the last items starting with "pri".')
    print("Press Control-C to retry. Control-D to exit.")
    print()

    session = PromptSession(history=history, enable_history_search=True)

    while True:
        try:
            text = session.prompt("Say something: ")
        except KeyboardInterrupt:
            pass  # Ctrl-C pressed. Try again.
        else:
            break

    print("You said: %s" % text) 
Example #8
Source File: test_yank_nth_arg.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _history():
    " Prefilled history. "
    history = InMemoryHistory()
    history.append_string("alpha beta gamma delta")
    history.append_string("one two three four")
    return history


# Test yank_last_arg. 
Example #9
Source File: test_cli.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _history():
    h = InMemoryHistory()
    h.append_string("line1 first input")
    h.append_string("line2 second input")
    h.append_string("line3 third input")
    return h 
Example #10
Source File: app.py    From aws-shell with Apache License 2.0 5 votes vote down vote up
def __init__(self, completer, model_completer, docs,
                 input=None, output=None, popen_cls=None):
        self.completer = completer
        self.model_completer = model_completer
        self.history = InMemoryHistory()
        self.file_history = FileHistory(build_config_file_path('history'))
        self._cli = None
        self._docs = docs
        self.current_docs = u''
        self.refresh_cli = False
        self.key_manager = None
        self._dot_cmd = DotCommandHandler()
        self._env = os.environ.copy()
        self._profile = None
        self._input = input
        self._output = output

        if popen_cls is None:
            popen_cls = subprocess.Popen
        self._popen_cls = popen_cls

        # These attrs come from the config file.
        self.config_obj = None
        self.config_section = None
        self.enable_vi_bindings = None
        self.show_completion_columns = None
        self.show_help = None
        self.theme = None

        self.load_config() 
Example #11
Source File: shell.py    From shellen with MIT License 5 votes vote down vote up
def __prompt_init(self):
        self.asm_history = InMemoryHistory()
        self.dsm_history = InMemoryHistory()

        self.prompt_style = style_from_pygments_dict({
            Token:       '#ff0066',
            Token.OS:    '#ff3838',
            Token.Colon: '#ffffff',
            Token.Mode:  '#f9a9c3 bold',
            Token.Arch:  '#5db2fc',
            Token.Pound: '#ffd82a',
        })