-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_app.py
215 lines (154 loc) · 5.93 KB
/
simple_app.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# PyTermGUI example under MIT license
"""A simple application using examples/boilerplate.py as a basis."""
from __future__ import annotations
import sys
from argparse import ArgumentParser, Namespace
import pytermgui as ptg
PALETTE_LIGHT = "#FCBA03"
PALETTE_MID = "#8C6701"
PALETTE_DARK = "#4D4940"
PALETTE_DARKER = "#242321"
def _process_arguments(argv: list[str] | None = None) -> Namespace:
"""Processes command line arguments.
Note that you don't _have to_ use the bultin argparse module for this; it
is just what the module uses.
Args:
argv: A list of command line arguments, not including the binary path
(sys.argv[0]).
"""
parser = ArgumentParser(description="My first PTG application.")
return parser.parse_args(argv)
def _create_aliases() -> None:
"""Creates all the TIM aliases used by the application.
Aliases should generally follow the following format:
namespace.item
For example, the title color of an app named "myapp" could be something like:
myapp.title
"""
ptg.tim.alias("app.text", "#cfc7b0")
ptg.tim.alias("app.header", f"bold @{PALETTE_MID} #d9d2bd")
ptg.tim.alias("app.header.fill", f"@{PALETTE_LIGHT}")
ptg.tim.alias("app.title", f"bold {PALETTE_LIGHT}")
ptg.tim.alias("app.button.label", f"bold @{PALETTE_DARK} app.text")
ptg.tim.alias("app.button.highlight", "inverse app.button.label")
ptg.tim.alias("app.footer", f"@{PALETTE_DARKER}")
def _configure_widgets() -> None:
"""Defines all the global widget configurations.
Some example lines you could use here:
ptg.boxes.DOUBLE.set_chars_of(ptg.Window)
ptg.Splitter.set_char("separator", " ")
ptg.Button.styles.label = "myapp.button.label"
ptg.Container.styles.border__corner = "myapp.border"
"""
ptg.boxes.DOUBLE.set_chars_of(ptg.Window)
ptg.boxes.ROUNDED.set_chars_of(ptg.Container)
ptg.Button.styles.label = "app.button.label"
ptg.Button.styles.highlight = "app.button.highlight"
ptg.Slider.styles.filled__cursor = PALETTE_MID
ptg.Slider.styles.filled_selected = PALETTE_LIGHT
ptg.Label.styles.value = "app.text"
ptg.Window.styles.border__corner = "#C2B280"
ptg.Container.styles.border__corner = PALETTE_DARK
ptg.Splitter.set_char("separator", "")
def _define_layout() -> ptg.Layout:
"""Defines the application layout.
Layouts work based on "slots" within them. Each slot can be given dimensions for
both width and height. Integer values are interpreted to mean a static width, float
values will be used to "scale" the relevant terminal dimension, and giving nothing
will allow PTG to calculate the corrent dimension.
"""
layout = ptg.Layout()
# A header slot with a height of 1
layout.add_slot("Header", height=1)
layout.add_break()
# A body slot that will fill the entire width, and the height is remaining
layout.add_slot("Body")
# A slot in the same row as body, using the full non-occupied height and
# 20% of the terminal's height.
layout.add_slot("Body right", width=0.2)
layout.add_break()
# A footer with a static height of 1
layout.add_slot("Footer", height=1)
return layout
def _confirm_quit(manager: ptg.WindowManager) -> None:
"""Creates an "Are you sure you want to quit" modal window"""
modal = ptg.Window(
"[app.title]Are you sure you want to quit?",
"",
ptg.Container(
ptg.Splitter(
ptg.Button("Yes", lambda *_: manager.stop()),
ptg.Button("No", lambda *_: modal.close()),
),
),
).center()
modal.select(1)
manager.add(modal)
def main(argv: list[str] | None = None) -> None:
"""Runs the application."""
_create_aliases()
_configure_widgets()
args = _process_arguments(argv)
with ptg.WindowManager() as manager:
manager.layout = _define_layout()
header = ptg.Window(
"[app.header] Welcome to PyTermGUI ",
box="EMPTY",
is_persistant=True,
)
header.styles.fill = "app.header.fill"
# Since header is the first defined slot, this will assign to the correct place
manager.add(header)
footer = ptg.Window(
ptg.Button("Quit", lambda *_: _confirm_quit(manager)),
box="EMPTY",
)
footer.styles.fill = "app.footer"
# Since the second slot, body was not assigned to, we need to manually assign
# to "footer"
manager.add(footer, assign="footer")
manager.add(
ptg.Window("My sidebar"),
assign="body_right",
)
manager.add(
ptg.Window(
"[app.title]Example widgets",
"",
ptg.Collapsible(
"Some sliders",
"",
ptg.Container(
ptg.Slider(),
ptg.Slider(),
ptg.Slider(),
static_width=40,
),
),
"",
ptg.Collapsible(
"XTERM color picker",
"",
ptg.ColorPicker(),
),
"",
ptg.Collapsible(
"Some containers within a splitter",
"",
ptg.Container(
ptg.Splitter(
ptg.Container("One"),
ptg.Container("Two"),
ptg.Container("Three"),
),
static_width=60,
),
),
vertical_align=ptg.VerticalAlignment.TOP,
overflow=ptg.Overflow.SCROLL,
),
assign="body",
)
ptg.tim.print(f"[{PALETTE_LIGHT}]Goodbye!")
if __name__ == "__main__":
main(sys.argv[1:])