Skip to content

Commit 6cb00fe

Browse files
Added examples.
1 parent 74a02db commit 6cb00fe

File tree

3 files changed

+165
-43
lines changed

3 files changed

+165
-43
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
"""
3+
A simple example of a scrollable pane.
4+
"""
5+
from prompt_toolkit.application import Application
6+
from prompt_toolkit.application.current import get_app
7+
from prompt_toolkit.key_binding import KeyBindings
8+
from prompt_toolkit.key_binding.bindings.focus import focus_next, focus_previous
9+
from prompt_toolkit.layout import Dimension, HSplit, Layout, ScrollablePane
10+
from prompt_toolkit.widgets import Frame, Label, TextArea
11+
12+
13+
def main():
14+
# Create a big layout of many text areas, then wrap them in a `ScrollablePane`.
15+
root_container = Frame(
16+
ScrollablePane(
17+
HSplit(
18+
[
19+
Frame(TextArea(text=f"label-{i}"), width=Dimension())
20+
for i in range(20)
21+
]
22+
)
23+
)
24+
# ScrollablePane(HSplit([TextArea(text=f"label-{i}") for i in range(20)]))
25+
)
26+
27+
layout = Layout(container=root_container)
28+
29+
# Key bindings.
30+
kb = KeyBindings()
31+
32+
@kb.add("c-c")
33+
def exit(event) -> None:
34+
get_app().exit()
35+
36+
kb.add("tab")(focus_next)
37+
kb.add("s-tab")(focus_previous)
38+
39+
# Create and run application.
40+
application = Application(layout=layout, key_bindings=kb, full_screen=True)
41+
application.run()
42+
43+
44+
if __name__ == "__main__":
45+
main()

examples/full-screen/scrollable-panes/test1.py

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env python
2+
"""
3+
A simple example of a scrollable pane.
4+
"""
5+
from prompt_toolkit.application import Application
6+
from prompt_toolkit.application.current import get_app
7+
from prompt_toolkit.completion import WordCompleter
8+
from prompt_toolkit.key_binding import KeyBindings
9+
from prompt_toolkit.key_binding.bindings.focus import focus_next, focus_previous
10+
from prompt_toolkit.layout import (
11+
CompletionsMenu,
12+
Float,
13+
FloatContainer,
14+
HSplit,
15+
Layout,
16+
ScrollablePane,
17+
VSplit,
18+
)
19+
from prompt_toolkit.widgets import Frame, Label, TextArea
20+
21+
22+
def main():
23+
# Create a big layout of many text areas, then wrap them in a `ScrollablePane`.
24+
root_container = VSplit(
25+
[
26+
Label("<left column>"),
27+
HSplit(
28+
[
29+
Label("ScrollContainer Demo"),
30+
Frame(
31+
ScrollablePane(
32+
HSplit(
33+
[
34+
Frame(
35+
TextArea(
36+
text=f"label-{i}",
37+
completer=animal_completer,
38+
)
39+
)
40+
for i in range(20)
41+
]
42+
)
43+
),
44+
),
45+
]
46+
),
47+
]
48+
)
49+
50+
root_container = FloatContainer(
51+
root_container,
52+
floats=[
53+
Float(
54+
xcursor=True,
55+
ycursor=True,
56+
content=CompletionsMenu(max_height=16, scroll_offset=1),
57+
),
58+
],
59+
)
60+
61+
layout = Layout(container=root_container)
62+
63+
# Key bindings.
64+
kb = KeyBindings()
65+
66+
@kb.add("c-c")
67+
def exit(event) -> None:
68+
get_app().exit()
69+
70+
kb.add("n")(focus_next)
71+
kb.add("p")(focus_previous)
72+
kb.add("tab")(focus_next)
73+
kb.add("s-tab")(focus_previous)
74+
75+
# Create and run application.
76+
application = Application(layout=layout, key_bindings=kb, full_screen=True)
77+
application.run()
78+
79+
80+
animal_completer = WordCompleter(
81+
[
82+
"alligator",
83+
"ant",
84+
"ape",
85+
"bat",
86+
"bear",
87+
"beaver",
88+
"bee",
89+
"bison",
90+
"butterfly",
91+
"cat",
92+
"chicken",
93+
"crocodile",
94+
"dinosaur",
95+
"dog",
96+
"dolphin",
97+
"dove",
98+
"duck",
99+
"eagle",
100+
"elephant",
101+
"fish",
102+
"goat",
103+
"gorilla",
104+
"kangaroo",
105+
"leopard",
106+
"lion",
107+
"mouse",
108+
"rabbit",
109+
"rat",
110+
"snake",
111+
"spider",
112+
"turkey",
113+
"turtle",
114+
],
115+
ignore_case=True,
116+
)
117+
118+
119+
if __name__ == "__main__":
120+
main()

0 commit comments

Comments
 (0)