Debian Codegraph @

docs/tim/tags.md

raw symbols SCIP
1 ## Basics
2
3 **TIM** syntax tends to be simple. At the base, we have the following structure:
4
5 ```
6 [tag1 tag2 ...]My content
7 ^ ^
8 tag group plain text
9 ```
10
11 ...where `tag1` & `tag2` are some valid markup tags, and `My content` is the string you want them to modify.
12
13 Tag groups are always denoted by square brackets (`[` and `]`). All text outside of a tag group is considered to be plain text.
14
15
16
17 ### Nesting
18
19 When encountering nested tag groups, only the innermost one will be parsed:
20
21
22 ```termage include=docs/src/tim/tags_innermost.py chrome=false height=1 tabs=TIM,Output
23 ```
24
25
26 ### Escaping
27
28 You can escape a tag group using backslashes (`\`):
29
30
31 ```termage include=docs/src/tim/tags_escape.py chrome=false height=1 tabs=TIM,Output
32 ```
33
34 !!! warning "Re-parsing escaped markup"
35
36 Escaping tag groups will only be effective for the first parsing of said string. During parsing, the escaped tag group will lose the backslash to hide it from the output.
37
38
39 ## Styles
40
41 We support all generally supported terminal "modes":
42
43 !!! info "Resource"
44
45 For a great overview on these styles & ANSI sequences in general, see [this gist](https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797) by [Christian Petersen](https://www.cbp.io/), or this [website](https://terminalguide.namepad.de/attr/).
46
47
48 - **bold** (mode `1`):
49
50 Traditionally (and on some older emulators) makes text brighter, nowadays it increases the font's weight.
51
52 - **dim** (mode `2`):
53
54 The opposite of the traditional function behind bold, makes text a bit darker.
55
56 - **italic** (mode `3`):
57
58 Uses the italicized font variant, which tilts each character.
59
60 - **underline** (mode `4`):
61
62 Draws a thin line under each character.
63
64 - **blink & blink2** (mode `5` & `6`):
65
66 Repeatedly shows and hides the characters at an internal frequency. Like HTML's `<blink>`, it seems to have fallen out of favor and is no longer supported on many terminals. Some terminal's support one, but not the other.
67
68 - **inverse** (mode `7`):
69
70 Inverts the **function** of the foreground and background colors, i.e. the foreground color will now act on the background and vice-versa.
71
72 - **invisible** (mode `8`):
73
74 Hides text. Not widely supported, for some reason.
75
76 - **strikethrough** (mode `9`):
77
78 Draws a line through each character, somewhere near the vertical middle.
79
80 - **overline** (mode `53`):
81
82 Draws a line above each character.
83
84 ```termage-svg include=docs/src/tim/tags_modes.py
85 ```
86
87
88 ??? info "Implementation"
89 Under the hood, these styles are set by printing a special _escape sequence_ to the terminal. These sequences are structured the following way:
90
91 ```
92 \x1b[{mode_id}m
93 ```
94
95 In effect, this means `bold` stands for `\x1b[1m`, and `inverse` for `\x1b[7m`.
96
97
98 ## Colors
99
100 The terminal generally supports 3 color palettes
101
102 - 3 bit, or 16-color
103
104 8 base colors (`red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white` and `black`), and high-brightness version of each, making a total of 16.
105
106 - 8-bit, or 256-color:
107
108 256 colors, each using 8 bits of information. The standard colors make up the first 16 indices.
109
110 - 24-bit, or truecolor
111
112 Something in the ballpark of 16.7 million colors, this set represents all colors definable by 3 8-bit (`0-255`) values, each standing for one of the `RED`, `GREEN` and `BLUE` channels.
113
114 Lucky for you, the reader, TIM exposes convenient syntax for all of these!
115
116
117 - **Indexed colors** (3-bit and 8-bit):
118
119 `[{index}]`, where `index` is a number in the range `0-255`, including both ends.
120
121 - **RGB colors** (24-bit):
122
123 `[{rrr};{ggg};{bbb}]`, where `rrr`, `ggg` and `bbb` each represent a number alike the ones indexed colors use, standing for the red, green and blue channels respectively.
124
125 - **HEX colors** (alternate representation of 24-bit):
126
127 `[#{rr};{gg};{bb}]`, where each value represents a 3-bit number like above, as a hexadecimal value.
128
129 - **Named colors** (3-bit or 24-bit):
130
131 `[{name}]`, where `name` is a CSS color name [known to TIM](https://github.com/bczsalba/pytermgui/blob/22327a3fb2841d9f219cc1f4784fd58029347c5d/pytermgui/color_info.py#L266):
132
133 ```termage-svg title=CSS\ Colors width=60 height=49 include=docs/src/tim/tags_css_colors.py
134 ```
135
136 Colors, by default, will act on the foreground. This can be changed by prefixing the color markup by an at-symbol (`@`), like so:
137
138 ```termage include=docs/src/tim/tags_colors_fore_back.py height=3 chrome=false tabs=TIM,Output
139 ```
140
141
142 ## Clearers
143
144 After you set a style, you might want to stop it affecting text. To do so, you want to use a clearer token.
145
146 Clearer tokens are denoted with the slash (`/`) prefix, and contain the identifier they are targeting.
147
148 For example:
149
150 - To clear all attributes, including modes, colors, macros, use `/` with no identifier.
151 - To clear a simple terminal mode, use `/{mode}`, like `/italic`.
152 - To clear a color, use `/fg` for foreground colors, and `/bg` for background ones.
153 - To clear a [macro](markup_language#macros), use either `/!{name}`, or `/!` to clear all active macros.
154 - To clear a hyperlink, use `/~`.
155
156 !!! warning "Clearing specific links"
157
158 Since the terminal only supports having one active hyperlink at a time, there is no reason to have syntax for specifically clearing a single link.
159
160
161
162 ## Hyperlinks
163
164 Newer terminals allow HTML anchor-inspired hyperlinks that can be clicked and take the user to places.
165
166 !!! info "Resource"
167
168 For information on the implementation of terminal-hyperlinks, check out this [gist](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda).
169
170 In PyTermGUI TIM, we use the syntax:
171
172 ```
173 [~{protocol}://{uri}]
174 ```
175
176 ...where `protocol` is a data-transfer protocol supported by the client (usually `http`, `https` or `file`), and `uri` is a standard URI readable by the specified protocol.
177
178 For example:
179
180 ```
181 This documentation is hosted as a [~https://ptg.bczsalba.com]subdomain[/~] on my [~https://bczsalba.com]website!
182 ```
183
184 ...would create markup equivalent to the HTML:
185
186 ```html
187 This documentation is hosted as a <a href="https://ptg.bczsalba.com">subdomain</a> on my <a href="https://bczsalba.com">website!</a>
188 ```
189
190
191 ## Macros
192
193 !!! info
194
195 This section details the syntax, but none of the semantics and usage tips, For a more in-depth explanation on macros, see the [macro docs](/tim/usage#define).
196
197 Macros use the syntax:
198
199 ```
200 [!{name}({arg1}:{arg2})]
201 ```
202
203 ...or the shorthand
204
205 ```
206 [!{name}]
207 ```
208
209 when there are no arguments passed.
210
211 In both examples, `name` is the name the macro is defined as, and `arg1` and `arg2` are some arguments passed to it. Macro arguments are separated by colons (`:`), in order to clearly visually differentiate them from Python function calls.
212
213 ## Positioners
214
215 You can position text in the terminal using the syntax:
216
217 ```
218 [({x};{y})]
219 ```
220
221 Where `x` and `y` are terminal coordinates, starting from [terminal.origin](/reference/pytermgui/terminal#pytermgui.terminal.origin).
222
223 ```termage include=docs/src/tim/tags_position.py chrome=false height=5 tabs=TIM,Output
224 ```
225
226 ## Pseudo-s
227
228 All tags above have behaviour that is "pre-defined"; their meaning will be the same in all uses, regardless of context. Pseudo tags are a sort of context-aware group, where they have don't have a consistent meaning: They modify the _existing_ meaning based on the context around them.
229
230 ### Auto
231
232 It's easier to explain by-example, so here is our first pseudo tag, `#auto`:
233
234 ```termage height=5 include=docs/src/tim/tags_pseudo1.py
235 ```
236
237 What this tag does is prompt the engine to look around in the current tag group, and inspect the colors set. From then it does the following (simplified):
238
239 ```python
240 foreground = ...
241 background = ...
242
243 if foreground is None and background is not None:
244 foreground = get_contrasting_color(background)
245 ```
246
247 In essence, it looks for any foreground color. If there is None, but there _is_ a background color, it finds a color that contrasts the background (either an off-white or off-black) and applies that to the foreground. You get color-contrast matches that follow the [W3C guidelines](https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum) with just one tag!