Skip to content

Commit 5b04955

Browse files
Whole refactoring.
- A lot of refactoring in the layout. The Prompt class became Layout. - Over the whole project, parametrization of objects happens through the constructor.
1 parent 9c0d049 commit 5b04955

27 files changed

+1478
-1313
lines changed

examples/autocompletion.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
from __future__ import unicode_literals
1111

1212
from prompt_toolkit import CommandLineInterface
13-
from prompt_toolkit.code import Code, Completion
13+
from prompt_toolkit.completion import Completer, Completion
1414
from prompt_toolkit.line import Line
15+
from prompt_toolkit.layout import Layout
16+
from prompt_toolkit.layout.prompt import DefaultPrompt
17+
from prompt_toolkit.layout.menus import CompletionMenu
1518

1619
from pygments.token import Token
1720
from pygments.style import Style
1821

1922

20-
class AnimalCode(Code):
23+
class AnimalCompleter(Completer):
2124
animals = [
2225
'alligator',
2326
'ant',
@@ -53,8 +56,15 @@ class AnimalCode(Code):
5356
'turtle',
5457
]
5558

56-
def get_completions(self):
57-
word_before_cursor = self.document.get_word_before_cursor()
59+
def complete_after_insert_text(self, document):
60+
"""
61+
Open completion menu when we type a character.
62+
(Except if we typed whitespace.)
63+
"""
64+
return not document.char_before_cursor.isspace()
65+
66+
def get_completions(self, document):
67+
word_before_cursor = document.get_word_before_cursor()
5868

5969
for a in self.animals:
6070
if a.startswith(word_before_cursor):
@@ -70,23 +80,12 @@ class AnimalStyle(Style):
7080
}
7181

7282

73-
class AnimalLine(Line):
74-
def complete_after_insert_text(self):
75-
"""
76-
Open completion menu when we type a character.
77-
(Except if we typed whitespace.)
78-
"""
79-
return not self.document.char_before_cursor.isspace()
80-
81-
82-
class AnimalCLI(CommandLineInterface):
83-
code_factory = AnimalCode
84-
line_factory = AnimalLine
85-
style = AnimalStyle
86-
87-
8883
def main():
89-
cli = AnimalCLI()
84+
cli = CommandLineInterface(style=AnimalStyle,
85+
layout=Layout(before_input=DefaultPrompt('Give some animals: '),
86+
menus=[CompletionMenu()]),
87+
line=Line(completer=AnimalCompleter())
88+
)
9089

9190
print('Press tab to complete')
9291
code_obj = cli.read_input()

examples/clock-input.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,25 @@
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.prompt import Prompt
6+
from prompt_toolkit.layout import Layout
7+
from prompt_toolkit.layout.prompt import Prompt
78
from pygments.token import Token
89

910
import datetime
1011
import time
1112

1213

1314
class ClockPrompt(Prompt):
14-
def get_tokens_before_input(self):
15+
def tokens(self, cli):
1516
now = datetime.datetime.now()
1617
return [
1718
(Token.Prompt, '%s:%s:%s' % (now.hour, now.minute, now.second)),
1819
(Token.Prompt, ' Enter something: ')
1920
]
2021

2122

22-
class ClockCLI(CommandLineInterface):
23-
prompt_factory = ClockPrompt
24-
enable_concurency = True
2523

24+
class ClockCLI(CommandLineInterface):
2625
def on_read_input_start(self):
2726
self.run_in_executor(self._clock_update)
2827

@@ -34,7 +33,7 @@ def _clock_update(self):
3433

3534

3635
def main():
37-
cli = ClockCLI()
36+
cli = ClockCLI(layout=Layout(before_input=ClockPrompt()))
3837

3938
code_obj = cli.read_input()
4039
print('You said: ' + code_obj.text)

examples/get-password.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55

66
if __name__ == '__main__':
7-
password = get_input('Give me some input: ', is_password=True)
7+
password = get_input('Password: ', is_password=True)
88
print('You said: %s' % password)

