Skip to content

Instantly share code, notes, and snippets.

@renanivo
Created January 1, 2016 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save renanivo/44efe7dbb52790f431a0 to your computer and use it in GitHub Desktop.
Save renanivo/44efe7dbb52790f431a0 to your computer and use it in GitHub Desktop.
First steps with prompt toolkit (Python 3)
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()
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())
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