BaseEffect
Module: terminaltexteffects.engine.base_effect
Base classes for all effects.
Base classes from which all effects should inherit. These classes define the basic structure for an effect and establish the effect iterator interface as well as the effect configuration and terminal configuration.
Classes:
| Name | Description |
|---|---|
BaseEffectIterator |
An abstract base class that defines the basic structure for an iterator
that applies a certain effect to the input data. Provides initialization for the effect configuration and
terminal as well as the |
BaseEffect |
An abstract base class that defines the basic structure for an effect. Provides
the |
BaseEffect
Bases: ABC, Generic[T]
Base iterable class for all effects.
Base class for all effects. Provides the __iter__ method and a context manager for terminal output.
Attributes:
| Name | Type | Description |
|---|---|---|
input_data |
str
|
Text to which the effect will be applied. |
effect_config |
T
|
Configuration for the effect. |
terminal_config |
TerminalConfig
|
Configuration for the terminal. |
Source code in terminaltexteffects/engine/base_effect.py
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 | |
__init__(input_data, effect_config=None, terminal_config=None)
Initialize the effect with the input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_data
|
str
|
Text to which the effect will be applied. |
required |
effect_config
|
BaseConfig | None
|
Effect configuration. If not provided, a new configuration will be built with default values. Defaults to None. |
None
|
terminal_config
|
TerminalConfig | None
|
Terminal configuration. If not provided, a new configuration will be built with default values. Defaults to None. |
None
|
Source code in terminaltexteffects/engine/base_effect.py
__iter__()
Create and return a new iterator for the effect.
Returns:
| Name | Type | Description |
|---|---|---|
BaseEffectIterator |
BaseEffectIterator
|
A new iterator instance for this effect. |
terminal_output(end_symbol='\n')
Prepare a shared iterator terminal for output and restore it afterward.
When called before iteration, the context stages its terminal for the first
iterator created inside it. When an iterator was created immediately before
the context, that iterator's terminal is reused instead. Every call to
iter(effect) still creates a fresh iterator, and only one iterator consumes
each context-staged terminal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
end_symbol
|
str
|
Symbol to print after the effect has completed. Defaults to newline. |
'\n'
|
Yields:
| Name | Type | Description |
|---|---|---|
Terminal |
Terminal
|
Terminal object for handling output. |
Raises:
| Type | Description |
|---|---|
Exception
|
Any exception that occurs within the context manager is re-raised after the terminal state is restored. |
Source code in terminaltexteffects/engine/base_effect.py
BaseEffectIterator
Bases: ABC, Generic[T]
Base iterator class for all effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
effect
|
BaseEffect
|
Effect to apply to the input data. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
config |
T
|
Configuration for the effect. |
terminal |
Terminal
|
Terminal to use for output. |
active_characters |
set[EffectCharacter]
|
Set of active characters in the effect. |
preexisting_colors_present |
bool
|
Whether any terminal input characters were initialized with parsed foreground or background input colors. |
Properties: frame (str): Current frame of the effect.
Methods:
| Name | Description |
|---|---|
update |
Run the tick method for all active characters and remove inactive characters from the active set. |
__iter__ |
Return the iterator object. |
__next__ |
Return the next frame of the effect. |
Raises:
| Type | Description |
|---|---|
EmptyInputError
|
The configured canvas contains no visible input characters. |
Source code in terminaltexteffects/engine/base_effect.py
frame
property
Return the current formatted frame from the terminal.
If the configured terminal frame rate is greater than 0, enforce the frame rate
before reading the formatted output string. This property does not advance effect
state on its own.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Current frame of the effect. |
__init__(effect)
Initialize the iterator with the Effect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
effect
|
BaseEffect
|
Effect to apply to the input data. |
required |
Source code in terminaltexteffects/engine/base_effect.py
__iter__()
Return this iterator instance.
Returns:
| Name | Type | Description |
|---|---|---|
BaseEffectIterator |
BaseEffectIterator
|
This iterator. |
__next__()
abstractmethod
Return the next frame of the effect.
Perform any necessary updates to the effect to progress the effect logic and return the next frame.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Next frame of the effect. |
Source code in terminaltexteffects/engine/base_effect.py
update()
Run one tick for each active character and prune inactive characters.
Each character in active_characters is ticked once. After all ticks complete,
characters whose is_active flag is false are removed from the set.