Skip to content

Commit aa7926c

Browse files
authored
Merge pull request #2290 from Textualize/more-svg-tweaks
More svg tweaks
2 parents 37cde9d + 6b824b6 commit aa7926c

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [12.4.4] - 2022-05-24
9+
10+
### Changed
11+
12+
- Added clipping per line to SVG output to avoid box characters overlapping
13+
- Optimized SVG output
14+
815
## [12.4.3] - 2022-05-23
916

1017
### Changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "rich"
33
homepage = "https://github.com/willmcgugan/rich"
44
documentation = "https://rich.readthedocs.io/en/latest/"
5-
version = "12.4.3"
5+
version = "12.4.4"
66
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
77
authors = ["Will McGugan <[email protected]>"]
88
license = "MIT"

rich/_export_format.py

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<clipPath id="{unique_id}-clip-terminal">
6262
<rect x="0" y="0" width="{terminal_width}" height="{terminal_height}" />
6363
</clipPath>
64+
{lines}
6465
</defs>
6566
6667
{chrome}

rich/console.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -2330,11 +2330,7 @@ def stringify(value: object) -> str:
23302330
)
23312331

23322332
with self._record_buffer_lock:
2333-
segments = list(
2334-
Segment.filter_control(
2335-
Segment.simplify(self._record_buffer),
2336-
)
2337-
)
2333+
segments = list(Segment.filter_control(self._record_buffer))
23382334
if clear:
23392335
self._record_buffer.clear()
23402336

@@ -2383,6 +2379,7 @@ def stringify(value: object) -> str:
23832379
y=y * line_height + 1.5,
23842380
width=char_width * text_length,
23852381
height=line_height + 0.25,
2382+
shape_rendering="crispEdges",
23862383
)
23872384
)
23882385

@@ -2395,10 +2392,19 @@ def stringify(value: object) -> str:
23952392
x=x * char_width,
23962393
y=y * line_height + char_height,
23972394
textLength=char_width * len(text),
2395+
clip_path=f"url(#{unique_id}-line-{y})",
23982396
)
23992397
)
24002398
x += cell_len(text)
24012399

2400+
line_offsets = [line_no * line_height + 1.5 for line_no in range(y)]
2401+
lines = "\n".join(
2402+
f"""<clipPath id="{unique_id}-line-{line_no}">
2403+
{make_tag("rect", x=0, y=offset, width=char_width * width, height=line_height + 0.25)}
2404+
</clipPath>"""
2405+
for line_no, offset in enumerate(line_offsets)
2406+
)
2407+
24022408
styles = "\n".join(
24032409
f".{unique_id}-r{rule_no} {{ {css} }}" for css, rule_no in classes.items()
24042410
)
@@ -2443,8 +2449,8 @@ def stringify(value: object) -> str:
24432449
char_width=char_width,
24442450
char_height=char_height,
24452451
line_height=line_height,
2446-
terminal_width=char_width * width,
2447-
terminal_height=(y + 1) * line_height,
2452+
terminal_width=char_width * width - 1,
2453+
terminal_height=(y + 1) * line_height - 1,
24482454
width=terminal_width + margin_width,
24492455
height=terminal_height + margin_height,
24502456
terminal_x=margin_left + padding_left,
@@ -2453,6 +2459,7 @@ def stringify(value: object) -> str:
24532459
chrome=chrome,
24542460
backgrounds=backgrounds,
24552461
matrix=matrix,
2462+
lines=lines,
24562463
)
24572464
return svg
24582465

tests/test_console.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ def test_export_html_inline():
494494
assert html == expected
495495

496496

