-
Notifications
You must be signed in to change notification settings - Fork 731
/
Copy pathfuzzy-custom-completer.py
executable file
·64 lines (54 loc) · 1.46 KB
/
fuzzy-custom-completer.py
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
#!/usr/bin/env python
"""
Demonstration of a custom completer wrapped in a `FuzzyCompleter` for fuzzy
matching.
"""
from prompt_toolkit.completion import Completer, Completion, FuzzyCompleter
from prompt_toolkit.shortcuts import CompleteStyle, prompt
colors = [
"red",
"blue",
"green",
"orange",
"purple",
"yellow",
"cyan",
"magenta",
"pink",
]
class ColorCompleter(Completer):
def get_completions(self, document, complete_event):
word = document.get_word_before_cursor()
for color in colors:
if color.startswith(word):
yield Completion(
color,
start_position=-len(word),
style="fg:" + color,
selected_style="fg:white bg:" + color,
)
def main():
# Simple completion menu.
print("(The completion menu displays colors.)")
r = prompt(
"Type a color: ",
completer=FuzzyCompleter(ColorCompleter()),
complete_style=CompleteStyle.MULTI_COLUMN,
)
print(r)
# Multi-column menu.
r = prompt(
"Type a color: ",
completer=FuzzyCompleter(ColorCompleter()),
complete_style=CompleteStyle.MULTI_COLUMN,
)
print(r)
# Readline-like
r = prompt(
"Type a color: ",
completer=FuzzyCompleter(ColorCompleter()),
complete_style=CompleteStyle.READLINE_LIKE,
)
print(r)
if __name__ == "__main__":
main()