Debian Codegraph @

examples/simple_app.py

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