497-
EXPECTED_SVG = '<svg class="rich-terminal" viewBox="0 0 1238 74.4" xmlns="http://www.w3.org/2000/svg">\n <!-- Generated with Rich https://www.textualize.io -->\n <style>\n\n @font-face {\n font-family: "Fira Code";\n src: local("FiraCode-Regular"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");\n font-style: normal;\n font-weight: 400;\n }\n @font-face {\n font-family: "Fira Code";\n src: local("FiraCode-Bold"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");\n font-style: bold;\n font-weight: 700;\n }\n\n .terminal-614794459-matrix {\n font-family: Fira Code, monospace;\n font-size: 20px;\n line-height: 24.4px;\n font-variant-east-asian: full-width;\n }\n\n .terminal-614794459-title {\n font-size: 18px;\n font-weight: bold;\n font-family: arial;\n }\n\n .terminal-614794459-r1 { fill: #608ab1;font-weight: bold }\n.terminal-614794459-r2 { fill: #c5c8c6 }\n </style>\n\n <defs>\n <clipPath id="terminal-614794459-clip-terminal">\n <rect x="0" y="0" width="1220.0" height="24.4" />\n </clipPath>\n </defs>\n\n <rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="1236" height="72.4" rx="8"/><text class="terminal-614794459-title" fill="#c5c8c6" text-anchor="middle" x="618" y="27">Rich</text>\n <g transform="translate(26,22)">\n <circle cx="0" cy="0" r="7" fill="#ff5f57"/>\n <circle cx="22" cy="0" r="7" fill="#febc2e"/>\n <circle cx="44" cy="0" r="7" fill="#28c840"/>\n </g>\n \n <g transform="translate(9, 41)" clip-path="url(#terminal-614794459-clip-terminal)">\n <rect fill="#cc555a" x="0" y="1.5" width="36.6" height="24.65"/>\n <g class="terminal-614794459-matrix">\n <text class="terminal-614794459-r1" x="0" y="20" textLength="36.6">foo</text><text class="terminal-614794459-r2" x="48.8" y="20" textLength="61">Click</text><text class="terminal-614794459-r2" x="1220" y="20" textLength="12.2">\n</text>\n </g>\n </g>\n</svg>\n'
497+
EXPECTED_SVG = '<svg class="rich-terminal" viewBox="0 0 1238 74.4" xmlns="http://www.w3.org/2000/svg">\n <!-- Generated with Rich https://www.textualize.io -->\n <style>\n\n @font-face {\n font-family: "Fira Code";\n src: local("FiraCode-Regular"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");\n font-style: normal;\n font-weight: 400;\n }\n @font-face {\n font-family: "Fira Code";\n src: local("FiraCode-Bold"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");\n font-style: bold;\n font-weight: 700;\n }\n\n .terminal-614794459-matrix {\n font-family: Fira Code, monospace;\n font-size: 20px;\n line-height: 24.4px;\n font-variant-east-asian: full-width;\n }\n\n .terminal-614794459-title {\n font-size: 18px;\n font-weight: bold;\n font-family: arial;\n }\n\n .terminal-614794459-r1 { fill: #608ab1;font-weight: bold }\n.terminal-614794459-r2 { fill: #c5c8c6 }\n </style>\n\n <defs>\n <clipPath id="terminal-614794459-clip-terminal">\n <rect x="0" y="0" width="1219.0" height="23.4" />\n </clipPath>\n \n </defs>\n\n <rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="1236" height="72.4" rx="8"/><text class="terminal-614794459-title" fill="#c5c8c6" text-anchor="middle" x="618" y="27">Rich</text>\n <g transform="translate(26,22)">\n <circle cx="0" cy="0" r="7" fill="#ff5f57"/>\n <circle cx="22" cy="0" r="7" fill="#febc2e"/>\n <circle cx="44" cy="0" r="7" fill="#28c840"/>\n </g>\n \n <g transform="translate(9, 41)" clip-path="url(#terminal-614794459-clip-terminal)">\n <rect fill="#cc555a" x="0" y="1.5" width="36.6" height="24.65" shape-rendering="crispEdges"/>\n <g class="terminal-614794459-matrix">\n <text class="terminal-614794459-r1" x="0" y="20" textLength="36.6" clip-path="url(#terminal-614794459-line-0)">foo</text><text class="terminal-614794459-r2" x="48.8" y="20" textLength="61" clip-path="url(#terminal-614794459-line-0)">Click</text><text class="terminal-614794459-r2" x="1220" y="20" textLength="12.2" clip-path="url(#terminal-614794459-line-0)">\n</text>\n </g>\n </g>\n</svg>\n'
498498

499499

500500
def test_export_svg():

0 commit comments

Comments
 (0)