Created
March 12, 2020 03:43
-
-
Save laixintao/76dfb501c1c1e3d4e2fc507273b7f37a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
from prompt_toolkit import prompt | |
from prompt_toolkit.completion import WordCompleter | |
from prompt_toolkit.contrib.regular_languages.compiler import compile | |
from prompt_toolkit.lexers import Lexer, PygmentsLexer, SimpleLexer | |
from prompt_toolkit.contrib.regular_languages.completion import GrammarCompleter | |
from prompt_toolkit.contrib.regular_languages.lexer import GrammarLexer | |
animal_completer = WordCompleter( | |
[ | |
"alligator", | |
"ant", | |
"ape", | |
"bat", | |
"bear", | |
"beaver", | |
"bee", | |
"bison", | |
"butterfly", | |
"cat", | |
"chicken", | |
"crocodile", | |
"dinosaur", | |
"dog", | |
"dolphin", | |
"dove", | |
"duck", | |
"eagle", | |
"elephant", | |
"fish", | |
"goat", | |
"gorilla", | |
"kangaroo", | |
"leopard", | |
"lion", | |
"mouse", | |
"rabbit", | |
"rat", | |
"snake", | |
"spider", | |
"turkey", | |
"turtle", | |
], | |
ignore_case=True, | |
) | |
class MyCompleter(WordCompleter): | |
def get_completions(self, document, event): | |
print(document, file=sys.stderr) | |
yield from super().get_completions(document, event) | |
grammar = compile(r"(?P<foo>\w+) (?P<bar>\d+)") | |
lexer = GrammarLexer(grammar, lexers={"foo": SimpleLexer(), "bar": SimpleLexer()}) | |
completer = GrammarCompleter(grammar, {"foo": animal_completer, "bar": MyCompleter(["123"])}) | |
def main(): | |
text = prompt( | |
"Input a word then numbers:", | |
complete_while_typing=True, | |
lexer=lexer, | |
completer=completer, | |
) | |
print("You said: %s" % text) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try to get the whole line from completers.
run:
see logs: