Skip to content

How do I create a menu? #281

@obeleh

Description

@obeleh

I've got so far as the code below but the cursor is not moving. Am I heading in the wrong direction?

from prompt_toolkit.layout.containers import Window
from prompt_toolkit.layout.controls import TokenListControl
from prompt_toolkit.layout.margins import ScrollbarMargin
from prompt_toolkit.key_binding.manager import KeyBindingManager
from prompt_toolkit.token import Token
from prompt_toolkit.keys import Keys
from prompt_toolkit.filters.utils import to_cli_filter
from prompt_toolkit.application import Application
from prompt_toolkit.shortcuts import create_eventloop
from prompt_toolkit.interface import CommandLineInterface


manager = KeyBindingManager()


def getItemList(cli):
    tokens = []
    for i in range(100):
        tokens.append((Token.Item, str(i)))
        tokens.append((Token.NewLine, '\n'))
    return tokens


@manager.registry.add_binding(Keys.ControlQ, eager=True)
def _(event):
    event.cli.set_return_value(None)

itemsControl = TokenListControl(get_tokens=getItemList, has_focus=True)


@manager.registry.add_binding(Keys.Down, eager=True)
def onArrowDown(cli):
    itemsControl.move_cursor_down(cli)

display_arrows = to_cli_filter(True)

app = Application(
    layout=Window(content=itemsControl, right_margins=[ScrollbarMargin(display_arrows=display_arrows)]),
    buffers={},
    key_bindings_registry=manager.registry,
    mouse_support=True,
    use_alternate_screen=True
)

eventloop = create_eventloop()
cli = CommandLineInterface(application=app, eventloop=eventloop)
cli.run()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions