Created
February 24, 2026 18:13
-
-
Save madrang/d2db0e3002039a61d0d75875f347a5d1 to your computer and use it in GitHub Desktop.
Convert Wezterm terminal capture to SVG.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import argparse | |
| import io | |
| import os | |
| import pathlib | |
| import re | |
| from rich.console import Console | |
| from rich.text import Text | |
| """ Convert Wezterm terminal capture to SVG. """ | |
| parser = argparse.ArgumentParser(description = "Convert terminal captures to svg") | |
| parser.add_argument("filepath", help = "The filename to convert.") | |
| parser.add_argument("-d", "--delete", action = "store_true", help = "Remove the target file after conversion.") | |
| parser.add_argument("-o", "--output", help = "Change the default svg name.") | |
| args = parser.parse_args() | |
| console_args = { | |
| "file": io.StringIO() | |
| , "force_terminal": True | |
| , "record": True | |
| , "height": None | |
| , "width": None | |
| } | |
| svg_args = { | |
| "title": "Example screenshot" | |
| #, theme: | |
| , "font_aspect_ratio": 0.61 | |
| } | |
| content = pathlib.Path(args.filepath).read_text() | |
| def setDimensions(matchobj): | |
| console_args["height"] = int(matchobj.group(1)) | |
| console_args["width"] = int(matchobj.group(2)) | |
| return "" | |
| content = re.sub(r"\x1B\[8;(\w*?);(\w*?)t" | |
| , setDimensions | |
| , content | |
| ) | |
| def setTitle(matchobj): | |
| svg_args["title"] = matchobj.group(1) | |
| return "" | |
| content = re.sub(r"\x1B\]0;(.*?)\x1B\\" | |
| , setTitle | |
| , content | |
| ) | |
| # Create virtual Console | |
| console = Console(**console_args) | |
| # Write content after conversion from Ansi to text. | |
| text = Text.from_ansi(content) | |
| console.print(text, end = "") | |
| svg = console.export_svg(**svg_args) | |
| svg_filepath = args.output if args.output else os.path.dirname(args.filepath) + os.path.sep + "console.svg" | |
| with open(svg_filepath, "w") as file_out: | |
| file_out.write(svg) | |
| print(f"{args.filepath} converted to {svg_filepath}") | |
| if args.delete: | |
| os.remove(args.filepath) | |
| print(f"{args.filepath} removed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment