Files
donlucasandClaude Fable 5.1 4444efa1b2 The page: fixed for phones, the hero in variants, H1, the owl's states, one more pass
His phone screenshots: the hero cropped with the companion off the stage,
the moon in strips. The crop reproduced in WebKit's iPhone emulation (124
columns at the player's 7px floor is 523px on a 351px stage) and the hero
has a narrow twin now, the same session at 80 columns, played under the
floor. The moon did not reproduce on this Mac's fonts, so it is iOS's
fallback on the half-block glyphs: the page never asks a font for a block
any more. Every half and quarter block is a space on a CSS gradient
(canvas.writeBlockCSS), a full block a space on its ink.

montage.go: the hero's machinery with the scape and the companion
switchable on a session timeline and the hour on a schedule of stops; six
variants on a review page (TestHeroPage), his pick H1: the owl's turn on
the vista, then the crab's on the shore. The companion states section
draws the owl with each state's picked motion. The feature kickers a tad
bigger; the copy synthesised; the Scapes section on his sentence with the
art then the commands; the lead, his option 3; the problem typed in its
own window in the third person; "Built by donlucas"; an ASCII heart with a
count shared through a free public counter; the three cues playable
(notify.Cues); the shore on its own beside the vista; captions at their
window's width.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-17 14:56:34 -07:00

455 lines
16 KiB
Go

