Files
donlucasandClaude Opus 5 c79efd1fa5 Mirror the composition, and make it survive real terminals
The companion moves to the right edge, the litter grows leftward, the sand is
written from the left margin, and the moon crosses to 0.28 so the two things a
glance goes looking for are not stacked in one column.

The cat sprite is not symmetric -- the body sits in the left nine of its twelve
columns and the right three are clearance for a tail that sweeps up from the
right hip -- so it is flipped rather than moved. Bitmap.Mirrored already
existed; the eyes are plotted as characters on top and had to be mirrored by
hand.

Measured, the mirror is also strictly MORE robust when the pane is narrow,
which was the worry that prompted checking. The cat used to be pinned at x=5
with everything crammed to its right; it now slides with the edge, so it stays
whole down to 14 columns instead of clipping at 16, and the tail is what
clips first because the flip put it on the inside. The sand gets the whole left
side instead of starting at column 19, so it survives to 30 columns rather
than 34.

Two composition changes came out of rendering every terminal shape a person
actually has:

  - The waterline was a fixed eight tenths of the height, which is right in a
    tall window and collapses the beach in a short one: at 14 rows it left ONE
    row of sand, so three of the four lines of writing had nowhere to go. It
    now keeps at least five rows of beach. At 24 rows and above this computes
    exactly the number it always did.

  - A line of sand that did not fit was chopped where it ran out --
    `edit internal/auth/ha`, which names no file. It now drops whole pieces in
    order: the result column, then the directories, so a narrow pane still
    reads `edit handler.go`.

Also fixes what the review found in the render loop: the loop never resized
(termSize was read once, Canvas.Resize had no callers, SIGWINCH was unhandled),
the sand was placed from canvas height alone and painted through the companion
and the sea, text was painted one cell per rune so a CJK path shifted every row
after it, and -fps was unvalidated.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AU2ieNpECMF7S4go2CwcDX
2026-08-30 20:30:37 -07:00

97 lines
3.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package companion
// Terminals render CJK and many symbol characters two cells wide, which shears
// a monospace grid apart. Rather than carry a Unicode East Asian Width table,
// sprites are restricted to an ALLOW-LIST of runes verified narrow.
//
// The fail direction is the point: an unrecognised rune is rejected, not
// admitted. A deny-list would silently pass every character nobody thought to
// ban, which is exactly how kaomoji art gets into a codebase and breaks it.
//
// Verified against unicodedata.east_asian_width: every rune below is N or Na.
// Notably NOT here, despite being tempting: · ° ≈ • ▒ ▓ █ ─ ⌒ ● ∧ ☆ ω (all
// Ambiguous — one cell in a Western terminal, two in a CJK locale) and 人 つ
// ・ ミ ノ (Wide — two cells everywhere).
// NarrowOnly makes a string safe to paint one rune per cell.
//
// Everything the scene writes is derived from something the agent touched -- a
// path, an error, an assistant message -- and any of those can carry a rune
// that occupies two terminal cells. Painted one-per-cell it pushes the rest of
// the row right, and every row after it in the frame is then misaligned. There
// is no width table in this program and adding one is not worth it, so fail
// closed: anything not known to be one cell wide becomes a placeholder.
func NarrowOnly(s string) string {
safe := true
for _, r := range s {
if !isNarrow(r) {
safe = false
break
}
}
if safe {
return s
}
out := make([]rune, 0, len(s))
for _, r := range s {
if isNarrow(r) {
out = append(out, r)
continue
}
out = append(out, '?')
}
return string(out)
}
func isNarrow(r rune) bool {
switch {
case r >= 0x20 && r <= 0x7E: // printable ASCII
return true
case r == 0x203F: // ‿ UNDERTIE
return true
case r == 0x2218: // ∘ RING OPERATOR
return true
case r == 0x2591: // ░ LIGHT SHADE (medium/dark/full are Ambiguous)
return true
case r == 0x224B: // triple tilde, the whitecap
return true
case r >= 0x2800 && r <= 0x28FF: // braille
return true
case r == 0x2590: // RIGHT HALF BLOCK -- Narrow, unlike the other three halves
return true
case r >= 0x2596 && r <= 0x259F: // quadrants -- all Narrow, unlike the halves
return true
}
return false
}
// isBlockSafe covers the four block characters quadrant rendering cannot do
// without: top half, bottom half, left half and full.
//
// These are AMBIGUOUS width, not Narrow: one cell in a Western terminal, two
// where the terminal is configured for East Asian ambiguous-width. That is a
// deliberate, documented exception rather than an oversight -- there is no
// narrow substitute for a full block. Any sprite relying on these is reported
// by TestReportsAmbiguousReliance so the exposure stays visible.
func isBlockSafe(r rune) bool {
switch r {
case '\u2580', '\u2584', '\u258C', '\u2588': // top, bottom, left, full
return true
}
return false
}
// UnsafeRunes reports every rune in the sprite that is not verified narrow.
func (s *Sprite) UnsafeRunes() []rune {
var bad []rune
seen := map[rune]bool{}
for _, row := range s.Rows {
for _, r := range row {
if !isNarrow(r) && !seen[r] {
seen[r] = true
bad = append(bad, r)
}
}
}
return bad
}