prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.37k stars 716 forks source link

Writing to `.text` or `buffer.document` inside Frame #667

Closed decentral1se closed 6 years ago

decentral1se commented 6 years ago

This is related to full screen applications and widgets.

I have the following prompt embedded in a FloatContainer:

    buffer = Buffer(
        completer=WordCompleter(
            ['help', 'stacks', 'books'],
            ignore_case=True
        ),
        complete_while_typing=True
    )

    prompt_buffer = Window(
        BufferControl(
            buffer=buffer,
            input_processors=[
                BeforeInput('λ '),
            ]
        )
    )

    command_prompt = Frame(
        title='COMMAND SHELL',
        body=FloatContainer(
            content=prompt_buffer,
            floats=[
                Float(
                    xcursor=True,
                    ycursor=True,
                    content=CompletionsMenu(max_height=5, scroll_offset=1)
                )
            ]
        ),
    )

So, I can start to see the following:

myshell

I then wire up a keybinding to handle hitting enter when submitting some input:

    bindings = KeyBindings()

    @bindings.add('c-m', filter=has_focus(command_prompt))
    def handle_command(event):
        output.body.buffer.document = Document(text='faked output')
        command_prompt.text = ''

This is following the calculator.py example in the examples/full-screen folder.

However, I soon find that:

AttributeError: 'Frame' object has no attribute 'body'

Now, I've poked around and can't see an API for getting access to writing new text values from the Frame. I just happen to like the clarity of the Frame borders for my UI, so interested to see if this can be solved.

Any thoughts? I'd be happy to submit a PR if something needs doing.

decentral1se commented 6 years ago

I've also tried to add an accept_handler=accept_handler to my Buffer but it is not running :/

jonathanslenders commented 6 years ago

These are valid remarks. I'll fix it ASAP.

jonathanslenders commented 6 years ago

Hi @lwm,

Did you try running using master instead of the latest Pypi release? Some time ago I did this fix: https://github.com/jonathanslenders/python-prompt-toolkit/commit/4aee265b184b5011d6698083a3ada51323afd019 That should make the "body" attribute of a Frame object changeable.

decentral1se commented 6 years ago

Lovely job, can confirm that latest master is making this all possible. Thanks!