prompt-toolkit: Dynamically add and remove buffers to VSplit or HSplit?

981 Views Asked by At

is it possible to add and remove buffers? So I want to change a fullscreen layout.

For example I want have some layout like this:

layout = VSplit([
Window(content=BufferControl(buffer_name=DEFAULT_BUFFER)),
Window(width=D.exact(1),
       content=FillControl('|', token=Token.Line)),
Window(content=BufferControl(buffer_name='RESULT')),
])

Now I want to add another Buffer after some time so that the layout is like this...

layout = VSplit([
Window(content=BufferControl(buffer_name=DEFAULT_BUFFER)),
Window(width=D.exact(1),
       content=FillControl('|', token=Token.Line)),
Window(content=BufferControl(buffer_name='RESULT')),
Window(content=BufferControl(buffer_name='NEW_BUFFER')),
])

Is there some layout.addbuffer(...) function or something like this?

1

There are 1 best solutions below

1
On

This can be done by completely replacing the layout attribute of the Application. So something like:

class MyApplication(Application):

    def __init__(self):
        # Initialise with the first layout
        super(MyApplication, self).__init__(
            layout=VSplit([
                Window(content=BufferControl(buffer_name=DEFAULT_BUFFER)),
                Window(width=D.exact(1),
                       content=FillControl('|', token=Token.Line)),
                Window(content=BufferControl(buffer_name='RESULT')),
            ]),
        )

    def add_buffer(self):
        # Update to use a new layout
        self.layout = VSplit([
            Window(content=BufferControl(buffer_name=DEFAULT_BUFFER)),
            Window(width=D.exact(1),
                   content=FillControl('|', token=Token.Line)),
            Window(content=BufferControl(buffer_name='RESULT')),
            Window(content=BufferControl(buffer_name='NEW_BUFFER')),
        ])