examples/html-input.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,14 @@
55
from pygments.lexers import HtmlLexer
66

77
from prompt_toolkit import CommandLineInterface
8-
from prompt_toolkit.code import Code
9-
from prompt_toolkit.prompt import Prompt
10-
11-
12-
class HtmlCode(Code):
13-
lexer = HtmlLexer
14-
15-
16-
class HtmlPrompt(Prompt):
17-
prompt_text = 'Enter HTML: '
18-
19-
20-
class HtmlCLI(CommandLineInterface):
21-
code_factory = HtmlCode
22-
prompt_factory = HtmlPrompt
8+
from prompt_toolkit.layout import Layout
9+
from prompt_toolkit.layout.prompt import DefaultPrompt
2310

2411

2512
def main():
26-
cli = HtmlCLI()
13+
cli = CommandLineInterface(layout=Layout(
14+
before_input=DefaultPrompt('Enter HTML: '),
15+
lexer=HtmlLexer))
2716

2817
html_code_obj = cli.read_input()
2918
print('You said: ' + html_code_obj.text)

examples/input-validation.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
"""
3+
Simple example of the layout options.
4+
"""
5+
from __future__ import unicode_literals
6+
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
11+
from prompt_toolkit.validation import Validator, ValidationError
12+
from prompt_toolkit.line import Line
13+
14+
from pygments.token import Token
15+
from pygments.style import Style
16+
17+
18+
layout = Layout(
19+
before_input = DefaultPrompt(text='Give an e-mail address: '),
20+
bottom_toolbars = [ValidationToolbar()]
21+
)
22+
23+
class EmailValidator(Validator):
24+
def validate(self, document):
25+
if not '@' in document.text:
26+
raise ValidationError(message='Not a valid e-mail address')
27+
28+
29+
class TestLine(Line):
30+
is_multiline = True
31+
32+
33+
class TestStyle(Style):
34+
styles = {
35+
Token.ValidationToolbar: 'bg:#aa0000 #ffbbbb',
36+
}
37+
38+
39+
def main():
40+
cli = CommandLineInterface(layout=layout, style=TestStyle, line=Line(validator=EmailValidator()))
41+
42+
document = cli.read_input()
43+
print('You said: ' + document.text)
44+
45+
46+
if __name__ == '__main__':
47+
main()

examples/layout-test.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
"""
3+
Simple example of the layout options.
4+
"""
5+
from __future__ import unicode_literals
6+
7+
from prompt_toolkit import CommandLineInterface
8+
from prompt_toolkit.layout import Layout
9+
from prompt_toolkit.layout.prompt import DefaultPrompt, Prompt
10+
from prompt_toolkit.layout.margins import LeftMarginWithLineNumbers
11+
from prompt_toolkit.layout.menus import CompletionMenu
12+
from prompt_toolkit.layout.toolbars import TextToolbar, ArgToolbar, SearchToolbar, CompletionToolbar
13+
from prompt_toolkit.line import Line
14+
from prompt_toolkit.completion import Completion, Completer
15+
16+
from pygments.token import Token
17+
from pygments.style import Style
18+
19+
20+
lipsum = """This is the input:
21+
22+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer blandit
23+
elementum ante, vel fermentum massa fermentum vitae. Nulla ornare egestas
24+
metus, ut molestie lacus sodales lacinia. Vivamus lacinia, lectus at laoreet
25+
fermentum, quam ligula hendrerit massa, in vulputate velit lacus eu tortor. Sed
26+
mi dui, iaculis nec odio iaculis, iaculis pellentesque velit. Duis consequat,
27+
felis vitae hendrerit accumsan, lectus massa volutpat quam, quis scelerisque ex
28+
urna eu neque. Phasellus vitae pharetra tellus, dapibus viverra lectus. Quisque
29+
ornare risus sit amet auctor convallis. Ut vestibulum tincidunt orci vitae
30+
tincidunt. Quisque ornare consectetur elementum."""
31+
32+
33+
class TestCompleter(Completer):
34+
def get_completions(self, document):
35+
word_before_cursor = document.get_word_before_cursor()
36+
37+
for i in range(0, 20):
38+
yield Completion('Completion %i' % i, -len(word_before_cursor))
39+
40+
41+
layout = Layout(
42+
left_margin = LeftMarginWithLineNumbers(),
43+
before_input = DefaultPrompt(text='Before input >> '),
44+
after_input = Prompt(' << after input'),
45+
top_toolbars = [
46+
TextToolbar('This is a top toolbar', token=Token.TopToolbar1),
47+
TextToolbar('This is another top toolbar', token=Token.TopToolbar2),
48+
],
49+
bottom_toolbars = [
50+
ArgToolbar(),
51+
SearchToolbar(),
52+
CompletionToolbar(),
53+
TextToolbar('This is a bottom toolbar', token=Token.BottomToolbar1),
54+
TextToolbar('This is another bottom toolbar', token=Token.BottomToolbar2),
55+
],
56+
show_tildes=True,
57+
menus=[CompletionMenu()])
58+
59+
60+
class TestStyle(Style):
61+
styles = {
62+
Token.Layout.LeftMargin: 'bg:#00aaaa #000000',
63+
Token.Prompt.BeforeInput: 'bg:#aa2266 #ffffff',
64+
Token.AfterInput: 'bg:#aa2266 #ffffff',
65+
Token.BottomToolbar1: 'bg:#440044 #ffffff',
66+
Token.BottomToolbar2: 'bg:#aa0088 #222222',
67+
Token.TopToolbar1: 'bg:#aa0088 #222222',
68+
Token.TopToolbar2: 'bg:#440044 #ffffff',
69+
70+
Token.Layout.Toolbar.Arg: 'bg:#aaaaff #000088',
71+
Token.Layout.Toolbar.Arg.Text: 'bg:#aaaaff #000088 bold',
72+
73+
Token.CompletionMenu.Completion.Current: 'bg:#00aaaa #000000',
74+
Token.CompletionMenu.Completion: 'bg:#008888 #ffffff',
75+
Token.CompletionMenu.ProgressButton: 'bg:#003333',
76+
Token.CompletionMenu.ProgressBar: 'bg:#00aaaa',
77+
78+
Token.CompletionToolbar: 'bg:#888800 #000000',
79+
Token.CompletionToolbar.Arrow: 'bg:#888800 #000000',
80+
Token.CompletionToolbar.Completion: 'bg:#aaaa00 #000000',
81+
Token.CompletionToolbar.Completion.Current: 'bg:#ffffaa #000000 bold',
82+
83+
Token.SelectedText: 'bg:#000088 #ffffff',
84+
}
85+
86+
87+
def main():
88+
cli = CommandLineInterface(layout=layout,
89+
style=TestStyle,
90+
line=Line(is_multiline=True, completer=TestCompleter()))
91+
92+
code_obj = cli.read_input(initial_value=lipsum)
93+
print('You said: ' + code_obj.text)
94+
95+
96+
if __name__ == '__main__':
97+
main()

examples/python-input.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
"""
3+
Autocompletion example.
4+
5+
Press [Tab] to complete the current word.
6+
- The first Tab press fills in the common part of all completions.
7+
- The second Tab press shows all the completions. (In the menu)
8+
- Any following tab press cycles through all the possible completions.
9+
"""
10+
from __future__ import unicode_literals
11+
12+
from prompt_toolkit.contrib.python_input import PythonCommandLineInterface
13+
14+
15+
16+
17+
def main():
18+
cli = PythonCommandLineInterface()
19+
20+
code_obj = cli.read_input()
21+
print('You said: ' + code_obj.text)
22+
23+
24+
if __name__ == '__main__':
25+
main()

0 commit comments

Comments
 (0)