styles
Conveniences for styling widgets
All styles have a depth
and item
argument. depth
is an int
that represents that "deep" the Widget is within the hierarchy, and
item
is the string that the style is applied to.
HighlighterStyle
dataclass
A style that highlights the items given to it.
See pytermgui.highlighters
for more information.
Source code in pytermgui/widgets/styles.py
121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
__call__(_, item)
Highlights the given string.
Source code in pytermgui/widgets/styles.py
130 131 132 133 |
|
MarkupFormatter
dataclass
A style that formats depth & item into the given markup on call.
Useful in Widget styles, such as:
import pytermgui as ptg
root = ptg.Container()
# Set border style to be reactive to the widget's depth
root.set_style("border", ptg.MarkupFactory("[35 @{depth}]{item}]")
Source code in pytermgui/widgets/styles.py
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 117 118 |
|
__call__(depth, item)
StyleType: Format depth & item into given markup template
Source code in pytermgui/widgets/styles.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
__str__()
Returns repr, but with markup escaped.
Source code in pytermgui/widgets/styles.py
115 116 117 118 |
|
StyleCall
dataclass
A callable object that simplifies calling style methods.
Instances of this class are created within the Widget._get_style
method, and this class should not be used outside of that context.
Source code in pytermgui/widgets/styles.py
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 |
|
__call__(item)
DepthlessStyleType: Apply style method to item, using depth
Source code in pytermgui/widgets/styles.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
StyleManager
Bases: UserDict
An fancy dictionary to manage a Widget's styles.
Individual styles can be accessed two ways:
manager.styles.style_name == manager._get_style("style_name")
Same with setting:
widget.styles.style_name = ...
widget.set_style("style_name", ...)
The set
and get
methods remain for backwards compatibility reasons, but all
newly written code should use the dot syntax.
It is also possible to set styles as markup shorthands. For example:
widget.styles.border = "60 bold"
...is equivalent to:
widget.styles.border = "[60 bold]{item}"
Source code in pytermgui/widgets/styles.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
__call__(**styles)
Allows calling the manager and setting its styles.
For example:
>>> Button("Hello").styles(label="@60")
Source code in pytermgui/widgets/styles.py
326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
__getattr__(key)
Allows styles.dot_syntax.
Source code in pytermgui/widgets/styles.py
315 316 317 318 319 320 321 322 323 324 |
|
__init__(parent=None, **base)
Initializes a StyleManager
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parent |
Widget | Type[Widget] | None
|
The parent of this instance. It will be assigned in all
|
None
|
Source code in pytermgui/widgets/styles.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
__setattr__(key, value)
Sets an attribute.
It first looks if it can set inside self.data, and defaults back to self.dict.
Raises:
Type | Description |
---|---|
KeyError
|
The given key is not a defined attribute, and is not part of this object's style set. |
Source code in pytermgui/widgets/styles.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
__setitem__(key, value)
Sets an item in self.data
.
If the item is a string, it will be expanded into a MarkupFormatter
before
being converted into the StyleCall
, using expand_shorthand
.
Source code in pytermgui/widgets/styles.py
280 281 282 283 284 285 286 287 |
|
branch(parent)
Branch off from the base
style dictionary.
This method should be called during widget construction. It creates a new
StyleManager
based on self, but with its data detached from the original.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parent |
Widget | Type[Widget]
|
The parent of the new instance. |
required |
Returns:
Type | Description |
---|---|
StyleManager
|
A new |
StyleManager
|
modified without touching the original instance. |
Source code in pytermgui/widgets/styles.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
expand_shorthand(shorthand)
staticmethod
Expands a shorthand string into a MarkupFormatter
instance.
For example, all of these will expand into MarkupFormatter([60]{item}')
:
- '60'
- '[60]'
- '[60]{item}'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shorthand |
str
|
The short version of markup to expand. |
required |
Returns:
Type | Description |
---|---|
MarkupFormatter
|
A |
Source code in pytermgui/widgets/styles.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
merge(other, **styles)
classmethod
Creates a new manager that merges other
with the passed in styles.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
StyleManager
|
The style manager to base the new one from. |
required |
**styles |
str
|
The additional styles the new instance should have. |
{}
|
Returns:
Type | Description |
---|---|
StyleManager
|
A new |
StyleManager
|
|
StyleManager
|
data between the |
StyleManager
|
reflected. |
Source code in pytermgui/widgets/styles.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|