Skip to content

Commit f6c85ef

Browse files
Complete refactoring of the layout engine.
- prompt_toolkit.layout now has a list of containers (HSplit, VSplit, FloatContainer) as well as a list of controls (TokenListControl, BufferControl) in order to design much more complex layouts. - All examples are refactored to understand the new layout definitions. - prompt_toolkit.contrib.shortcuts.get_input has been extended.
1 parent c66073d commit f6c85ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+3918
-2531
lines changed

examples/asyncio-prompt.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
sys.stdout = cli.stdout_proxy()
1919
"""
2020
from __future__ import unicode_literals
21-
from prompt_toolkit import CommandLineInterface, AbortAction, Exit, Abort
22-
from prompt_toolkit.layout.prompt import DefaultPrompt
23-
from prompt_toolkit.layout import Layout
21+
from prompt_toolkit import AbortAction, Exit, Abort
22+
from prompt_toolkit.contrib.shortcuts import create_cli
2423

2524
from pygments.style import Style
2625
from pygments.token import Token
@@ -56,9 +55,7 @@ def interactive_shell():
5655
Coroutine that shows the interactive command line.
5756
"""
5857
# Create interface. (style/layout is only for demonstration.)
59-
cli = CommandLineInterface(
60-
layout=Layout(before_input=DefaultPrompt(text='Say something inside the event loop: ')),
61-
style=TestStyle)
58+
cli = create_cli('Say something inside the event loop: ')
6259

6360
# Patch stdout in something that will always print *above* the prompt when
6461
# something is written to stdout.

examples/autocompletion.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@
99
"""
1010
from __future__ import unicode_literals
1111

12-
from prompt_toolkit import CommandLineInterface
13-
from prompt_toolkit.buffer import Buffer
14-
from prompt_toolkit.layout import Layout
15-
from prompt_toolkit.layout.prompt import DefaultPrompt
16-
from prompt_toolkit.layout.menus import CompletionsMenu
1712
from prompt_toolkit.contrib.completers import WordCompleter
13+
from prompt_toolkit.contrib.shortcuts import get_input
1814

1915
from pygments.token import Token
2016
from pygments.style import Style
@@ -66,16 +62,8 @@ class AnimalStyle(Style):
6662

6763

6864
def main():
69-
cli = CommandLineInterface(
70-
style=AnimalStyle,
71-
layout=Layout(before_input=DefaultPrompt('Give some animals: '),
72-
menus=[CompletionsMenu()]),
73-
buffer=Buffer(completer=animal_completer),
74-
create_async_autocompleters=True)
75-
76-
print('Press tab to complete')
77-
code_obj = cli.read_input()
78-
print('You said: ' + code_obj.text)
65+
text = get_input('Give some animals: ', completer=animal_completer, style=AnimalStyle)
66+
print('You said: %s' % text)
7967

8068

8169
if __name__ == '__main__':

examples/clock-input.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,32 @@
33
Example of a 'dynamic' prompt. On that shows the current time in the prompt.
44
"""
55
from prompt_toolkit import CommandLineInterface
6-
from prompt_toolkit.layout import Layout
7-
from prompt_toolkit.layout.prompt import Prompt
6+
from prompt_toolkit.layout import Window
7+
from prompt_toolkit.layout.controls import BufferControl
8+
from prompt_toolkit.layout.processors import Processor
9+
from prompt_toolkit.layout.utils import token_list_len
810
from pygments.token import Token
911

1012
import datetime
1113
import time
1214

1315

14-
class ClockPrompt(Prompt):
15-
def tokens(self, cli):
16+
class ClockPrompt(Processor):
17+
def run(self, cli, buffer, tokens):
1618
now = datetime.datetime.now()
17-
return [
19+
before = [
1820
(Token.Prompt, '%s:%s:%s' % (now.hour, now.minute, now.second)),
1921
(Token.Prompt, ' Enter something: ')
2022
]
2123

24+
return before + tokens, lambda i: i + token_list_len(before)
25+
26+
def invalidation_hash(self, cli, buffer):
27+
return datetime.datetime.now()
28+
2229

2330
def main():
24-
cli = CommandLineInterface(layout=Layout(before_input=ClockPrompt()))
31+
cli = CommandLineInterface(layout=Window(BufferControl(input_processors=[ClockPrompt()])))
2532

2633
def on_read_start():
2734
"""

examples/custom-key-binding.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def _(event):
2525
"""
2626
(Useless, but for demoing.)
2727
Typing 'xy' will insert 'z'.
28+
29+
Note that when you type for instance 'xa', the insertion of 'x' is
30+
postponed until the 'a' is typed. because we don't know earlier whether
31+
or not a 'y' will follow.
2832
"""
2933
event.cli.current_buffer.insert_text('z')
3034

examples/html-input.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@
44
"""
55
from pygments.lexers import HtmlLexer
66

7-
from prompt_toolkit import CommandLineInterface
8-
from prompt_toolkit.layout import Layout
9-
from prompt_toolkit.layout.prompt import DefaultPrompt
7+
from prompt_toolkit.contrib.shortcuts import get_input
108

119

1210
def main():
13-
cli = CommandLineInterface(layout=Layout(
14-
before_input=DefaultPrompt('Enter HTML: '),
15-
lexer=HtmlLexer))
16-
17-
html_code_obj = cli.read_input()
18-
print('You said: ' + html_code_obj.text)
11+
text = get_input('Enter HTML: ', lexer=HtmlLexer)
12+
print('You said: ' + text)
1913

2014

2115
if __name__ == '__main__':

examples/input-validation.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
#!/usr/bin/env python
22
"""
3-
Simple example of the layout options.
3+
Simple example of input validation.
44
"""
55
from __future__ import unicode_literals
66

7-
from prompt_toolkit import CommandLineInterface
8-
from prompt_toolkit.layout import Layout
9-
from prompt_toolkit.layout.prompt import DefaultPrompt
10-
from prompt_toolkit.layout.toolbars import ValidationToolbar
117
from prompt_toolkit.validation import Validator, ValidationError
12-
from prompt_toolkit.buffer import Buffer
8+
from prompt_toolkit.contrib.shortcuts import get_input
139

1410
from pygments.token import Token
1511
from pygments.style import Style
1612

1713

18-
layout = Layout(
19-
before_input=DefaultPrompt(text='Give an e-mail address: '),
20-
bottom_toolbars=[ValidationToolbar()]
21-
)
22-
23-
2414
class EmailValidator(Validator):
2515
def validate(self, document):
2616
if '@' not in document.text:
@@ -34,14 +24,8 @@ class TestStyle(Style):
3424

3525

3626
def main():
37-
cli = CommandLineInterface(
38-
layout=layout,
39-
style=TestStyle,
40-
buffer=Buffer(validator=EmailValidator()))
41-
42-
document = cli.read_input()
43-
print('You said: ' + document.text)
44-
27+
text = get_input('Enter e-mail address: ', validator=EmailValidator(), style=TestStyle)
28+
print('You said: %s' % text)
4529

4630
if __name__ == '__main__':
4731
main()

examples/layout-test.py

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)