Skip to content

Conversation

@LucasParsy
Copy link

Allow to easily style WordCompleter and NestedCompleter completion prompts

exampleColorNested

obtained with this code example:

#!/usr/bin/env python3

from prompt_toolkit import prompt
from prompt_toolkit.completion import Completer, NestedCompleter, Completion

completer = NestedCompleter.from_nested_dict({
    Completion('colors', style="bold italic"): {
        Completion('blue', style="fg:ansiblue"): None,
        Completion('red', style="fg:ansired"): None,
        Completion('green', style="fg:ansigreen"): None
    },
    'normal': {'classic', 'strings', 'working'}
})

text = prompt('# ', completer=completer)
print('You said: %s' % text)

Also fixed bug where Completion could not be hashable. but there may be some risks of duplicates, please review.

My use case:
I just finished Advent of code 2019 (day 25), wich is a text-adventure game.
I wanted create a prompt which gives me completion of all available actions (in the style "go south", "take matches")
But I wanted the game to hint me for certain actions, like greying "south" if I already went there, or highlight some items in red to mark some sort of danger.

made Completion properly hashable
'display' and '_display_meta' could have been a FormattedText, which is not hashable
replaced with 'style', an str.

'Completion' objects can be used along 'str's on WordCompleter and NestedCompleter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant