Debian Codegraph @

docs/widgets/styling.md

raw symbols SCIP
1 All built in widgets use a central color [palette](/reference/pytermgui/palettes/#pytermgui.palettes.Palette), referencable within [TIM](/tim) code. Each widget will use appropriate shades of the palette by default.
2
3 Since this palette is defined globally, you can also re-generate it with new colors. It only requires a primary color, but you may give it as many as you please.
4
5 Here is the default palette:
6
7 ```termage-svg width=91 height=44 title=pytermgui/palettes.py
8 from pytermgui import palette
9 palette.print()
10 ```
11
12 As you can see, each color name has a set of negatively and positively shaded variants. These can be accessed in the format: `{color_name}{shade_offset}`, such as `primary+2` or `surface2-1`. The "main" color can be referred to without an offset, like `accent` or `success`.
13
14 Each color also has a background variant assigned to it, which may be used by prefixing the color-of-interest by an at symbol (`@`), like `@error` or `@secondary+3`.
15
16 !!! warning
17
18 Since the underlying colors _may_ change between runs, it's best to make sure contrast ratios are met at all times. This is possible in PTG using the `#auto` markup pseudo-tag.
19
20 As mentioned later, the library will try to append this tag to any widget style without a set foreground color. To use it outside of a widget (or to make absolute certain that it applies), add it to your markup:
21
22 ```termage chrome=false height=2
23 from pytermgui import tim
24
25 tim.alias_multiple(**{"@my-surface1": "@white", "@my-surface2": "@black"})
26 tim.print("[@my-surface1 #auto] Black on white [@my-surface2 #auto] White on black ")
27 ```
28
29
30 ## Styling widgets
31
32 As mentioned above, all widgets come with a basic, aesthetically pleasing set of styles. Sometimes though, you might wanna get a fresher look.
33
34
35 ### Modifying the palette
36
37 The easiest way to change up the look of your apps is by manipulating the global palette. This will change the colors of all built-in widgets, and all custom ones (so long as they use the palette colors).
38
39 This can be done using the [Palette.regenerate](/reference/pytermgui/palettes/#pytermgui.palettes.Palette.regenerate) method:
40
41 ```python
42 from pytermgui import palette
43
44 palette.regenerate(primary="skyblue")
45 ```
46
47 Under the hood, it will look at the given color, and generate some complementaries for it.
48
49 The above code gives use the following palette, by the way:
50
51 ```termage-svg width=91 height=44 title=
52 from pytermgui import palette
53
54 palette.regenerate(primary="skyblue").print()
55 ```
56
57 !!! note
58 Using highly saturated colors as for the primary color will likely result in harsh looking styles. Even if you are tied to a certain neon-looking HEX color, it is usually nicer to use some toned-down version of it as the primary, and possibly including it as the `secondary`, `tertiary` or `accent` arguments.
59
60
61 ### Modifying styles
62
63 If just changing the base colors isn't enough, you can add more involved styles on a per-widget basis.
64
65 Assigning styles is done through each widget's [StyleManager](/reference/pytermgui/widgets/styles/#pytermgui.widgets.styles.StyleManager), which can be accessed using `widget.styles`.
66
67 Properties can be set in a couple of ways. The simplest one uses the dot syntax:
68
69 ```python
70 widget.styles.label = my_style
71 ```
72
73 You may also assign styles in the same statement as a widget's creation if you are pressed on space, by calling the `styles` property with a set of keyword arguments:
74
75 ```python
76 widget = MyWidget().style(label=my_style)
77 ```
78
79 Finally, it is possible to assign multiple styles to the same value in one statement. This can be done by separating each key with 2 underscore characters, and works for either of the above methods:
80
81 ```python
82 widget.styles.border__corner = my_border_style
83 widget2 = MyWidget().styles(border__corner=my_border_style)
84 ```
85
86 All the above examples modify styles pre-defined by the widget at declaration time. To see all the built-in styles, see the section on [built-in widgets](/widgets/builtins).
87
88 Values for these calls can be of 2 general types:
89
90
91 #### TIM string shorthands
92
93 The easier way to create custom styles is by defining some markup for them. A fully expanded markup string contains both the `{depth}` and `{item}` template keys, but it can be shortened and [expanded automatically](/reference/pytermgui/widgets/styles/#pytermgui.widgets.styles.StyleManager.expand_shorthand) for convenience.
94
95 `item` must be in the final style string. It represents the string that was passed for styling. `depth` is not necessary to use; it represents the given widget's `depth` property.
96
97 For example, `surface+2 italic dim` would be expanded into `[surface+2 italic dim]{item}`, while `[!gradient(210) bold]{item}` would remain untouched.
98
99 !!! info
100 Under the hood, these strings will create [MarkupFormatter](/reference/pytermgui/widgets/styles/#pytermgui.widgets.styles.MarkupFormatter) instances, which perform the formatting and parsing when called.
101
102 TL;DR: This method is only syntactic sugar over the one below.
103
104
105 #### Custom callables
106
107 For more granular control of a style, one may use a callable. These follow the following signature:
108
109 ```python
110 def my_style(depth: int, item: str) -> str:
111 ```
112
113 ...where `depth` is an integer related to the styled widget, and `item` is the text in need of styling. The return of this is always assumed to be parsed, ANSI-coded text.
114
115 This method is far more powerful than just using template string, but it's also a lot more noisy to write. Here is a cool button style:
116
117 ```python
118 --8<-- "docs/src/widgets/style_callables.py"
119 ```
120
121 This gives the following result. See the `#auto` tag setting valid foregrounds for both light and dark backgrounds:
122
123 ```termage-svg include=docs/src/widgets/style_callables.py title=docs/src/widgets/style_callables.py height=5
124 ```