package main
import (
"encoding/json"
"fmt"
"math"
"os"
"path/filepath"
"strings"
"time"
"github.com/donlucasx/xscapes/internal/canvas"
"github.com/donlucasx/xscapes/internal/companion"
"github.com/donlucasx/xscapes/internal/reduce"
"github.com/donlucasx/xscapes/internal/scape"
"github.com/donlucasx/xscapes/internal/term"
)
// gifScene is one animated clip of the demo turn: which beat it starts on,
// at what hour, for how long. Frames come out of the same reducer fold the
// site's stills came from, so nothing in them is posed.
type gifScene struct {
name string
at float64 // the beat, seconds from the prompt
tod float64
secs float64
note string
// cols is the frame's width in cells; 0 means the design size, 80.
cols int
// agentRows, when set, puts that many rows of the agent's own output
// above the scape, so the clip is the whole terminal window rather than
// the scape alone. The scape keeps its 24 rows underneath.
agentRows int
// beats, when set, replaces the shared demo turn with a session written
// for this clip and written to end where it began.
beats []loopBeat
// speed is session seconds per second of clip. The waves and the
// companion keep real time either way; only the session is wound on, so
// a whole arc fits in a few seconds without the water looking sped up.
speed float64
// todEnd, when set, runs the time of day from tod to todEnd across the
// clip instead of holding it still.
todEnd float64
// crop, when set, is the cell rectangle of the scape the frame keeps:
// x0, y0, x1, y1. It is how one channel is shown on its own.
crop [4]int
// pal, when set, takes the colours out of every frame's markup and into a
// shared stylesheet. The page embeds its frames as text rather than
// screenshotting them, and inline colours on every run is the difference
// between a page that ships and one that does not.
pal *canvas.HTMLPalette
// prof overrides the render profile. The GIF clips are truecolor -- "the
// scene at its best" -- but an embedded frame pays for its colours in the
// stylesheet, and truecolor gradients give almost every run its own pair:
// measured, 920KB of CSS against 158KB of frames. The cube is both far
// cheaper AND what the product actually shows in a terminal, which is the
// ruling the whole scape is designed around.
// ⚠ A bool, not a term.Profile: Profile256 is the ZERO VALUE of that type,
// so a profile field would have silently taken every existing GIF scene
// off truecolor the moment it was added.
cube bool
// rows overrides the SCAPE's height in cells. His screenshot of 2026-09-14
// is the reason it exists: his real window is 125x62 and the hero was
// rendering at 96x36, so the page was showing a cramped version of a
// product that has room to breathe.
rows int
// animal names the companion to draw, for the one clip pair whose whole
// subject is that you can change it. Empty means the shipped default,
// which is what every other clip must use: the page has to render the same
// on any checkout and show what a fresh install gets.
animal string
// fps overrides GIFFPS. A page playing frames from JS can run slower than
// a GIF without looking like it is stuttering, and every frame dropped is
// markup not shipped.
fps int
}
const gifRows = 24
// rate is this clip's frame rate: its own, or the shared GIF one.
func (sc gifScene) rate() int {
if sc.fps > 0 {
return sc.fps
}
return GIFFPS
}
// cropped reports whether this clip keeps only part of the scape.
func (sc gifScene) cropped() bool { return sc.crop != [4]int{} }
// rowsOf is the frame's height in cells, after any crop.
func (sc gifScene) rowsOf() int {
if sc.cropped() {
return sc.crop[3] - sc.crop[1]
}
return sc.scapeRows() + sc.agentRows
}
// scapeRows is the height of the scape itself, before the agent's pane.
func (sc gifScene) scapeRows() int {
if sc.rows > 0 {
return sc.rows
}
return gifRows
}
// colsOf is the frame's width in cells, after any crop.
func (sc gifScene) colsOf() int {
if sc.cropped() {
return sc.crop[2] - sc.crop[0]
}
if sc.cols > 0 {
return sc.cols
}
return 80
}
// sceneCols is the width the scape is rendered at, before any crop.
func (sc gifScene) sceneCols() int {
if sc.cols > 0 {
return sc.cols
}
return 80
}
// gifPageCap is the tallest page a headless capture will take without
// trouble. A long clip is split across several, each screenshotted and
// sliced on its own; the frames are concatenated in order.
const gifPageCap = 15000
func (sc gifScene) framesPerPage() int {
n := gifPageCap / (sc.rowsOf()*GIFPx + 4)
if n < 1 {
n = 1
}
return n
}
var gifScenes = []gifScene{
{name: "window", tod: 0.30, todEnd: 1.30, secs: 16, cols: 108, agentRows: 16,
speed: loopSecs / 16.0, beats: windowLoop(),
note: "one whole session in one loop, the day turning under it"},
{name: "hero", at: 14, tod: 0.52, secs: 6, note: "the fan-out at noon: busy sea, five crablets, the sand carrying the tool calls"},
{name: "worried", at: 22, tod: 0.62, secs: 4, note: "a command exited 1: the companion carries it"},
{name: "ask", at: 30, tod: 0.80, secs: 4, note: "dusk: the agent needs permission, solid balloon, alert pose"},
{name: "done", at: 44, tod: 0.96, secs: 5, note: "night: done, dotted balloon, the constellation, the moon with its readout"},
{name: "resting", at: 72, tod: 0.27, secs: 8, note: "dawn, half a minute later: flat sea, the writing receding, the litter swims off as its dwell ends"},
}
// allGIFScenes is every clip the site wants: the five of the demo turn plus
// the dissected ones beside the legend.
func allGIFScenes() []gifScene { return append(append([]gifScene{}, gifScenes...), glanceClips()...) }
// GIFFPS is the clip rate. Ten a second is the scape's own cadence rounded
// down; the sea and the companion read at it, and the pages stay under the
// height a headless capture will take.
const GIFFPS = 10
// GIFPx is the cell's font size in the clips. 14px Menlo is 8.4 by 14
// pixels a cell, 672 by 336 for a frame: crisp at the page's width, and a
// clip still under a megabyte.
const GIFPx = 14
// gifPages writes one HTML page per scene into dir, each holding every frame
// of the clip stacked under a magenta separator, plus manifest.json. A
// capture script (site/make-gifs.py) screenshots each page once, slices it
// on the separators and encodes the GIF. Frames are 80 columns by 24 rows
// at GIFPx Menlo, in truecolor.
func gifPages(seed int64, dir string) error {
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
type entry struct {
Name string `json:"name"`
Frames int `json:"frames"`
FPS int `json:"fps"`
Note string `json:"note"`
Secs float64 `json:"secs"`
Px int `json:"px"`
Cols int `json:"cols"`
Rows int `json:"rows"`
Pages int `json:"pages"`
}
var manifest []entry
for _, sc := range allGIFScenes() {
frames, err := gifFrames(seed, sc)
if err != nil {
return err
}
per := sc.framesPerPage()
pages := 0
for from := 0; from < len(frames); from += per {
to := min(from+per, len(frames))
var b strings.Builder
fmt.Fprintf(&b, `<!doctype html><html><head><meta charset="utf-8"><style>`+
`body{margin:0;background:#000}`+
`.f{display:inline-block;width:max-content;height:%dpx;overflow:hidden;vertical-align:top;border-right:3px solid #ff00ff;font-family:Menlo,"SF Mono","DejaVu Sans Mono",monospace;font-size:%dpx;line-height:1}`+
`.f pre{margin:0;font-family:inherit;font-size:%dpx;line-height:1;letter-spacing:0;display:block}`+
`.sep{width:100%%;height:4px;background:#ff00ff}`+
`</style></head><body>`, sc.rowsOf()*GIFPx, GIFPx, GIFPx)
for _, f := range frames[from:to] {
b.WriteString(`<div class="sep"></div><div><div class="f">`)
b.WriteString(f)
b.WriteString(`</div></div>`)
}
b.WriteString(`<div class="sep"></div></body></html>`)
name := fmt.Sprintf("%s.%d.html", sc.name, pages)
if err := os.WriteFile(filepath.Join(dir, name), []byte(b.String()), 0o644); err != nil {
return err
}
pages++
}
manifest = append(manifest, entry{sc.name, len(frames), sc.rate(), sc.note, sc.secs, GIFPx, sc.colsOf(), sc.rowsOf(), pages})
}
js, _ := json.MarshalIndent(manifest, "", " ")
return os.WriteFile(filepath.Join(dir, "manifest.json"), js, 0o644)
}
// sessionRun folds a clip's session -- the shared demo turn from one of its
// beats, or beats of its own -- and reads the reduced state at any frame.
// gifFrames and portraitFrames share it, so the legend's companion-only
// clips are the same moments of the same session as the scenes they replaced,
// and the transcript the hero shows above its scape comes from the same fold.
type sessionRun struct {
sc gifScene
base time.Time
red *reduce.Reducer
beats []loopBeat // nil means the shared demo turn
demo []turnBeat
applied int
speed float64
// lines is what the agent has printed so far: the prompt that opened the
// turn, then whatever the beats print.
lines []string
}
func newSessionRun(sc gifScene) *sessionRun {
r := &sessionRun{sc: sc, base: time.Now(), red: reduce.New("site"), speed: sc.speed,
lines: []string{"> add rate limiting to the auth endpoint"}}
if r.speed <= 0 {
r.speed = 1
}
if sc.beats != nil {
r.beats = sc.beats
} else {
r.demo = demoTurn()
// The pane opens on the prompt that started the turn; beats add to it.
r.apply(sc.at)
}
return r
}
// apply lands every beat up to a session second.
func (r *sessionRun) apply(upto float64) {
if r.beats != nil {
for r.applied < len(r.beats) && r.beats[r.applied].at <= upto {
b := r.beats[r.applied]
now := r.base.Add(time.Duration(b.at * float64(time.Second)))
for _, e := range b.evs {
r.red.Apply(e, now)
}
if b.clear {
r.lines = r.lines[:1]
}
r.lines = append(r.lines, b.print...)
r.applied++
}
return
}
for r.applied < len(r.demo) && r.demo[r.applied].at <= upto {
now := r.base.Add(time.Duration(r.demo[r.applied].at * float64(time.Second)))
for _, e := range r.demo[r.applied].evs {
r.red.Apply(e, now)
}
r.applied++
}
}
// at is the state at frame i of n, with the picture's own clock t and the
// session's wall time. t is real seconds, so the swell and the breathing read
// at their designed rate whatever the session does; only the session is wound
// on, by the clip's speed.
func (r *sessionRun) at(i, n int) (st reduce.State, t float64, now time.Time) {
t = float64(i) / float64(r.sc.rate())
session := t * r.speed
if r.beats == nil {
t = r.sc.at + t
session = t
}
r.apply(session)
now = r.base.Add(time.Duration(session * float64(time.Second)))
st = r.red.State(now)
st.Act.TimeOfDay = r.sc.tod
if r.sc.todEnd != 0 {
tod := r.sc.tod + (r.sc.todEnd-r.sc.tod)*float64(i)/float64(n)
st.Act.TimeOfDay = tod - math.Floor(tod)
}
return st, t, now
}
// gifFrames renders a clip frame by frame. A clip either walks the shared
// demo turn from one of its beats, or runs a session of its own written to
// end where it began; either way the waves and the companion keep real time
// and only the session is wound on, by sc.speed.
func gifFrames(seed int64, sc gifScene) ([]string, error) {
run := newSessionRun(sc)
sh := scape.NewShore(seed, false)
// The SHIPPED default, not this machine's saved preference: the page has to
// render the same on any checkout, and it has to show what a fresh install
// actually gets. companionPref() would make the entry depend on whatever is
// in ~/.config/xscapes/companion, which is the one thing a submission must
// not do.
name := companion.DefaultName
if sc.animal != "" {
name = sc.animal
}
cat := companion.New(name)
cat.FaceLeft(true)
ccw, chh := cat.Size()
c := canvas.New(sc.sceneCols(), sc.scapeRows(), canvas.AlphaFar, canvas.AlphaMid, canvas.AlphaNear)
lay := compose(c.W, ccw, true)
sh.MoonX = lay.MoonX
n := int(sc.secs * float64(sc.rate()))
var out []string
for i := 0; i < n; i++ {
st, t, now := run.at(i, n)
sh.Update(c, t, st.Act)
st.Tail = st.FitTail(now, lay.SandTo-lay.SandFrom)
// THE COMPANION WALKS UP BEFORE IT ASKS, in the clips too.
//
// This line is why the come-closer pose -- the best thing in the
// product -- had never appeared anywhere on the page. Approach had
// exactly one non-test caller, the live TUI (frames.go), so every
// rendered clip drew the animal at rung 0 and the ask read as a
// thumbnail with a balloon over it. The live loop drives the walk off
// its own frame time; so does this one.
cat.Approach(1/float64(sc.rate()), st.Pose == companion.NeedsYou)
drawScene(c, sh, cat, lay, st, t, seed, c.H-2-chh)
// Truecolor, his direction of 2026-09-05: the page shows the scene at
// its best, not as the cube rounds it. The product still runs on the
// cube; the clips are what it is reaching for.
prof := term.ProfileTrueColor
if sc.cube {
prof = term.Profile256
}
var frame string
switch {
case sc.cropped() && sc.pal != nil:
frame = c.HTMLFragmentCropClassed(sc.crop[0], sc.crop[1], sc.crop[2], sc.crop[3], GIFPx, prof, sc.pal)
case sc.cropped():
frame = c.HTMLFragmentCropAs(sc.crop[0], sc.crop[1], sc.crop[2], sc.crop[3], GIFPx, prof)
case sc.pal != nil:
frame = c.HTMLFragmentClassed(GIFPx, prof, sc.pal)
default:
frame = c.HTMLFragment(GIFPx)
}
if sc.agentRows > 0 {
// The agent's own output sits above its scape, in the same
// window, which is the thing the page is actually claiming.
pane := agentPane(sc.sceneCols(), sc.agentRows, run.lines)
if sc.pal != nil {
frame = pane.HTMLFragmentClassed(GIFPx, prof, sc.pal) + frame
} else {
frame = pane.HTMLFragment(GIFPx) + frame
}
}
out = append(out, frame)
}
if len(out) == 0 {
return nil, fmt.Errorf("scene %s rendered no frames", sc.name)
}
return out, nil
}
// agentPane draws the agent's half of the window: what it has printed so far,
// scrolled so the newest is at the bottom, with the cursor waiting under it.
//
// It is a canvas rather than markup so it goes through the same renderer and
// lands on the same grid as the scape below it, cell for cell.
func agentPane(cols, rows int, lines []string) *canvas.Canvas {
c := canvas.New(cols, rows, canvas.AlphaFar, canvas.AlphaMid, canvas.AlphaNear)
c.Clear()
var (
bg = term.RGB{R: 26, G: 26, B: 26}
bright = term.RGB{R: 238, G: 238, B: 238}
tool = term.RGB{R: 135, G: 175, B: 255}
body = term.RGB{R: 200, G: 200, B: 200}
dim = term.RGB{R: 122, G: 122, B: 122}
rule = term.RGB{R: 58, G: 58, B: 58}
)
for y := 0; y < rows; y++ {
for x := 0; x < cols; x++ {
c.SetBG(x, y, bg)
}
}
near := c.Near()
put := func(x, y int, s string, col term.RGB) {
for i, r := range []rune(s) {
if x+i < cols-1 && y >= 0 && y < rows {
near.Plot(x+i, y, r, col, 1)
}
}
}
// The transcript scrolls: the last however many lines fit above the
// input row, oldest first.
room := rows - 4
from := 0
if len(lines) > room {
from = len(lines) - room
}
y := 1
for _, l := range lines[from:] {
switch {
case strings.HasPrefix(l, "> "):
put(2, y, ">", dim)
put(4, y, l[2:], bright)
case strings.HasPrefix(l, "*"):
// A tool call: name, target, and the result against the right
// edge, the way an agent lays them out.
f := strings.SplitN(l[1:], "\t", 3)
put(2, y, "*", dim)
put(4, y, f[0], tool)
if len(f) > 1 {
put(13, y, f[1], body)
}
if len(f) > 2 {
if col := cols - 2 - len([]rune(f[2])); col > 14+len([]rune(f[1])) {
put(col, y, f[2], dim)
}
}
case strings.HasPrefix(l, "$ "):
// A shell line typed at the agent's prompt, the way Claude Code
// shows one: the bang, then the command.
put(2, y, "!", dim)
put(4, y, l[2:], bright)
case strings.HasPrefix(l, "!"):
put(4, y, l[1:], bright)
default:
put(4, y, strings.TrimSpace(l), dim)
}
y++
}
// The input line, with the cursor the mark is made of.
y = rows - 2
for x := 1; x < cols-1; x++ {
near.Plot(x, y-1, '_', rule, 0.5)
}
put(2, y, ">", dim)
c.SetBG(4, y, bright)
return c
}