blob: afd978d5fc44fb4e219382566814f3321fbb9012 (
plain)
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
|
#!/usr/bin/env python
"""
Example of a 'dynamic' prompt. On that shows the current time in the prompt.
"""
from __future__ import unicode_literals
from prompt_toolkit.interface import CommandLineInterface
from prompt_toolkit.application import Application
from prompt_toolkit.layout import Window
from prompt_toolkit.layout.controls import BufferControl
from prompt_toolkit.layout.processors import BeforeInput
from prompt_toolkit.shortcuts import create_eventloop
from prompt_toolkit.utils import Callback
from pygments.token import Token
import datetime
import time
def _clock_tokens(cli):
" Tokens to be shown before the prompt. "
now = datetime.datetime.now()
return [
(Token.Prompt, '%s:%s:%s' % (now.hour, now.minute, now.second)),
(Token.Prompt, ' Enter something: ')
]
def main():
eventloop = create_eventloop()
done = [False] # Non local
def on_read_start(cli):
"""
This function is called when we start reading at the input.
(Actually the start of the read-input event loop.)
"""
# Following function should be run in the background.
# We do it by using an executor thread from the `CommandLineInterface`
# instance.
def run():
# Send every second a redraw request.
while not done[0]:
time.sleep(1)
cli.request_redraw()
cli.eventloop.run_in_executor(run)
def on_read_end(cli):
done[0] = True
app = Application(
layout=Window(BufferControl(input_processors=[BeforeInput(_clock_tokens)])),
on_start=Callback(on_read_start),
on_stop=Callback(on_read_end))
cli = CommandLineInterface(application=app, eventloop=eventloop)
code_obj = cli.run()
print('You said: %s' % code_obj.text)
eventloop.close()
if __name__ == '__main__':
main()
|