1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#!/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 <ansired>alligator</ansired> is a <u>crocodilian</u> in the genus Alligator of the family Alligatoridae."
),
"ant": HTML(
"<ansired>Ants</ansired> are eusocial <u>insects</u> of the family Formicidae."
),
"ape": HTML(
"<ansired>Apes</ansired> (Hominoidea) are a branch of Old World tailless anthropoid catarrhine <u>primates</u>."
),
"bat": HTML("<ansired>Bats</ansired> are mammals of the order <u>Chiroptera</u>."),
"bee": HTML(
"<ansired>Bees</ansired> are flying <u>insects</u> closely related to wasps and ants."
),
"beaver": HTML(
"The <ansired>beaver</ansired> (genus Castor) is a large, primarily <u>nocturnal</u>, semiaquatic <u>rodent</u>."
),
"bear": HTML(
"<ansired>Bears</ansired> are carnivoran <u>mammals</u> of the family Ursidae."
),
"butterfly": HTML(
"<ansiblue>Butterflies</ansiblue> are <u>insects</u> 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<b>:</b> <ansired>(<"
+ family_color
+ ">%s</"
+ family_color
+ ">)</ansired>"
) % (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()
|