|  | 
|  | 1 | +#!/usr/bin/env python | 
|  | 2 | +""" | 
|  | 3 | +Demonstration of a custom completer class and the possibility of styling | 
|  | 4 | +completions independently by passing formatted text objects to the "display" | 
|  | 5 | +and "display_meta" arguments of "Completion". | 
|  | 6 | +""" | 
|  | 7 | +from __future__ import unicode_literals | 
|  | 8 | +from prompt_toolkit.completion import Completion, Completer | 
|  | 9 | +from prompt_toolkit.formatted_text import HTML | 
|  | 10 | +from prompt_toolkit.shortcuts import prompt, CompleteStyle | 
|  | 11 | + | 
|  | 12 | + | 
|  | 13 | +animals = [ | 
|  | 14 | + 'alligator', 'ant', 'ape', 'bat', 'bear', 'beaver', 'bee', 'bison', | 
|  | 15 | + 'butterfly', 'cat', 'chicken', 'crocodile', 'dinosaur', 'dog', 'dolphin', | 
|  | 16 | + 'dove', 'duck', 'eagle', 'elephant', | 
|  | 17 | +] | 
|  | 18 | + | 
|  | 19 | +animal_family = { | 
|  | 20 | + 'alligator': 'reptile', | 
|  | 21 | + 'ant': 'insect', | 
|  | 22 | + 'ape': 'mammal', | 
|  | 23 | + 'bat': 'mammal', | 
|  | 24 | + 'bear': 'mammal', | 
|  | 25 | + 'beaver': 'mammal', | 
|  | 26 | + 'bee': 'insect', | 
|  | 27 | + 'bison': 'mammal', | 
|  | 28 | + 'butterfly': 'insect', | 
|  | 29 | + 'cat': 'mammal', | 
|  | 30 | + 'chicken': 'bird', | 
|  | 31 | + 'crocodile': 'reptile', | 
|  | 32 | + 'dinosaur': 'reptile', | 
|  | 33 | + 'dog': 'mammal', | 
|  | 34 | + 'dolphin': 'mammal', | 
|  | 35 | + 'dove': 'bird', | 
|  | 36 | + 'duck': 'bird', | 
|  | 37 | + 'eagle': 'bird', | 
|  | 38 | + 'elephant': 'mammal', | 
|  | 39 | +} | 
|  | 40 | + | 
|  | 41 | +family_colors = { | 
|  | 42 | + 'mammal': 'ansimagenta', | 
|  | 43 | + 'insect': 'ansigreen', | 
|  | 44 | + 'reptile': 'ansired', | 
|  | 45 | + 'bird': 'ansiyellow', | 
|  | 46 | +} | 
|  | 47 | + | 
|  | 48 | +meta = { | 
|  | 49 | + 'alligator': HTML('An <ansired>alligator</ansired> is a <u>crocodilian</u> in the genus Alligator of the family Alligatoridae.'), | 
|  | 50 | + 'ant': HTML('<ansired>Ants</ansired> are eusocial <u>insects</u> of the family Formicidae.'), | 
|  | 51 | + 'ape': HTML('<ansired>Apes</ansired> (Hominoidea) are a branch of Old World tailless anthropoid catarrhine <u>primates</u>.'), | 
|  | 52 | + 'bat': HTML('<ansired>Bats</ansired> are mammals of the order <u>Chiroptera</u>.'), | 
|  | 53 | + 'bee': HTML('<ansired>Bees</ansired> are flying <u>insects</u> closely related to wasps and ants.'), | 
|  | 54 | + 'beaver': HTML('The <ansired>beaver</ansired> (genus Castor) is a large, primarily <u>nocturnal</u>, semiaquatic <u>rodent</u>.'), | 
|  | 55 | + 'bear': HTML('<ansired>Bears</ansired> are carnivoran <u>mammals</u> of the family Ursidae.'), | 
|  | 56 | + 'butterfly': HTML('<ansiblue>Butterflies</ansiblue> are <u>insects</u> in the macrolepidopteran clade Rhopalocera from the order Lepidoptera.'), | 
|  | 57 | + # ... | 
|  | 58 | +} | 
|  | 59 | + | 
|  | 60 | + | 
|  | 61 | +class AnimalCompleter(Completer): | 
|  | 62 | + def get_completions(self, document, complete_event): | 
|  | 63 | + word = document.get_word_before_cursor() | 
|  | 64 | + for animal in animals: | 
|  | 65 | + if animal.startswith(word): | 
|  | 66 | + if animal in animal_family: | 
|  | 67 | + family = animal_family[animal] | 
|  | 68 | + family_color = family_colors.get(family, 'default') | 
|  | 69 | + | 
|  | 70 | + display = HTML( | 
|  | 71 | + '%s<b>:</b> <ansired>(<' + family_color + '>%s</' + family_color + '>)</ansired>' | 
|  | 72 | + ) % (animal, family) | 
|  | 73 | + else: | 
|  | 74 | + display = animal | 
|  | 75 | + | 
|  | 76 | + yield Completion( | 
|  | 77 | + animal, | 
|  | 78 | + start_position=-len(word), | 
|  | 79 | + display=display, | 
|  | 80 | + display_meta=meta.get(animal) | 
|  | 81 | + ) | 
|  | 82 | + | 
|  | 83 | + | 
|  | 84 | +def main(): | 
|  | 85 | + # Simple completion menu. | 
|  | 86 | + print('(The completion menu displays colors.)') | 
|  | 87 | + prompt('Type an animal: ', completer=AnimalCompleter()) | 
|  | 88 | + | 
|  | 89 | + # Multi-column menu. | 
|  | 90 | + prompt('Type an animal: ', completer=AnimalCompleter(), | 
|  | 91 | + complete_style=CompleteStyle.MULTI_COLUMN) | 
|  | 92 | + | 
|  | 93 | + # Readline-like | 
|  | 94 | + prompt('Type an animal: ', completer=AnimalCompleter(), | 
|  | 95 | + complete_style=CompleteStyle.READLINE_LIKE) | 
|  | 96 | + | 
|  | 97 | + | 
|  | 98 | +if __name__ == '__main__': | 
|  | 99 | + main() | 
0 commit comments