prompt_toolkit层级显示与动态bottom

陈法
2023-12-01
from prompt_toolkit import PromptSession
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.completion import NestedCompleter

# Create prompt object.
session = PromptSession(history=FileHistory('history.txt'),
                        auto_suggest=AutoSuggestFromHistory())


def bottom_toolbar():
    text = session.default_buffer.document.text
    if not text:
        return HTML('This is a <b><style bg="ansired">Toolbar</style></b>!')
    elif text == "show":
        return HTML('This is a <b><style bg="ansired">show</style></b>!')
    elif text == "show ip":
        return HTML('This is a <b><style bg="ansired">show ip</style></b>!')


completer = NestedCompleter.from_nested_dict({
    'show': {
        'version': None,
        'clock': None,
        'ip': {
            'interface': {'brief'}
        }
    },
    'exit': None,
})

# Do multiple input calls.
text1 = session.prompt(message="#", bottom_toolbar=bottom_toolbar, completer=completer)

 类似资料: