Debian Codegraph @

examples/boilerplate.py

raw symbols SCIP
1 from __future__ import annotations
2
3 import sys
4 from argparse import ArgumentParser, Namespace
5
6 import pytermgui as ptg
7
8
9 def _process_arguments(argv: list[str] | None = None) -> Namespace:
10 """Processes command line arguments.
11
12 Note that you don't _have to_ use the bultin argparse module for this; it
13 is just what the module uses.
14
15 Args:
16 argv: A list of command line arguments, not including the binary path
17 (sys.argv[0]).
18 """
19
20 parser = ArgumentParser(description="My first PTG application.")
21
22 return parser.parse_args(argv)
23
24
25 def _create_aliases() -> None:
26 """Creates all the TIM aliases used by the application.
27
28 Aliases should generally follow the following format:
29
30 namespace.item
31
32 For example, the title color of an app named "myapp" could be something like:
33
34 myapp.title
35 """
36
37
38 def _configure_widgets() -> None:
39 """Defines all the global widget configurations.
40
41 Some example lines you could use here:
42
43 ptg.boxes.DOUBLE.set_chars_of(ptg.Window)
44 ptg.Splitter.set_char("separator", " ")
45 ptg.Button.styles.label = "myapp.button.label"
46 ptg.Container.styles.border__corner = "myapp.border"
47 """
48
49 ptg.boxes.SINGLE.set_chars_of(ptg.Window)
50
51
52 def _define_layout() -> ptg.Layout:
53 """Defines the application layout.
54
55 Layouts work based on "slots" within them. Each slot can be given dimensions for
56 both width and height. Integer values are interpreted to mean a static width, float
57 values will be used to "scale" the relevant terminal dimension, and giving nothing
58 will allow PTG to calculate the corrent dimension.
59 """
60
61 layout = ptg.Layout()
62
63 # A header slot with a height of 1
64 layout.add_slot("Header", height=1)
65 layout.add_break()
66
67 # A body slot that will fill the entire width, and the height is remaining
68 layout.add_slot("Body")
69
70 # A slot in the same row as body, using the full non-occupied height and
71 # 20% of the terminal's height.
72 layout.add_slot("Body right", width=0.2)
73
74 layout.add_break()
75
76 # A footer with a static height of 1
77 layout.add_slot("Footer", height=1)
78
79 return layout
80
81
82 def main(argv: list[str] | None = None) -> None:
83 """Runs the application."""
84
85 _create_aliases()
86 _configure_widgets()
87
88 args = _process_arguments(argv)
89
90 with ptg.WindowManager() as manager:
91 manager.layout = _define_layout()
92
93 header = ptg.Window(
94 "[app.header] Welcome to PyTermGUI ",
95 box="EMPTY",
96 )
97
98 # Since header is the first defined slot, this will assign to the correct place
99 manager.add(header)
100
101 footer = ptg.Window(ptg.Button("Quit", lambda *_: manager.stop()), box="EMPTY")
102
103 # Since the second slot, body was not assigned to, we need to manually assign
104 # to "footer"
105 manager.add(footer, assign="footer")
106
107 manager.add(ptg.Window("My sidebar"), assign="body_right")
108 manager.add(ptg.Window("My body window"), assign="body")
109
110 ptg.tim.print("[!gradient(210)]Goodbye!")
111
112
113 if __name__ == "__main__":
114 main(sys.argv[1:])