asciilibx 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- asciilibx-0.1.0/LICENSE +21 -0
- asciilibx-0.1.0/PKG-INFO +236 -0
- asciilibx-0.1.0/README.md +210 -0
- asciilibx-0.1.0/pyproject.toml +65 -0
- asciilibx-0.1.0/setup.cfg +4 -0
- asciilibx-0.1.0/src/asciilibx/__init__.py +29 -0
- asciilibx-0.1.0/src/asciilibx/__main__.py +6 -0
- asciilibx-0.1.0/src/asciilibx/cli.py +125 -0
- asciilibx-0.1.0/src/asciilibx/format.py +452 -0
- asciilibx-0.1.0/src/asciilibx/generator.py +348 -0
- asciilibx-0.1.0/src/asciilibx/player.py +280 -0
- asciilibx-0.1.0/src/asciilibx/py.typed +0 -0
- asciilibx-0.1.0/src/asciilibx/terminal.py +119 -0
- asciilibx-0.1.0/src/asciilibx.egg-info/PKG-INFO +236 -0
- asciilibx-0.1.0/src/asciilibx.egg-info/SOURCES.txt +23 -0
- asciilibx-0.1.0/src/asciilibx.egg-info/dependency_links.txt +1 -0
- asciilibx-0.1.0/src/asciilibx.egg-info/entry_points.txt +2 -0
- asciilibx-0.1.0/src/asciilibx.egg-info/requires.txt +9 -0
- asciilibx-0.1.0/src/asciilibx.egg-info/top_level.txt +1 -0
- asciilibx-0.1.0/tests/test_cli.py +88 -0
- asciilibx-0.1.0/tests/test_format.py +267 -0
- asciilibx-0.1.0/tests/test_generator.py +228 -0
- asciilibx-0.1.0/tests/test_player.py +257 -0
- asciilibx-0.1.0/tests/test_roundtrip.py +37 -0
- asciilibx-0.1.0/tests/test_terminal.py +80 -0
asciilibx-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 asciilibx contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
asciilibx-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: asciilibx
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: ASCII-art loading animations for Python entry points, with a built-in image/GIF to ASCII generator.
|
|
5
|
+
Author: Nikita K
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/goat-py/ASCIILIBX
|
|
8
|
+
Project-URL: Repository, https://github.com/goat-py/ASCIILIBX
|
|
9
|
+
Project-URL: Issues, https://github.com/goat-py/ASCIILIBX/issues
|
|
10
|
+
Keywords: ascii,loader,spinner,terminal,animation,gif
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Typing :: Typed
|
|
15
|
+
Requires-Python: >=3.9
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Provides-Extra: generator
|
|
19
|
+
Requires-Dist: Pillow>=9.1; extra == "generator"
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
22
|
+
Requires-Dist: Pillow>=9.1; extra == "dev"
|
|
23
|
+
Requires-Dist: ruff; extra == "dev"
|
|
24
|
+
Requires-Dist: mypy; extra == "dev"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# asciilibx
|
|
28
|
+
|
|
29
|
+
ASCII-art **loading screens** for Python entry points, with a built-in **generator** that turns
|
|
30
|
+
PNG / JPG / GIF / APNG into a compact, colour (8-bit) ASCII asset, and a tiny **player** that
|
|
31
|
+
plays it in the background while your code works.
|
|
32
|
+
|
|
33
|
+
* Player: pure standard library, no runtime dependencies, no import-time side effects.
|
|
34
|
+
* Generator: needs [Pillow](https://python-pillow.org) (optional extra).
|
|
35
|
+
* Python 3.9+, Windows cmd / Windows Terminal, zsh/bash on Linux and macOS.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install asciilibx # player + CLI (validate / play)
|
|
41
|
+
pip install "asciilibx[generator]" # + Pillow, needed for generating assets
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The import name is `asciilibx`.
|
|
45
|
+
|
|
46
|
+
## Quick start
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from asciilibx import Loader
|
|
50
|
+
|
|
51
|
+
with Loader("spinner.ascii"):
|
|
52
|
+
connect_to_database()
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### As a decorator
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from asciilibx import Loader
|
|
59
|
+
|
|
60
|
+
@Loader("spinner.ascii") # animates until main() returns (or raises)
|
|
61
|
+
def main():
|
|
62
|
+
load_models()
|
|
63
|
+
return "ready"
|
|
64
|
+
|
|
65
|
+
if __name__ == "__main__":
|
|
66
|
+
print(main()) # the function's return value passes through
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`functools.wraps` is applied, async functions are supported, and the terminal is restored
|
|
70
|
+
(cursor visible, colours reset, loader wiped) on normal return, exceptions and Ctrl+C.
|
|
71
|
+
|
|
72
|
+
## Generate an asset from an image or GIF
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
python -m asciilibx generate input.gif -o spinner.ascii --width 60 --fps 12
|
|
76
|
+
# spinner.ascii: 16 frame(s), 60x30 chars, 54,229 bytes
|
|
77
|
+
|
|
78
|
+
python -m asciilibx validate spinner.ascii # exit code 0 = valid, 1 = invalid
|
|
79
|
+
python -m asciilibx play spinner.ascii # quick look while developing
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
or from Python:
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from asciilibx import generate, save_asset
|
|
86
|
+
|
|
87
|
+
asset = generate("input.gif", width=60, fps=12, dither=True)
|
|
88
|
+
save_asset(asset, "spinner.ascii.gz") # a .gz suffix gzips the file
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
`generate` accepts a path, an open binary file or a `PIL.Image`. A progress bar is drawn on
|
|
92
|
+
**stderr** (never stdout) for multi-frame input when stderr is a terminal.
|
|
93
|
+
|
|
94
|
+
How the conversion works, per pixel:
|
|
95
|
+
|
|
96
|
+
1. RGBA is read; `alpha < 128` is background (a space, no colour).
|
|
97
|
+
2. Luminance `L = 0.2126 R + 0.7152 G + 0.0722 B`, applied directly to the sRGB values (no gamma
|
|
98
|
+
linearisation, which is not noticeable at character resolution).
|
|
99
|
+
3. Character = `charset[int(L / 255 * (len(charset) - 1))]`, dark to light. Default charset
|
|
100
|
+
`" .:-=+*#%@"`; any length works.
|
|
101
|
+
4. Colour = nearest xterm-256 entry (6x6x6 cube + 24 greys, the 16 system colours are never
|
|
102
|
+
used because users can redefine them), emitted as `ESC[38;5;Nm`.
|
|
103
|
+
5. Whitespace is never wrapped in colour codes, and a colour code is only emitted when the colour
|
|
104
|
+
changes along a line. In `8bit` mode each line ends with `ESC[0m`.
|
|
105
|
+
6. Height is `round(src_h * width / src_w * cell_aspect)` with `cell_aspect=0.5`, compensating for
|
|
106
|
+
character cells being about twice as tall as wide.
|
|
107
|
+
|
|
108
|
+
GIF/APNG: frame disposal and per-frame transparency are applied (via Pillow's compositing), so
|
|
109
|
+
frames don't leave ghosts behind. Frame delays come from the file, with anything below 20 ms
|
|
110
|
+
clamped to 20 ms, and are stored as a multiplier of `1/fps`.
|
|
111
|
+
|
|
112
|
+
Optional Floyd-Steinberg dithering (`--dither` / `dither=True`) spreads luminance error across
|
|
113
|
+
neighbouring cells, which helps gradients with short charsets.
|
|
114
|
+
|
|
115
|
+
## Parameters
|
|
116
|
+
|
|
117
|
+
### `Loader(asset, *, loop=True, fps_override=None, color_mode=None, stream=None, static_fallback=False)`
|
|
118
|
+
|
|
119
|
+
| Parameter | Default | Meaning |
|
|
120
|
+
|---|---|---|
|
|
121
|
+
| `asset` | required | Path to a `.ascii` file (plain or gzip) or an `Asset` object. |
|
|
122
|
+
| `loop` | `True` | Repeat while the wrapped code runs. `False` plays once and holds the last frame. |
|
|
123
|
+
| `fps_override` | `None` | Play at this frame rate instead of the asset's `fps`. |
|
|
124
|
+
| `color_mode` | `None` | `"8bit"` / `"none"`; `None` follows the asset. Colour is also dropped when the stream is not a TTY or `NO_COLOR` is set. |
|
|
125
|
+
| `stream` | `sys.stderr` | Where to draw. Use `sys.stdout` if you prefer. |
|
|
126
|
+
| `static_fallback` | `False` | When not animating (non-TTY), print the first frame once instead of nothing. |
|
|
127
|
+
|
|
128
|
+
### `generate(src, *, width=80, fps=12, charset=" .:-=+*#%@", color_mode="8bit", dither=False, cell_aspect=0.5, progress=False, max_frames=2000)`
|
|
129
|
+
|
|
130
|
+
| Parameter | Meaning |
|
|
131
|
+
|---|---|
|
|
132
|
+
| `width` | Output width in characters (1-500). |
|
|
133
|
+
| `fps` | Base frame rate stored in the asset. |
|
|
134
|
+
| `charset` | Characters from dark to light; each must be printable and one column wide. |
|
|
135
|
+
| `color_mode` | `"8bit"` or `"none"`. |
|
|
136
|
+
| `dither` | Floyd-Steinberg dithering of luminance. |
|
|
137
|
+
| `cell_aspect` | Cell width / height, used for the aspect compensation. |
|
|
138
|
+
| `progress` | `True` draws a bar on stderr; a callable receives `(done, total)`. |
|
|
139
|
+
|
|
140
|
+
### CLI
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
python -m asciilibx generate INPUT -o OUT [--width N] [--fps N] [--charset S]
|
|
144
|
+
[--color {8bit,none}] [--dither] [--gzip]
|
|
145
|
+
[--cell-aspect X] [--no-progress]
|
|
146
|
+
python -m asciilibx validate FILE
|
|
147
|
+
python -m asciilibx play FILE [--seconds N] [--once] [--fps N] [--color {8bit,none}]
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Asset format (`.ascii`, version 1)
|
|
151
|
+
|
|
152
|
+
```json
|
|
153
|
+
{
|
|
154
|
+
"version": 1,
|
|
155
|
+
"fps": 12,
|
|
156
|
+
"width": 80,
|
|
157
|
+
"color_mode": "8bit",
|
|
158
|
+
"frames": [
|
|
159
|
+
{"delay": 1, "lines": ["\u001b[38;5;196m@@\u001b[0m ", "..."]}
|
|
160
|
+
]
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
* `delay` is optional (default 1): a multiplier on `1/fps`; integers or floats, `0 < delay <= 100`.
|
|
165
|
+
* Every frame has the same number of lines, and every line has exactly `width` visible columns.
|
|
166
|
+
* `color_mode: "none"` assets must not contain any colour codes.
|
|
167
|
+
|
|
168
|
+
Other tools' frames can be loaded too. Besides this JSON, the loader accepts gzip-compressed
|
|
169
|
+
files (detected by magic bytes, whatever the file name), UTF-8 with BOM, a bare JSON list of
|
|
170
|
+
frames (strings with `\n`, or lists of lines), and plain ANSI text with frames separated by form
|
|
171
|
+
feeds. Literal `\033[` / `\x1b[` / `\e[` text is converted to real colour sequences. Imported
|
|
172
|
+
frames are padded to equal width and go through the **same** validator as native assets.
|
|
173
|
+
Pass `lenient=False` to `load_asset` to accept only the native format.
|
|
174
|
+
|
|
175
|
+
## Asset security
|
|
176
|
+
|
|
177
|
+
An `.ascii` file is printed to your terminal, and terminals are programmable: escape sequences can
|
|
178
|
+
move the cursor, clear the screen, change the window title, and on some terminals do far worse. An
|
|
179
|
+
asset downloaded from somewhere should therefore be treated as untrusted input. The validator
|
|
180
|
+
(run on every load, and by `validate`) enforces:
|
|
181
|
+
|
|
182
|
+
* **Only SGR colour sequences** (`ESC[` digits and `;` `m`, parameters 0-255) are allowed.
|
|
183
|
+
Cursor movement, screen clear, OSC (titles, hyperlinks, clipboard), DCS, bare `ESC`, C1 control
|
|
184
|
+
characters (e.g. `0x9B`), `\r`, `\n`, `\b`, BEL, NUL and every other control character are
|
|
185
|
+
rejected.
|
|
186
|
+
* Invisible/format Unicode characters (bidi overrides such as U+202E, zero-width and line
|
|
187
|
+
separators) are rejected.
|
|
188
|
+
* Size caps against resource abuse: 20 MB file (also applied to the *decompressed* size of gzip
|
|
189
|
+
files, so a gzip bomb is rejected), 2000 frames, 500 columns, 200 lines. Override with
|
|
190
|
+
`asciilibx.format.Limits`.
|
|
191
|
+
* Errors say where: `frame 3, line 7: disallowed escape sequence ...`.
|
|
192
|
+
|
|
193
|
+
The player only ever receives validated data and emits its own control sequences (hide/show
|
|
194
|
+
cursor, cursor up). What this does *not* protect against: the content of the art itself (it can
|
|
195
|
+
still be offensive or misleading), and terminal bugs in how SGR sequences themselves are handled.
|
|
196
|
+
|
|
197
|
+
## Terminal behaviour and limitations
|
|
198
|
+
|
|
199
|
+
* **No TTY** (pipes, CI, `> file`): nothing is animated and nothing is written (or one plain frame
|
|
200
|
+
with `static_fallback=True`), so logs stay clean. Because the default stream is **stderr**,
|
|
201
|
+
`python app.py > out.txt` still shows the loader on your terminal.
|
|
202
|
+
* **`NO_COLOR`** set (non-empty), `TERM=dumb`, or `color_mode="none"`: colour is stripped;
|
|
203
|
+
`TERM=dumb` additionally disables animation.
|
|
204
|
+
* **Things your code prints while the loader runs share the terminal** and will interleave with
|
|
205
|
+
the animation. Print after the loader stops, or log to a file.
|
|
206
|
+
* **Narrow / short terminals:** frames are cropped (never wrapped) to fit, keeping a spare column
|
|
207
|
+
and row, and re-fitted if the terminal is resized.
|
|
208
|
+
* **zsh / bash on Linux & macOS:** any modern terminal emulator works (xterm, GNOME Terminal,
|
|
209
|
+
iTerm2, kitty, ...), including inside tmux/screen.
|
|
210
|
+
* **Windows:** VT processing is switched on once at start (`os.system("")`) and needs
|
|
211
|
+
Windows 10 or later. **Windows Terminal** is recommended. The classic **cmd.exe** console works,
|
|
212
|
+
but use a TrueType font such as Consolas or Cascadia Mono so the glyphs line up, and expect
|
|
213
|
+
slightly slower redraws on big frames. Wide (CJK) glyphs count as two columns. This library's
|
|
214
|
+
Windows paths were reviewed but not run on a real Windows console by the author of this
|
|
215
|
+
initial version, so please report problems.
|
|
216
|
+
* **Ctrl+C / exceptions:** the loader's `finally` stops the thread (`Event.set()` + `join()`),
|
|
217
|
+
wipes the animation, resets colours and shows the cursor; an `atexit` hook covers abnormal
|
|
218
|
+
exits while a loader is running.
|
|
219
|
+
|
|
220
|
+
## Roadmap
|
|
221
|
+
|
|
222
|
+
* A switch for light terminal backgrounds (invert luminance mapping / reversed charset).
|
|
223
|
+
* Frame de-duplication and delta encoding for smaller files.
|
|
224
|
+
* Background-colour (half-block) rendering for double vertical resolution.
|
|
225
|
+
|
|
226
|
+
## Development
|
|
227
|
+
|
|
228
|
+
```bash
|
|
229
|
+
pip install -e ".[dev]"
|
|
230
|
+
pytest
|
|
231
|
+
ruff check . && mypy
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
## License
|
|
235
|
+
|
|
236
|
+
MIT, see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# asciilibx
|
|
2
|
+
|
|
3
|
+
ASCII-art **loading screens** for Python entry points, with a built-in **generator** that turns
|
|
4
|
+
PNG / JPG / GIF / APNG into a compact, colour (8-bit) ASCII asset, and a tiny **player** that
|
|
5
|
+
plays it in the background while your code works.
|
|
6
|
+
|
|
7
|
+
* Player: pure standard library, no runtime dependencies, no import-time side effects.
|
|
8
|
+
* Generator: needs [Pillow](https://python-pillow.org) (optional extra).
|
|
9
|
+
* Python 3.9+, Windows cmd / Windows Terminal, zsh/bash on Linux and macOS.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install asciilibx # player + CLI (validate / play)
|
|
15
|
+
pip install "asciilibx[generator]" # + Pillow, needed for generating assets
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
The import name is `asciilibx`.
|
|
19
|
+
|
|
20
|
+
## Quick start
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from asciilibx import Loader
|
|
24
|
+
|
|
25
|
+
with Loader("spinner.ascii"):
|
|
26
|
+
connect_to_database()
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### As a decorator
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from asciilibx import Loader
|
|
33
|
+
|
|
34
|
+
@Loader("spinner.ascii") # animates until main() returns (or raises)
|
|
35
|
+
def main():
|
|
36
|
+
load_models()
|
|
37
|
+
return "ready"
|
|
38
|
+
|
|
39
|
+
if __name__ == "__main__":
|
|
40
|
+
print(main()) # the function's return value passes through
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`functools.wraps` is applied, async functions are supported, and the terminal is restored
|
|
44
|
+
(cursor visible, colours reset, loader wiped) on normal return, exceptions and Ctrl+C.
|
|
45
|
+
|
|
46
|
+
## Generate an asset from an image or GIF
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
python -m asciilibx generate input.gif -o spinner.ascii --width 60 --fps 12
|
|
50
|
+
# spinner.ascii: 16 frame(s), 60x30 chars, 54,229 bytes
|
|
51
|
+
|
|
52
|
+
python -m asciilibx validate spinner.ascii # exit code 0 = valid, 1 = invalid
|
|
53
|
+
python -m asciilibx play spinner.ascii # quick look while developing
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
or from Python:
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from asciilibx import generate, save_asset
|
|
60
|
+
|
|
61
|
+
asset = generate("input.gif", width=60, fps=12, dither=True)
|
|
62
|
+
save_asset(asset, "spinner.ascii.gz") # a .gz suffix gzips the file
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
`generate` accepts a path, an open binary file or a `PIL.Image`. A progress bar is drawn on
|
|
66
|
+
**stderr** (never stdout) for multi-frame input when stderr is a terminal.
|
|
67
|
+
|
|
68
|
+
How the conversion works, per pixel:
|
|
69
|
+
|
|
70
|
+
1. RGBA is read; `alpha < 128` is background (a space, no colour).
|
|
71
|
+
2. Luminance `L = 0.2126 R + 0.7152 G + 0.0722 B`, applied directly to the sRGB values (no gamma
|
|
72
|
+
linearisation, which is not noticeable at character resolution).
|
|
73
|
+
3. Character = `charset[int(L / 255 * (len(charset) - 1))]`, dark to light. Default charset
|
|
74
|
+
`" .:-=+*#%@"`; any length works.
|
|
75
|
+
4. Colour = nearest xterm-256 entry (6x6x6 cube + 24 greys, the 16 system colours are never
|
|
76
|
+
used because users can redefine them), emitted as `ESC[38;5;Nm`.
|
|
77
|
+
5. Whitespace is never wrapped in colour codes, and a colour code is only emitted when the colour
|
|
78
|
+
changes along a line. In `8bit` mode each line ends with `ESC[0m`.
|
|
79
|
+
6. Height is `round(src_h * width / src_w * cell_aspect)` with `cell_aspect=0.5`, compensating for
|
|
80
|
+
character cells being about twice as tall as wide.
|
|
81
|
+
|
|
82
|
+
GIF/APNG: frame disposal and per-frame transparency are applied (via Pillow's compositing), so
|
|
83
|
+
frames don't leave ghosts behind. Frame delays come from the file, with anything below 20 ms
|
|
84
|
+
clamped to 20 ms, and are stored as a multiplier of `1/fps`.
|
|
85
|
+
|
|
86
|
+
Optional Floyd-Steinberg dithering (`--dither` / `dither=True`) spreads luminance error across
|
|
87
|
+
neighbouring cells, which helps gradients with short charsets.
|
|
88
|
+
|
|
89
|
+
## Parameters
|
|
90
|
+
|
|
91
|
+
### `Loader(asset, *, loop=True, fps_override=None, color_mode=None, stream=None, static_fallback=False)`
|
|
92
|
+
|
|
93
|
+
| Parameter | Default | Meaning |
|
|
94
|
+
|---|---|---|
|
|
95
|
+
| `asset` | required | Path to a `.ascii` file (plain or gzip) or an `Asset` object. |
|
|
96
|
+
| `loop` | `True` | Repeat while the wrapped code runs. `False` plays once and holds the last frame. |
|
|
97
|
+
| `fps_override` | `None` | Play at this frame rate instead of the asset's `fps`. |
|
|
98
|
+
| `color_mode` | `None` | `"8bit"` / `"none"`; `None` follows the asset. Colour is also dropped when the stream is not a TTY or `NO_COLOR` is set. |
|
|
99
|
+
| `stream` | `sys.stderr` | Where to draw. Use `sys.stdout` if you prefer. |
|
|
100
|
+
| `static_fallback` | `False` | When not animating (non-TTY), print the first frame once instead of nothing. |
|
|
101
|
+
|
|
102
|
+
### `generate(src, *, width=80, fps=12, charset=" .:-=+*#%@", color_mode="8bit", dither=False, cell_aspect=0.5, progress=False, max_frames=2000)`
|
|
103
|
+
|
|
104
|
+
| Parameter | Meaning |
|
|
105
|
+
|---|---|
|
|
106
|
+
| `width` | Output width in characters (1-500). |
|
|
107
|
+
| `fps` | Base frame rate stored in the asset. |
|
|
108
|
+
| `charset` | Characters from dark to light; each must be printable and one column wide. |
|
|
109
|
+
| `color_mode` | `"8bit"` or `"none"`. |
|
|
110
|
+
| `dither` | Floyd-Steinberg dithering of luminance. |
|
|
111
|
+
| `cell_aspect` | Cell width / height, used for the aspect compensation. |
|
|
112
|
+
| `progress` | `True` draws a bar on stderr; a callable receives `(done, total)`. |
|
|
113
|
+
|
|
114
|
+
### CLI
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
python -m asciilibx generate INPUT -o OUT [--width N] [--fps N] [--charset S]
|
|
118
|
+
[--color {8bit,none}] [--dither] [--gzip]
|
|
119
|
+
[--cell-aspect X] [--no-progress]
|
|
120
|
+
python -m asciilibx validate FILE
|
|
121
|
+
python -m asciilibx play FILE [--seconds N] [--once] [--fps N] [--color {8bit,none}]
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Asset format (`.ascii`, version 1)
|
|
125
|
+
|
|
126
|
+
```json
|
|
127
|
+
{
|
|
128
|
+
"version": 1,
|
|
129
|
+
"fps": 12,
|
|
130
|
+
"width": 80,
|
|
131
|
+
"color_mode": "8bit",
|
|
132
|
+
"frames": [
|
|
133
|
+
{"delay": 1, "lines": ["\u001b[38;5;196m@@\u001b[0m ", "..."]}
|
|
134
|
+
]
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
* `delay` is optional (default 1): a multiplier on `1/fps`; integers or floats, `0 < delay <= 100`.
|
|
139
|
+
* Every frame has the same number of lines, and every line has exactly `width` visible columns.
|
|
140
|
+
* `color_mode: "none"` assets must not contain any colour codes.
|
|
141
|
+
|
|
142
|
+
Other tools' frames can be loaded too. Besides this JSON, the loader accepts gzip-compressed
|
|
143
|
+
files (detected by magic bytes, whatever the file name), UTF-8 with BOM, a bare JSON list of
|
|
144
|
+
frames (strings with `\n`, or lists of lines), and plain ANSI text with frames separated by form
|
|
145
|
+
feeds. Literal `\033[` / `\x1b[` / `\e[` text is converted to real colour sequences. Imported
|
|
146
|
+
frames are padded to equal width and go through the **same** validator as native assets.
|
|
147
|
+
Pass `lenient=False` to `load_asset` to accept only the native format.
|
|
148
|
+
|
|
149
|
+
## Asset security
|
|
150
|
+
|
|
151
|
+
An `.ascii` file is printed to your terminal, and terminals are programmable: escape sequences can
|
|
152
|
+
move the cursor, clear the screen, change the window title, and on some terminals do far worse. An
|
|
153
|
+
asset downloaded from somewhere should therefore be treated as untrusted input. The validator
|
|
154
|
+
(run on every load, and by `validate`) enforces:
|
|
155
|
+
|
|
156
|
+
* **Only SGR colour sequences** (`ESC[` digits and `;` `m`, parameters 0-255) are allowed.
|
|
157
|
+
Cursor movement, screen clear, OSC (titles, hyperlinks, clipboard), DCS, bare `ESC`, C1 control
|
|
158
|
+
characters (e.g. `0x9B`), `\r`, `\n`, `\b`, BEL, NUL and every other control character are
|
|
159
|
+
rejected.
|
|
160
|
+
* Invisible/format Unicode characters (bidi overrides such as U+202E, zero-width and line
|
|
161
|
+
separators) are rejected.
|
|
162
|
+
* Size caps against resource abuse: 20 MB file (also applied to the *decompressed* size of gzip
|
|
163
|
+
files, so a gzip bomb is rejected), 2000 frames, 500 columns, 200 lines. Override with
|
|
164
|
+
`asciilibx.format.Limits`.
|
|
165
|
+
* Errors say where: `frame 3, line 7: disallowed escape sequence ...`.
|
|
166
|
+
|
|
167
|
+
The player only ever receives validated data and emits its own control sequences (hide/show
|
|
168
|
+
cursor, cursor up). What this does *not* protect against: the content of the art itself (it can
|
|
169
|
+
still be offensive or misleading), and terminal bugs in how SGR sequences themselves are handled.
|
|
170
|
+
|
|
171
|
+
## Terminal behaviour and limitations
|
|
172
|
+
|
|
173
|
+
* **No TTY** (pipes, CI, `> file`): nothing is animated and nothing is written (or one plain frame
|
|
174
|
+
with `static_fallback=True`), so logs stay clean. Because the default stream is **stderr**,
|
|
175
|
+
`python app.py > out.txt` still shows the loader on your terminal.
|
|
176
|
+
* **`NO_COLOR`** set (non-empty), `TERM=dumb`, or `color_mode="none"`: colour is stripped;
|
|
177
|
+
`TERM=dumb` additionally disables animation.
|
|
178
|
+
* **Things your code prints while the loader runs share the terminal** and will interleave with
|
|
179
|
+
the animation. Print after the loader stops, or log to a file.
|
|
180
|
+
* **Narrow / short terminals:** frames are cropped (never wrapped) to fit, keeping a spare column
|
|
181
|
+
and row, and re-fitted if the terminal is resized.
|
|
182
|
+
* **zsh / bash on Linux & macOS:** any modern terminal emulator works (xterm, GNOME Terminal,
|
|
183
|
+
iTerm2, kitty, ...), including inside tmux/screen.
|
|
184
|
+
* **Windows:** VT processing is switched on once at start (`os.system("")`) and needs
|
|
185
|
+
Windows 10 or later. **Windows Terminal** is recommended. The classic **cmd.exe** console works,
|
|
186
|
+
but use a TrueType font such as Consolas or Cascadia Mono so the glyphs line up, and expect
|
|
187
|
+
slightly slower redraws on big frames. Wide (CJK) glyphs count as two columns. This library's
|
|
188
|
+
Windows paths were reviewed but not run on a real Windows console by the author of this
|
|
189
|
+
initial version, so please report problems.
|
|
190
|
+
* **Ctrl+C / exceptions:** the loader's `finally` stops the thread (`Event.set()` + `join()`),
|
|
191
|
+
wipes the animation, resets colours and shows the cursor; an `atexit` hook covers abnormal
|
|
192
|
+
exits while a loader is running.
|
|
193
|
+
|
|
194
|
+
## Roadmap
|
|
195
|
+
|
|
196
|
+
* A switch for light terminal backgrounds (invert luminance mapping / reversed charset).
|
|
197
|
+
* Frame de-duplication and delta encoding for smaller files.
|
|
198
|
+
* Background-colour (half-block) rendering for double vertical resolution.
|
|
199
|
+
|
|
200
|
+
## Development
|
|
201
|
+
|
|
202
|
+
```bash
|
|
203
|
+
pip install -e ".[dev]"
|
|
204
|
+
pytest
|
|
205
|
+
ruff check . && mypy
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## License
|
|
209
|
+
|
|
210
|
+
MIT, see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "asciilibx"
|
|
7
|
+
authors = [{name = "Nikita K"}]
|
|
8
|
+
version = "0.1.0"
|
|
9
|
+
description = "ASCII-art loading animations for Python entry points, with a built-in image/GIF to ASCII generator."
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
license = "MIT"
|
|
13
|
+
license-files = ["LICENSE"]
|
|
14
|
+
keywords = ["ascii", "loader", "spinner", "terminal", "animation", "gif"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
"Environment :: Console",
|
|
19
|
+
"Typing :: Typed",
|
|
20
|
+
]
|
|
21
|
+
# The player has no runtime dependencies. Pillow is only needed by the generator.
|
|
22
|
+
dependencies = []
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://github.com/goat-py/ASCIILIBX"
|
|
26
|
+
Repository = "https://github.com/goat-py/ASCIILIBX"
|
|
27
|
+
Issues = "https://github.com/goat-py/ASCIILIBX/issues"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
generator = ["Pillow>=9.1"]
|
|
32
|
+
dev = ["pytest>=7", "Pillow>=9.1", "ruff", "mypy"]
|
|
33
|
+
|
|
34
|
+
[project.scripts]
|
|
35
|
+
asciilibx = "asciilibx.cli:main"
|
|
36
|
+
|
|
37
|
+
[tool.setuptools.packages.find]
|
|
38
|
+
where = ["src"]
|
|
39
|
+
|
|
40
|
+
[tool.setuptools.package-data]
|
|
41
|
+
asciilibx = ["py.typed"]
|
|
42
|
+
|
|
43
|
+
[tool.pytest.ini_options]
|
|
44
|
+
testpaths = ["tests"]
|
|
45
|
+
pythonpath = ["src"]
|
|
46
|
+
|
|
47
|
+
[tool.ruff]
|
|
48
|
+
line-length = 100
|
|
49
|
+
target-version = "py39"
|
|
50
|
+
src = ["src", "tests"]
|
|
51
|
+
|
|
52
|
+
[tool.ruff.lint]
|
|
53
|
+
select = ["E", "F", "I", "UP", "B", "W"]
|
|
54
|
+
ignore = ["UP007", "UP006", "UP035", "UP045"]
|
|
55
|
+
|
|
56
|
+
[tool.mypy]
|
|
57
|
+
# mypy >= 2 cannot target 3.9; the code itself avoids 3.10+ syntax (see CI note in README).
|
|
58
|
+
python_version = "3.10"
|
|
59
|
+
strict = true
|
|
60
|
+
files = ["src"]
|
|
61
|
+
|
|
62
|
+
[[tool.mypy.overrides]]
|
|
63
|
+
module = ["PIL", "PIL.*", "numpy", "numpy.*"]
|
|
64
|
+
follow_imports = "skip"
|
|
65
|
+
ignore_missing_imports = true
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""asciilibx - ASCII-art loading animations for Python entry points.
|
|
2
|
+
|
|
3
|
+
Public API::
|
|
4
|
+
|
|
5
|
+
from asciilibx import Loader, generate, validate_asset
|
|
6
|
+
|
|
7
|
+
Importing this package has no side effects (no terminal writes, no argv parsing,
|
|
8
|
+
no Pillow import).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from .format import Asset, AssetError, Frame, load_asset, save_asset, validate_asset
|
|
12
|
+
from .generator import generate, generate_to_file, rgb_to_xterm256
|
|
13
|
+
from .player import Loader
|
|
14
|
+
|
|
15
|
+
__version__ = "0.1.0"
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"Loader",
|
|
19
|
+
"generate",
|
|
20
|
+
"generate_to_file",
|
|
21
|
+
"validate_asset",
|
|
22
|
+
"load_asset",
|
|
23
|
+
"save_asset",
|
|
24
|
+
"rgb_to_xterm256",
|
|
25
|
+
"Asset",
|
|
26
|
+
"Frame",
|
|
27
|
+
"AssetError",
|
|
28
|
+
"__version__",
|
|
29
|
+
]
|