Skip to content

Commit

Permalink
Add support for blink and blink2 to SVG export, use Fira Code webfont…
Browse files Browse the repository at this point in the history
… fallback
  • Loading branch information
darrenburns committed Mar 28, 2022
1 parent bd55ee3 commit a7acc30
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 3 additions & 1 deletion rich/__main__.py
Expand Up @@ -226,7 +226,9 @@ def iter_last(values: Iterable[T]) -> Iterable[Tuple[bool, T]]:
console.print(test_card)
taken = round((process_time() - start) * 1000.0, 1)

Console().print(test_card)
c = Console(record=True)
c.print(test_card)
print(c.export_svg("Rich can export to SVG!"))

print(f"rendered in {pre_cache_taken}ms (cold cache)")
print(f"rendered in {taken}ms (warm cache)")
Expand Down
29 changes: 28 additions & 1 deletion rich/console.py
Expand Up @@ -119,6 +119,22 @@ class NoChange:
<svg width="{total_width}" height="{total_height}" viewBox="0 0 {total_width} {total_height}"
xmlns="http://www.w3.org/2000/svg">
<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;
}}
span {{
display: inline-block;
white-space: pre;
Expand All @@ -130,6 +146,14 @@ class NoChange:
text-decoration: none;
color: inherit;
}}
.blink {{
animation: blinker 1s infinite;
}}
@keyframes blinker {{
from {{ opacity: 1.0; }}
50% {{ opacity: 0.3; }}
to {{ opacity: 1.0; }}
}}
#wrapper {{
padding: {margin}px;
}}
Expand Down Expand Up @@ -2334,6 +2358,9 @@ def export_svg(
if style.link:
text = f'<a href="{style.link}">{text}</a>'

if style.blink or style.blink2:
text = f'<span class="blink">{text}</span>'

# If the style doesn't contain a color, we still
# need to make sure we output the default foreground color
# from the TerminalTheme.
Expand Down Expand Up @@ -2364,7 +2391,7 @@ def export_svg(

# Monospace fonts are generally around 0.5-0.55 width/height ratio, but I've
# added extra width to ensure that the output SVG is big enough.
monospace_font_width_scale = 0.6
monospace_font_width_scale = 0.57

# This works out as a good heuristic for the final size of the drawn terminal.
terminal_height = required_code_height + code_start_y
Expand Down

0 comments on commit a7acc30

Please sign in to comment.