Extract AnsiArtDocument and friends to files

This fixes the gallery app's --help, because before it was importing the "paint" module, which imported "args", which parsed arguments for the paint app instead of the gallery app.

This is also a refactor I've been meaning to do — since the very beginning, really — and it would've been a lot less trouble if I could've done it from the beginning, but I couldn't get imports to work. Yeah, really. Sounds pretty stupid 'cause it is. Python's module system is terrible.
This commit is contained in:
Isaiah Odhner 2023-09-06 12:58:15 -04:00
parent 9c2b48ec21
commit 5441c0d3b0
5 changed files with 1252 additions and 1228 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,82 @@
"""Templates for exporting to SVG and HTML."""
# This SVG template is based on the template in rich/_export_format.py
# It removes the simulated window frame, and crops the SVG to just the terminal content.
# It also adds a placeholder for ANSI data to be stored in the SVG,
# in order to support opening the file after saving it, in a perfectly lossless way.
# (I have also implemented a more general SVG loading mechanism, but it's now a fallback.)
# It was very nice during development to automate saving a file as SVG:
# textual run --dev "src.textual_paint.paint --restart-on-changes samples/ship.ans" --press ctrl+shift+s,.,s,v,g,enter
# (The Ctrl+Shift+S shortcut doesn't work when actually trying it as a user, but it works to simulate it.)
CUSTOM_CONSOLE_SVG_FORMAT = """\
<svg
class="rich-terminal"
viewBox="0 0 {terminal_width} {terminal_height}"
xmlns="http://www.w3.org/2000/svg"
xmlns:txtpnt="http://github.com/1j01/textual-paint"
>
<!-- Generated with Rich https://www.textualize.io and Textual Paint https://github.com/1j01/textual-paint -->
<style>
@font-face {{
font-family: "Fira Code";
src: local("FiraCode-Regular"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");
font-style: normal;
font-weight: 400;
}}
@font-face {{
font-family: "Fira Code";
src: local("FiraCode-Bold"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");
font-style: bold;
font-weight: 700;
}}
.{unique_id}-matrix {{
font-family: Fira Code, monospace;
font-size: {char_height}px;
line-height: {line_height}px;
font-variant-east-asian: full-width;
}}
{styles}
</style>
<defs>
<clipPath id="{unique_id}-clip-terminal">
<rect x="0" y="0" width="{terminal_width}" height="{terminal_height}" />
</clipPath>
{lines}
<txtpnt:ansi>%ANSI_GOES_HERE%</txtpnt:ansi>
</defs>
<g clip-path="url(#{unique_id}-clip-terminal)">
{backgrounds}
<g class="{unique_id}-matrix">
{matrix}
</g>
</g>
</svg>
"""
CUSTOM_CONSOLE_HTML_FORMAT = """\
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
{stylesheet}
body {{
color: {foreground};
background-color: {background};
}}
</style>
</head>
<body>
<pre style="font-family:monospace;line-height:1"><code>{code}</code></pre>
</body>
</html>
"""

View File

@ -13,7 +13,7 @@ from textual.widgets import Footer, Header, Static
from .__init__ import __version__
from .auto_restart import restart_on_changes, restart_program
from .paint import AnsiArtDocument
from .ansi_art_document import AnsiArtDocument
parser = argparse.ArgumentParser(description='ANSI art gallery', usage='%(prog)s [folder]', prog="python -m src.textual_paint.gallery")
parser.add_argument('folder', nargs='?', default=None, help='Path to a folder containing ANSI art.')

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,51 @@
DEFAULT_PALETTE = [
"rgb(0,0,0)", # Black
"rgb(128,128,128)", # Dark Gray
"rgb(128,0,0)", # Dark Red
"rgb(128,128,0)", # Pea Green
"rgb(0,128,0)", # Dark Green
"rgb(0,128,128)", # Slate
"rgb(0,0,128)", # Dark Blue
"rgb(128,0,128)", # Lavender
"rgb(128,128,64)",
"rgb(0,64,64)",
"rgb(0,128,255)",
"rgb(0,64,128)",
"rgb(64,0,255)",
"rgb(128,64,0)",
"rgb(255,255,255)", # White
"rgb(192,192,192)", # Light Gray
"rgb(255,0,0)", # Bright Red
"rgb(255,255,0)", # Yellow
"rgb(0,255,0)", # Bright Green
"rgb(0,255,255)", # Cyan
"rgb(0,0,255)", # Bright Blue
"rgb(255,0,255)", # Magenta
"rgb(255,255,128)",
"rgb(0,255,128)",
"rgb(128,255,255)",
"rgb(128,128,255)",
"rgb(255,0,128)",
"rgb(255,128,64)",
]
IRC_PALETTE = [
"rgb(255,255,255)", # 0 White
"rgb(0,0,0)", # 1 Black
"rgb(0,0,127)", # 2 Navy
"rgb(0,147,0)", # 3 Green
"rgb(255,0,0)", # 4 Red
"rgb(127,0,0)", # 5 Maroon
"rgb(156,0,156)", # 6 Purple
"rgb(252,127,0)", # 7 Orange
"rgb(255,255,0)", # 8 Yellow
"rgb(0,252,0)", # 9 Light Green
"rgb(0,147,147)", # 10 Teal
"rgb(0,255,255)", # 11 Cyan
"rgb(0,0,252)", # 12 Royal blue
"rgb(255,0,255)", # 13 Magenta
"rgb(127,127,127)", # 14 Gray
"rgb(210,210,210)", # 15 Light Gray
]