-
Notifications
You must be signed in to change notification settings - Fork 0
/
boilerplate.py
116 lines (73 loc) · 3.11 KB
/
boilerplate.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
# PyTermGUI example under MIT license
from __future__ import annotations
import sys
from argparse import ArgumentParser, Namespace
import pytermgui as ptg
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
"""
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.SINGLE.set_chars_of(ptg.Window)
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 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",
)
# Since header is the first defined slot, this will assign to the correct place
manager.add(header)
footer = ptg.Window(ptg.Button("Quit", lambda *_: manager.stop()), box="EMPTY")
# 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("My body window"), assign="body")
ptg.tim.print("[!gradient(210)]Goodbye!")
if __name__ == "__main__":
main(sys.argv[1:])