Skip to content

Show diagrams from a terminal with draw() - #85

Merged
Autoplectic merged 1 commit into
mainfrom
terminal-draw
Nov 29, 2026
Merged

Autoplectic merged 1 commit into
mainfrom
terminal-draw

Conversation

@Autoplectic

Copy link
Copy Markdown
Member

Summary

  • model.draw() with no filename has raised TypeError since the first commit: it passed format to graphviz.Digraph.view(), which takes none. It now shows the diagram, which also works from a terminal (IPython or the plain REPL):
    • inline in terminals that support the kitty graphics protocol (kitty, Ghostty), detected from TERM, TERM_PROGRAM and KITTY_WINDOW_ID, and not inside tmux or screen, which drop the escape sequences unless configured to pass them through. The PNG's dpi comes from the terminal's cell height (TIOCGWINSZ), so that the 11pt node labels come out about as tall as the terminal's text, and a Graphviz size fits the diagram to the window, less two rows for the prompt;
    • otherwise as a temporary PDF in the system viewer (Preview on macOS). PDF rather than the SVG default, because the default handler for .svg can be any app (LaTeXiT on one Mac).
  • draw(filename, view=True) rendered a second time through view(), which overwrote the file it had just written with the DOT source and opened <filename>.svg.pdf. It now opens the file it wrote.
  • The terminal pieces (detection, sizing, chunked escape sequences) are in a new private module, sofic/viz/_terminal.py. docs/viz.rst and the CHANGELOG describe the behavior.

Tests

  • New tests in tests/test_viz.py: the viewer path (a real PDF, nothing on stdout), the inline path (escape sequences that decode to the PNG, terminal attributes applied), view=True (opens the SVG it wrote, no stray files), detection for kitty, Ghostty, Terminal.app, tmux, screen and a non-terminal stream, chunking (4096-character chunks, keys only on the first), and reading the window size through a real pty. On main the three draw tests fail for the reasons above.
  • End to end: a child process with a pty as stdout (TERM=xterm-ghostty, 120 × 40 cells of 14 × 34 pixels) draws the mixed-state presentation of the butterfly process (7 states, long belief labels) as 65 well-formed chunks. The PNG is 1623 × 1004 pixels, inside the 1680 × 1292 left for it.
  • By hand: in Terminal.app and Wave the diagram opens in Preview. Nobody has looked at the inline display in kitty or Ghostty yet.
  • Ruff, ty check (exit 0) and sphinx-build -W pass, as do the test files that touch visualization (320 passed, 2 skipped).

draw() with no filename raised TypeError: it passed format to
graphviz.Digraph.view, which takes none. It now shows the diagram inline
in terminals that support the kitty graphics protocol (kitty, Ghostty;
not inside tmux or screen), at a resolution that matches the terminal's
text and fits the window, and otherwise opens a temporary PDF in the
system viewer.

draw(filename, view=True) rendered a second time through view(), which
overwrote the file it had just written with the DOT source and opened
<filename>.svg.pdf. It now opens the file it wrote.
@Autoplectic
Autoplectic marked this pull request as ready for review November 29, 2026 02:24
@Autoplectic
Autoplectic merged commit df9fcb6 into main Nov 29, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant