Something went wrong. Try again.
A reference for terminal control sequences whose examples run live in libghostty-vt compiled to WebAssembly.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121# SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us># SPDX-License-Identifier: MIT
"""Run each case's input through pyte and print the screen it leaves.
Driven by emulators/pyte.mjs, which does the judging. Each line of standardinput is one case, {"cols", "rows", "input"} with the input in base64; eachline of standard output is the state pyte leaves after it, in the shapewebsite/assets/vt.js gives libghostty-vt's, so that the site's owncomparison judges both:
{"rows": [[{"text", "style"}]], "cursor": {x, y, visible, pendingWrap}, "title", "reply" (base64), "bells"}
The first line out is {"version", "palette"}: pyte's release, and the 256colors it resolves palette indices to."""
import base64import jsonimport sysfrom importlib.metadata import version
import pytefrom pyte.graphics import FG_BG_256
# pyte names the sixteen basic colors rather than numbering them, "brown"# for yellow, and spells bright magenta's background "bfightmagenta".NAMES = ["black", "red", "green", "brown", "blue", "magenta", "cyan", "white"]INDEX = {name: n for n, name in enumerate(NAMES)}INDEX.update({f"bright{name}": n + 8 for n, name in enumerate(NAMES)})INDEX["bfightmagenta"] = 13PALETTE = [[int(h[i : i + 2], 16) for i in (0, 2, 4)] for h in FG_BG_256]
def color(value): if value == "default": return None if value in INDEX: return PALETTE[INDEX[value]] try: return [int(value[i : i + 2], 16) for i in (0, 2, 4)] except ValueError: return None
class Screen(pyte.Screen): """A screen that keeps what it would have sent back, and its bells."""
def __init__(self, columns, lines): super().__init__(columns, lines) self.sent = bytearray() self.bells = 0
def write_process_input(self, data): self.sent += data.encode("utf-8")
def bell(self, *args): self.bells += 1
def state(screen): rows = [] for y in range(screen.lines): line = screen.buffer[y] cells = [] for x in range(screen.columns): ch = line[x] cells.append( { "text": ch.data, "style": { "fg": color(ch.fg), "bg": color(ch.bg), "bold": ch.bold, "italic": ch.italics, "faint": False, "blink": ch.blink, "inverse": ch.reverse, "invisible": False, "strikethrough": ch.strikethrough, "overline": False, "underline": "SINGLE" if ch.underscore else "NONE", }, } ) rows.append(cells) # After a character in the last column pyte leaves the cursor one past # the edge until the next character wraps; that is a pending wrap. pending = screen.cursor.x >= screen.columns return { "rows": rows, "cursor": { "x": min(screen.cursor.x, screen.columns - 1), "y": screen.cursor.y, "visible": not screen.cursor.hidden, "pendingWrap": pending, }, "title": screen.title, "reply": base64.b64encode(bytes(screen.sent)).decode(), "bells": screen.bells, }
def main(): print(json.dumps({"version": version("pyte"), "palette": PALETTE}), flush=True) for line in sys.stdin: case = json.loads(line) screen = Screen(case["cols"], case["rows"]) stream = pyte.ByteStream(screen) try: stream.feed(base64.b64decode(case["input"])) out = state(screen) except Exception as e: # pyte raising is itself a result out = {"error": f"{type(e).__name__}: {e}"} print(json.dumps(out), flush=True)
if __name__ == "__main__": main()