mirror of
https://github.com/donlucasx/xscapes.git
synced 2026-10-20 08:43:32 +02:00
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
159 lines
4.6 KiB
Go
159 lines
4.6 KiB
Go
package event
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// Home is where everything lives. The brief locks this to
|
|
// ~/.config/asciiscapes/, and the real argument for honouring it exactly is
|
|
// not convention: the emitter and the engine are separate processes that
|
|
// share no context but the environment, so the path has to be derivable from
|
|
// $HOME alone by both. Two derivation rules that can disagree is a silent,
|
|
// undebuggable failure -- the hook writes somewhere the scape is not reading
|
|
// and the scene simply never moves.
|
|
func Home() (string, error) {
|
|
if v := os.Getenv("ASCIISCAPES_HOME"); v != "" {
|
|
return v, nil
|
|
}
|
|
if v := os.Getenv("XDG_CONFIG_HOME"); v != "" {
|
|
return filepath.Join(v, "asciiscapes"), nil
|
|
}
|
|
h, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(h, ".config", "asciiscapes"), nil
|
|
}
|
|
|
|
// RunDir holds the per-session sockets and spool files.
|
|
func RunDir() (string, error) {
|
|
h, err := Home()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(h, "run"), nil
|
|
}
|
|
|
|
// EnsureRunDir creates it 0700. The directory is the access control: a unix
|
|
// socket created by bind() takes its mode from the umask (0755 under the
|
|
// common umask 022), and there is an unavoidable window between bind and
|
|
// chmod, so the socket's own mode is a second line and not the first.
|
|
func EnsureRunDir() (string, error) {
|
|
d, err := RunDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return d, os.MkdirAll(d, 0o700)
|
|
}
|
|
|
|
// maxSunPath is the usable sun_path on darwin. The struct field is 104 bytes,
|
|
// one of which is the terminator; linux allows 108. Using the smaller keeps a
|
|
// path that works on the dev machine working everywhere.
|
|
const maxSunPath = 103
|
|
|
|
// ErrPathTooLong means the socket path will not fit in sockaddr_un.
|
|
var ErrPathTooLong = errors.New("socket path too long for sockaddr_un; set ASCIISCAPES_HOME to something shorter")
|
|
|
|
// Tag reduces a session id to the short, filesystem-safe token that names its
|
|
// socket and spool.
|
|
//
|
|
// A HASH of the whole id, not a prefix of it. A prefix is only unique if the
|
|
// ids are, and nothing in the protocol promises that: two adapters numbering
|
|
// their sessions "session-1" and "session-2" would have collided on the same
|
|
// eight characters, silently sharing one socket, and each scape would have
|
|
// shown the other's work. Hashing also makes the sanitising free -- a session
|
|
// of "../../.ssh/id_rsa" cannot become a path when the output is hex.
|
|
func Tag(session string) string {
|
|
if session == "" {
|
|
return "anon"
|
|
}
|
|
sum := sha256.Sum256([]byte(session))
|
|
return hex.EncodeToString(sum[:6])
|
|
}
|
|
|
|
// Short is a human-facing abbreviation of a session id, for messages. It is
|
|
// never a path: use Tag for that.
|
|
func Short(session string) string {
|
|
if session == "" {
|
|
return "(none)"
|
|
}
|
|
if len(session) > 8 {
|
|
return session[:8]
|
|
}
|
|
return session
|
|
}
|
|
|
|
// SockPath is where the engine for this session listens.
|
|
func SockPath(session string) (string, error) {
|
|
d, err := RunDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
p := filepath.Join(d, Tag(session)+".sock")
|
|
if len(p) > maxSunPath {
|
|
return p, ErrPathTooLong
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
// SpoolPath is the JSON-lines fallback for this session, used whenever the
|
|
// socket is not there. It doubles as the replay log.
|
|
func SpoolPath(session string) (string, error) {
|
|
d, err := RunDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(d, Tag(session)+".jsonl"), nil
|
|
}
|
|
|
|
// currentPath names the file holding the most recently started session.
|
|
func currentPath() (string, error) {
|
|
d, err := RunDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(d, "current"), nil
|
|
}
|
|
|
|
// SetCurrent records a session as the newest one. A scape pane started by
|
|
// hand, in a terminal that never saw the agent's environment, has no other way
|
|
// to find out which session to show.
|
|
func SetCurrent(session string) error {
|
|
p, err := currentPath()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(p, []byte(session), 0o600)
|
|
}
|
|
|
|
// Current reads it back. Empty is a normal answer, not an error.
|
|
func Current() string {
|
|
p, err := currentPath()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
b, err := os.ReadFile(p)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(string(b))
|
|
}
|
|
|
|
// SessionFromEnv is how a scape launched inside the agent's own environment
|
|
// binds to the right session with no configuration at all. Verified on
|
|
// 2026-08-30: Claude Code exports CLAUDE_CODE_SESSION_ID to every child, and
|
|
// its value equals the transcript's basename.
|
|
func SessionFromEnv() string {
|
|
for _, k := range []string{"ASCIISCAPES_SESSION", "CLAUDE_CODE_SESSION_ID"} {
|
|
if v := strings.TrimSpace(os.Getenv(k)); v != "" {
|
|
return v
|
|
}
|
|
}
|
|
return ""
|
|
}
|