#!/usr/bin/env python """ Demonstration of a custom completer class and the possibility of styling completions independently by passing formatted text objects to the "display" and "display_meta" arguments of "Completion". """ from prompt_toolkit.completion import Completer, Completion from prompt_toolkit.formatted_text import HTML from prompt_toolkit.shortcuts import CompleteStyle, prompt animals = [ "alligator", "ant", "ape", "bat", "bear", "beaver", "bee", "bison", "butterfly", "cat", "chicken", "crocodile", "dinosaur", "dog", "dolphin", "dove", "duck", "eagle", "elephant", ] animal_family = { "alligator": "reptile", "ant": "insect", "ape": "mammal", "bat": "mammal", "bear": "mammal", "beaver": "mammal", "bee": "insect", "bison": "mammal", "butterfly": "insect", "cat": "mammal", "chicken": "bird", "crocodile": "reptile", "dinosaur": "reptile", "dog": "mammal", "dolphin": "mammal", "dove": "bird", "duck": "bird", "eagle": "bird", "elephant": "mammal", } family_colors = { "mammal": "ansimagenta", "insect": "ansigreen", "reptile": "ansired", "bird": "ansiyellow", } meta = { "alligator": HTML( "An alligator is a crocodilian in the genus Alligator of the family Alligatoridae." ), "ant": HTML( "Ants are eusocial insects of the family Formicidae." ), "ape": HTML( "Apes (Hominoidea) are a branch of Old World tailless anthropoid catarrhine primates." ), "bat": HTML("Bats are mammals of the order Chiroptera."), "bee": HTML( "Bees are flying insects closely related to wasps and ants." ), "beaver": HTML( "The beaver (genus Castor) is a large, primarily nocturnal, semiaquatic rodent." ), "bear": HTML( "Bears are carnivoran mammals of the family Ursidae." ), "butterfly": HTML( "Butterflies are insects in the macrolepidopteran clade Rhopalocera from the order Lepidoptera." ), # ... } class AnimalCompleter(Completer): def get_completions(self, document, complete_event): word = document.get_word_before_cursor() for animal in animals: if animal.startswith(word): if animal in animal_family: family = animal_family[animal] family_color = family_colors.get(family, "default") display = HTML( "%s: (<" + family_color + ">%s)" ) % (animal, family) else: display = animal yield Completion( animal, start_position=-len(word), display=display, display_meta=meta.get(animal), ) def main(): # Simple completion menu. print("(The completion menu displays colors.)") prompt("Type an animal: ", completer=AnimalCompleter()) # Multi-column menu. prompt( "Type an animal: ", completer=AnimalCompleter(), complete_style=CompleteStyle.MULTI_COLUMN, ) # Readline-like prompt( "Type an animal: ", completer=AnimalCompleter(), complete_style=CompleteStyle.READLINE_LIKE, ) if __name__ == "__main__": main()