fix background

This commit is contained in:
Will McGugan 2022-05-02 20:18:28 +01:00
parent b290fdcc55
commit 95049662d6
2 changed files with 6 additions and 4 deletions

View File

@ -2302,7 +2302,7 @@ class Console:
def escape_text(text: str) -> str:
"""HTML escape text and replace spaces with nbsp."""
return escape(text).replace(" ", "\u00A0")
return escape(text).replace(" ", " ")
def make_tag(
name: str, content: Optional[str] = None, **attribs: object
@ -2346,12 +2346,14 @@ class Console:
class_name = f"r{classes[rules]}"
if style.reverse:
has_background = True
background = (
_theme.foreground_color.hex
if style.color is None
else style.color.get_truecolor(_theme).hex
)
else:
has_background = style.bgcolor is not None
background = (
_theme.background_color.hex
if style.bgcolor is None
@ -2359,7 +2361,7 @@ class Console:
)
text_length = cell_len(text)
if background is not None:
if has_background:
text_backgrounds.append(
make_tag(
"rect",

View File

@ -493,7 +493,7 @@ def test_export_html_inline():
assert html == expected
EXPECTED_SVG = '<svg class="rich-terminal" viewBox="0 0 1296 123.0" xmlns="http://www.w3.org/2000/svg">\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 font-variant: east-asian-width-values;\n line-height: 31.0px;\n }\n\n .terminal-614794459-title {\n font-size: 18px;\n opacity: 0.8;\n font-weight: bold;\n font-family: arial;\n }\n\n .terminal-614794459-r1 { fill: #4f76a1;font-weight: bold }\n.terminal-614794459-r2 { fill: #b9bcba }\n </style>\n <rect fill="#191919" x="16" y="20" width="1264" height="83.0" rx="12"/><text class="terminal-614794459-title" fill="#b9bcba" text-anchor="middle" x="632" y="46">Rich</text>\n <circle cx="40" cy="40" r="7" fill="#ff5f57"/>\n <circle cx="62" cy="40" r="7" fill="#febc2e"/>\n <circle cx="84" cy="40" r="7" fill="#28c840"/>\n \n <g transform="translate(28, 60)">\n <rect fill="#be3f48" x="0.0" y="0.0" width="38.2" height="32.0"/><rect fill="#191919" x="37.2" y="0.0" width="13.4" height="32.0"/><rect fill="#191919" x="49.6" y="0.0" width="63.0" height="32.0"/><rect fill="#191919" x="111.60000000000001" y="0.0" width="1129.4" height="32.0"/><rect fill="#191919" x="1240.0" y="0.0" width="1.0" height="32.0"/>\n <text alignment-baseline="baseline" class="terminal-614794459-matrix"><tspan class="terminal-614794459-r1" x="0.0" y="20.0" textLength="37.2">foo</tspan><tspan class="terminal-614794459-r2" x="37.2" y="20.0" textLength="12.4">\xa0</tspan><tspan class="terminal-614794459-r2" x="49.6" y="20.0" textLength="62.0">Click</tspan><tspan class="terminal-614794459-r2" x="111.60000000000001" y="20.0" textLength="1128.4">\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0</tspan><tspan class="terminal-614794459-r2" x="1240.0" y="20.0" textLength="12.4">\n</tspan></text>\n </g>\n</svg>\n'
EXPECTED_SVG = '<svg class="rich-terminal" viewBox="0 0 1296 123.0" xmlns="http://www.w3.org/2000/svg">\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 font-variant: east-asian-width-values;\n line-height: 31.0px;\n }\n\n .terminal-614794459-title {\n font-size: 18px;\n opacity: 0.8;\n font-weight: bold;\n font-family: arial;\n }\n\n .terminal-614794459-r1 { fill: #4f76a1;font-weight: bold }\n.terminal-614794459-r2 { fill: #b9bcba }\n </style>\n <rect fill="#191919" x="16" y="20" width="1264" height="83.0" rx="12"/><text class="terminal-614794459-title" fill="#b9bcba" text-anchor="middle" x="632" y="46">Rich</text>\n <circle cx="40" cy="40" r="7" fill="#ff5f57"/>\n <circle cx="62" cy="40" r="7" fill="#febc2e"/>\n <circle cx="84" cy="40" r="7" fill="#28c840"/>\n \n <g transform="translate(28, 60)">\n <rect fill="#be3f48" x="0.0" y="0.0" width="38.2" height="32.0"/>\n <text alignment-baseline="baseline" class="terminal-614794459-matrix"><tspan class="terminal-614794459-r1" x="0.0" y="20.0" textLength="37.2">foo</tspan><tspan class="terminal-614794459-r2" x="37.2" y="20.0" textLength="12.4">&#160;</tspan><tspan class="terminal-614794459-r2" x="49.6" y="20.0" textLength="62.0">Click</tspan><tspan class="terminal-614794459-r2" x="111.60000000000001" y="20.0" textLength="1128.4">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</tspan><tspan class="terminal-614794459-r2" x="1240.0" y="20.0" textLength="12.4">\n</tspan></text>\n </g>\n</svg>\n'
def test_export_svg():
@ -515,7 +515,7 @@ def test_save_svg():
with tempfile.TemporaryDirectory() as path:
export_path = os.path.join(path, "example.svg")
console.save_svg(export_path)
with open(export_path, "rt") as svg_file:
with open(export_path, "rt", encoding="utf-8") as svg_file:
assert svg_file.read() == EXPECTED_SVG