Created
January 1, 2016 03:34
-
-
Save renanivo/44efe7dbb52790f431a0 to your computer and use it in GitHub Desktop.
First steps with prompt toolkit (Python 3)
This file contains hidden or 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
import asyncio | |
from prompt_toolkit.shortcuts import prompt_async | |
@asyncio.coroutine | |
def my_coroutine(): | |
while True: | |
result = yield from prompt_async('Say something: ', patch_stdout=True) | |
print('You said: %s' % result) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(my_coroutine()) | |
loop.close() |
This file contains hidden or 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
from prompt_toolkit import prompt | |
from prompt_toolkit.completion import Completer, Completion | |
class MyCustomCompleter(Completer): | |
def get_completions(self, document, complete_event): | |
yield Completion('completion', start_position=0) | |
text = prompt('> ', completer=MyCustomCompleter()) |
This file contains hidden or 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
from prompt_toolkit.shortcuts import prompt | |
from pygments.style import Style | |
from prompt_toolkit.styles import Token | |
class MyStyle(Style): | |
styles = { | |
# User input. | |
Token: '#ff0066', | |
# Prompt. | |
Token.Username: '#884444', | |
Token.At: '#00aa00', | |
Token.Colon: '#00aa00', | |
Token.Pound: '#00aa00', | |
Token.Host: '#000088 bg:#aaaaff', | |
Token.Path: '#884444 underline', | |
} | |
def get_prompt_tokens(cli): | |
return [ | |
(Token.Username, 'john'), | |
(Token.At, '@'), | |
(Token.Host, 'localhost'), | |
(Token.Colon, ':'), | |
(Token.Path, '/user/john'), | |
(Token.Pound, '# '), | |
] | |
text = prompt(get_prompt_tokens=get_prompt_tokens, style=MyStyle) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment