Skip to content

Commit 42b48d1

Browse files
committed
Add an example
1 parent 925cb42 commit 42b48d1

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

Diff for: examples/full-screen/simple-demos/word-wrapping.py

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#!/usr/bin/env python
2+
"""
3+
An example of a BufferControl in a full screen layout that offers auto
4+
completion.
5+
6+
Important is to make sure that there is a `CompletionsMenu` in the layout,
7+
otherwise the completions won't be visible.
8+
"""
9+
10+
from prompt_toolkit.application import Application
11+
from prompt_toolkit.buffer import Buffer
12+
from prompt_toolkit.filters import Condition
13+
from prompt_toolkit.formatted_text import HTML, to_formatted_text
14+
from prompt_toolkit.key_binding import KeyBindings
15+
from prompt_toolkit.layout.containers import Float, FloatContainer, HSplit, Window
16+
from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl
17+
from prompt_toolkit.layout.layout import Layout
18+
from prompt_toolkit.layout.menus import CompletionsMenu
19+
20+
LIPSUM = " ".join(
21+
"""\
22+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas
23+
quis interdum enim. Nam viverra, mauris et blandit malesuada, ante est bibendum
24+
mauris, ac dignissim dui tellus quis ligula. Aenean condimentum leo at
25+
dignissim placerat. In vel dictum ex, vulputate accumsan mi. Donec ut quam
26+
placerat massa tempor elementum. Sed tristique mauris ac suscipit euismod. Ut
27+
tempus vehicula augue non venenatis. Mauris aliquam velit turpis, nec congue
28+
risus aliquam sit amet. Pellentesque blandit scelerisque felis, faucibus
29+
consequat ante. Curabitur tempor tortor a imperdiet tincidunt. Nam sed justo
30+
sit amet odio bibendum congue. Quisque varius ligula nec ligula gravida, sed
31+
convallis augue faucibus. Nunc ornare pharetra bibendum. Praesent blandit ex
32+
quis sodales maximus.""".split("\n")
33+
)
34+
35+
36+
def get_line_prefix(lineno, wrap_count):
37+
if wrap_count == 0:
38+
return HTML('[%s] <style bg="orange" fg="black">--&gt;</style> ') % lineno
39+
40+
text = str(lineno) + "-" + "*" * (lineno // 2) + ": "
41+
return HTML('[%s.%s] <style bg="ansigreen" fg="ansiblack">%s</style>') % (
42+
lineno,
43+
wrap_count,
44+
text,
45+
)
46+
47+
48+
# Global wrap lines flag.
49+
wrap_lines = True
50+
51+
52+
# The layout
53+
buff = Buffer(complete_while_typing=True)
54+
buff.text = LIPSUM
55+
56+
57+
body = FloatContainer(
58+
content=HSplit(
59+
[
60+
Window(
61+
FormattedTextControl(
62+
'Press "q" to quit. Press "w" to enable/disable wrapping.'
63+
),
64+
height=1,
65+
style="reverse",
66+
),
67+
# Break words
68+
Window(
69+
BufferControl(buffer=buff),
70+
wrap_lines=Condition(lambda: wrap_lines),
71+
word_wrap=False,
72+
),
73+
# Default word wrapping
74+
Window(
75+
BufferControl(buffer=buff),
76+
wrap_lines=Condition(lambda: wrap_lines),
77+
word_wrap=True,
78+
),
79+
# Add a marker to signify continuation
80+
Window(
81+
BufferControl(buffer=buff),
82+
wrap_lines=True,
83+
wrap_finder=Window._whitespace_wrap_finder(
84+
lambda n: [] if n else to_formatted_text(LIPSUM),
85+
continuation=to_formatted_text(" ⮠"),
86+
),
87+
get_line_prefix=lambda lineno, wrap_count: to_formatted_text(" ⭢ ")
88+
if wrap_count
89+
else [],
90+
),
91+
# Truncating (only wrap the first timle around)
92+
Window(
93+
BufferControl(buffer=buff),
94+
wrap_lines=True,
95+
wrap_finder=lambda lineno,
96+
wrap_count,
97+
start,
98+
end,
99+
fallback=Window._whitespace_wrap_finder(
100+
lambda n: [] if n else to_formatted_text(LIPSUM),
101+
): (end - 3, -1, "...")
102+
if wrap_count > 0
103+
else fallback(lineno, wrap_count, start, end),
104+
),
105+
]
106+
),
107+
floats=[
108+
Float(
109+
xcursor=True,
110+
ycursor=True,
111+
content=CompletionsMenu(max_height=16, scroll_offset=1),
112+
)
113+
],
114+
)
115+
116+
117+
# Key bindings
118+
kb = KeyBindings()
119+
120+
121+
@kb.add("q")
122+
@kb.add("c-c")
123+
def _(event):
124+
"Quit application."
125+
event.app.exit()
126+
127+
128+
@kb.add("w")
129+
def _(event):
130+
"Disable/enable wrapping."
131+
global wrap_lines
132+
wrap_lines = not wrap_lines
133+
134+
135+
# The `Application`
136+
application = Application(
137+
layout=Layout(body), key_bindings=kb, full_screen=True, mouse_support=True
138+
)
139+
140+
141+
def run():
142+
application.run()
143+
144+
145+
if __name__ == "__main__":
146+
run()

0 commit comments

Comments
 (0)