package tui import ( "encoding/json" "fmt" "os" "regexp" "strconv" "strings" "unicode/utf8" "github.com/bnema/zut/packages/core" "github.com/bnema/zut/packages/provider" "github.com/mattn/go-runewidth" ) // pathFromToolArgs returns the "path" argument from a tool_call's // JSON arguments, or "" if the args aren't a JSON object or don't // include one. Used to pick a syntax language for rendering the // corresponding tool_result. func pathFromToolArgs(raw json.RawMessage) string { if len(raw) == 0 { return "" } var m map[string]any if err := json.Unmarshal(raw, &m); err != nil { return "" } for _, k := range []string{"path", "file_path"} { if s, ok := m[k].(string); ok && s != "" { return s } } return "" } // offsetFromToolArgs returns the read tool's 1-indexed `offset` // arg (the first line of the slice the tool was asked to return), // or 0 when the call didn't specify one. Used by the tui to draw // the line-number gutter aligned to the right starting row, even // though the tool's text content itself no longer carries line // numbers. func offsetFromToolArgs(raw json.RawMessage) int { if len(raw) == 0 { return 0 } var m map[string]any if err := json.Unmarshal(raw, &m); err != nil { return 0 } switch v := m["offset"].(type) { case float64: return int(v) case int: return v } return 0 } // osUserHomeDir is aliased so the test file can swap it. var osUserHomeDir = os.UserHomeDir // View turns a transcript + live state into a slice of styled lines, // already wrapped to width. type View struct { Theme Theme ImageProto ImageProtocol // how to render inline images in this terminal Messages []provider.Message // MessagesRevision is supplied by the owning transcript when available. // Append-only revisions let Build reuse prior message rows without // hashing every existing payload. Zero keeps the standalone hash cache // behaviour for callers that do not have a revision source. MessagesRevision uint64 // RenderCacheRevision changes whenever a visual rendering dependency // changes. Interactive mode includes it in its stable transcript key so // the outer cache cannot bypass View's own theme/layout cache checks. RenderCacheRevision uint64 // toolPaths maps tool_use_id to the "path" argument of the call, if // any, so tool_result rendering can pick the right syntax language. // Rebuilt on each Build(). toolPaths map[string]string // toolStartLines maps tool_use_id to the 1-indexed first line // number of a `read` result, pulled from the call's offset arg. // Used by renderNumberedFile to draw a line-number gutter over // raw (unnumbered) file content the model receives. Rebuilt on // each Build(). toolStartLines map[string]int // toolCallLabels maps tool_use_id to the box label (tool name + // short args) for the call, so a tool_result message can render // the box top edge without having to look back at the assistant // message that originated the call. Rebuilt on each Build(). toolCallLabels map[string]string toolCallNames map[string]string toolCallArgs map[string]json.RawMessage // PlanUpdates maps a plan tool-call id to the checklist snapshot materialized // after that call. The transcript only stores per-call deltas, so the host // replays them at load and fills this map from plan_update events live. It is // deep-copied by CloneForRender so the render goroutine never shares it with // the live view. PlanUpdates map[string]core.PlanUpdate // PlanRevision advances whenever PlanUpdates changes. It is part of // msgVisualKey so a snapshot arriving after a message was first cached // invalidates that cache entry. PlanRevision uint64 Streaming string // current assistant text delta StreamingActive bool ToolCalls []ToolCallView // tool calls in flight or completed // Startup resource fields list host-loaded inputs before the transcript // without creating provider messages or persisted entries. StartupAgentName string StartupContextPaths []string StartupExtensionNames []string StartupSkillNames []string StatusLine string Err string // liveBodyHigh tracks the tallest live-preview body height seen // per tool-call id, so a streaming edit/write/bash box never // shrinks mid-stream. Keyed by ToolCallView.ID. Entries are only // consulted while a call has no Result; interactive mode prunes // the map when a turn ends. liveBodyHigh map[string]int liveBodyExpandAll bool // FlatTools renders tool calls without the bordered panel: a quiet // header line per call plus indented, frameless output. The // truncation/expand behaviour and theme colors are unchanged. // False (the default) keeps the bordered box. FlatTools bool // CompactUser renders sent user messages as a single quiet gutter // line per wrapped row instead of a tinted bubble with a blank // padding row above and below. False (the default) keeps the // padded, background-tinted bubble. CompactUser bool // CompactMode reduces visual chrome in the transcript. Tool calls // render with a flat header and no bordered panel, and sent user // messages render without padded background bubbles. False keeps // the current spacious rendering. CompactMode bool // ExpandAll forces every long tool result to render in full. // Toggled from the tui by ctrl+o. When false, results longer than // ToolCollapseLines collapse to ToolCollapsePreview lines plus a // "... (N more lines, M total, ctrl+o to expand)" footer. ExpandAll bool // renderCache holds the per-message rendered line slices so Build // doesn't re-markdown every message on every frame. Keyed by a // struct of (content hash, width, expandAll) — any of those // changing invalidates the entry. Messages are append-only after // they finalise so keeping the cache across turns is safe. The // revision-indexed cache additionally carries visual configuration // dependencies so resize/theme/compact changes cannot go stale. // // Streaming/in-flight work is cached per tool call. Interactive mode // advances ToolCallView.Revision for each visible update, so unchanged // tool results do not need to hash their payload on every frame. renderCache map[msgCacheKey][]string liveRenderCache map[liveToolCacheKey][]string liveText liveTextCache messageCache []messageRenderCacheEntry messageCacheStart int messageCacheRevision uint64 messageCacheLen int messageCachePrefix uint64 messageCachePrefixOK bool toolPathRevision uint64 // TailLimit caps how many messages from the END of Messages are // rendered. Messages older than the limit emit a zero-row slice // so first paint after a session resume doesn't pay the markdown // / chroma cost for the entire transcript at once. 0 means no // limit (render everything, the historical behaviour). Interactive // raises the cap when the user scrolls past the top of the // already-rendered tail. TailLimit int } // msgVisualKey identifies the visual configuration that affects a cached // message render. Keep this shared by both cache implementations so their // invalidation rules cannot drift apart. type msgVisualKey struct { width int expandAll bool turnOpen bool flatTools bool compactMode bool compactUser bool imageProto ImageProtocol theme uint64 // planRev carries the plan snapshot revision so a new snapshot invalidates // cached message rows that render a plan checklist. planRev uint64 } // msgCacheKey identifies a cached message render. hash is a 64-bit // FNV-1a of the message's content, which is cheap to compute and // unambiguous enough for the cache (collisions produce a stale frame, // not wrong data, and we recompute on invalidation anyway). type msgCacheKey struct { hash uint64 visual msgVisualKey } type messageRenderCacheEntry struct { visual msgVisualKey lines []string valid bool } type liveToolCacheKey struct { callID string revision uint64 content uint64 width int expandAll bool flatTools bool compact bool imageProto ImageProtocol theme uint64 } // InvalidateRenderCache drops all cached message renders. The tui // calls this when the transcript is replaced wholesale (/compact, // /clear, session swap) since messages can be replaced in place and // a content-hash miss alone doesn't reclaim the old entries. func (v *View) InvalidateRenderCache() { v.RenderCacheRevision++ v.renderCache = nil v.liveRenderCache = nil v.liveText = liveTextCache{} v.messageCache = nil v.messageCacheStart = 0 v.messageCacheRevision = 0 v.messageCacheLen = 0 v.messageCachePrefix = 0 v.messageCachePrefixOK = false v.toolPathRevision = 0 } // CloneForRender returns an immutable render snapshot. Its mutable cache // maps are copied so a renderer owner can build outside the interactive // state mutex without racing the next snapshot. func (v *View) CloneForRender() *View { clone := *v // The live-text cache grows in place; a snapshot must not share it. clone.liveText = liveTextCache{} clone.Messages = append([]provider.Message(nil), v.Messages...) clone.ToolCalls = append([]ToolCallView(nil), v.ToolCalls...) clone.StartupContextPaths = append([]string(nil), v.StartupContextPaths...) clone.StartupExtensionNames = append([]string(nil), v.StartupExtensionNames...) clone.StartupSkillNames = append([]string(nil), v.StartupSkillNames...) clone.toolPaths = cloneStringMap(v.toolPaths) clone.toolStartLines = cloneIntMap(v.toolStartLines) clone.toolCallLabels = cloneStringMap(v.toolCallLabels) clone.toolCallNames = cloneStringMap(v.toolCallNames) clone.toolCallArgs = cloneRawMessageMap(v.toolCallArgs) clone.PlanUpdates = clonePlanUpdateMap(v.PlanUpdates) clone.liveBodyHigh = cloneIntMap(v.liveBodyHigh) if v.renderCache != nil { clone.renderCache = make(map[msgCacheKey][]string, len(v.renderCache)) for key, lines := range v.renderCache { clone.renderCache[key] = lines } } if v.liveRenderCache != nil { clone.liveRenderCache = make(map[liveToolCacheKey][]string, len(v.liveRenderCache)) for key, lines := range v.liveRenderCache { clone.liveRenderCache[key] = lines } } clone.messageCache = append([]messageRenderCacheEntry(nil), v.messageCache...) return &clone } func cloneStringMap(src map[string]string) map[string]string { if src == nil { return nil } dst := make(map[string]string, len(src)) for key, value := range src { dst[key] = value } return dst } func cloneRawMessageMap(src map[string]json.RawMessage) map[string]json.RawMessage { if src == nil { return nil } dst := make(map[string]json.RawMessage, len(src)) for key, value := range src { dst[key] = append(json.RawMessage(nil), value...) } return dst } func cloneIntMap(src map[string]int) map[string]int { if src == nil { return nil } dst := make(map[string]int, len(src)) for key, value := range src { dst[key] = value } return dst } // clonePlanUpdateMap deep-copies a plan snapshot map. Each entry's Plan slice is // copied too, so the render clone owns every step it reads on the other // goroutine. func clonePlanUpdateMap(src map[string]core.PlanUpdate) map[string]core.PlanUpdate { if src == nil { return nil } dst := make(map[string]core.PlanUpdate, len(src)) for key, value := range src { if value.Plan != nil { steps := make([]core.PlanStep, len(value.Plan)) copy(steps, value.Plan) value.Plan = steps } dst[key] = value } return dst } // AdoptRenderCacheFrom transfers caches produced from a render snapshot // back to the owning view. Callers must ensure the transcript revision is // still current before adopting them. func (v *View) AdoptRenderCacheFrom(snapshot *View) { v.renderCache = snapshot.renderCache v.liveRenderCache = snapshot.liveRenderCache v.messageCache = snapshot.messageCache v.messageCacheStart = snapshot.messageCacheStart v.messageCacheRevision = snapshot.messageCacheRevision v.messageCacheLen = snapshot.messageCacheLen v.messageCachePrefix = snapshot.messageCachePrefix v.messageCachePrefixOK = snapshot.messageCachePrefixOK v.toolPaths = snapshot.toolPaths v.toolStartLines = snapshot.toolStartLines v.toolCallLabels = snapshot.toolCallLabels v.toolCallNames = snapshot.toolCallNames v.toolCallArgs = snapshot.toolCallArgs v.PlanUpdates = snapshot.PlanUpdates v.PlanRevision = snapshot.PlanRevision v.toolPathRevision = snapshot.toolPathRevision } // ToolCollapsePreview is the number of lines shown before a long tool // result is replaced with a "... ctrl+o to expand" footer. Tool // results shorter than ToolCollapseLines always render in full. const ( ToolCollapsePreview = 10 ToolCollapseLines = 12 ) // ToolCallView is a pending tool invocation plus optional result. type ToolCallView struct { ID string Name string // Revision is advanced by the owning interactive state whenever a // visible field changes. A non-zero revision lets the live render cache // avoid rescanning a large result string on every redraw. Revision uint64 Args string // rendered argument summary Preview string // side-effect-free result shown before confirmation Result string // rendered result preview (truncated) Error bool Done bool // Streaming is true while the model is still typing the tool // call's JSON arguments. The TUI renders a live preview of any // interesting string fields (for `write`, the `content`; for // `bash`, the `command`) so the user can watch the file being // composed. Set to false as soon as EvToolUseEnd arrives. Streaming bool // RawJSONBuf is the accumulator of every EvToolUseArgs delta // the stream has delivered for this tool call. Used by the // partial-JSON extractor to peel off the live string value // of one named field on each render. RawJSONBuf string // LivePath is the `path` arg extracted as soon as it parses // out of RawJSONBuf. Shown next to the tool name in the header // so the user can see which file is being written to. LivePath string } // MessageAnchor records where a rendered message starts in the chat // line slice. Used by /jump so the dialog can scroll the viewport to // the row where a turn's user prompt begins. type MessageAnchor struct { MessageIdx int // index into v.Messages Row int // first row of that message in the Build() output } // Build returns the chat log lines for the given width. func (v *View) Build(width int) []string { lines, _ := v.BuildWithAnchors(width) return lines } // BuildLive renders only in-flight assistant/tool/error state. Main-screen // scrollback renderers can keep these rows outside the immutable transcript // so native scrolling stays stable while a turn streams. func (v *View) BuildLive(width int) []string { v.prepareLiveToolState() if v.liveRenderCache == nil { v.liveRenderCache = make(map[liveToolCacheKey][]string) } themeKey := toolThemeKey(v.Theme) var out []string if v.StreamingActive && strings.TrimSpace(v.Streaming) != "" { out = v.liveTextRows(v.Streaming, width) } else { // Release the finished reply's cached source and rows. v.liveText = liveTextCache{} } finalised := map[string]bool{} for _, m := range v.Messages { for _, c := range m.Content { if b, ok := c.(provider.ToolResultBlock); ok { finalised[b.CallID] = true } } } insertedToolGap := false for _, tc := range v.ToolCalls { if finalised[tc.ID] { continue } if !insertedToolGap && len(out) > 0 { out = append(out, "") } insertedToolGap = true out = append(out, v.renderToolCallCached(tc, width, themeKey)...) } if v.Err != "" { out = append(out, v.renderErr(width)...) // out = append(out, "") } return out } // renderErr formats v.Err into one or more colored rows, word-wrapped // to width so long provider error messages (e.g. a Bedrock 400 with an // embedded JSON body) aren't cut off at the terminal edge. The first // row carries the "✖ " marker; continuation rows align under the text // with a two-cell indent. func (v *View) renderErr(width int) []string { const marker = "✖ " const indent = " " wrapWidth := width - len([]rune(marker)) if wrapWidth < 8 { wrapWidth = 8 } var out []string // Each returned row must represent one terminal line, including when // the error contains explicit newlines or blank separator lines. for _, paragraph := range splitLines(sanitizeText(v.Err, terminalTabStop)) { for _, line := range wrapLine(paragraph, wrapWidth, "") { prefix := indent if len(out) == 0 { prefix = marker } out = append(out, v.Theme.FGColor(v.Theme.Error, prefix+line)) } } return out } func (v *View) renderStartupResources(width int) []string { bodyWidth := width - 2 if bodyWidth < 8 { bodyWidth = 8 } var out []string appendSection := func(label string, items []string) { if len(items) == 0 { return } out = append(out, v.Theme.FGColor(v.Theme.Accent, "["+label+"]")) for _, line := range wrapLine(strings.Join(items, ", "), bodyWidth, "") { out = append(out, v.Theme.FGColor(v.Theme.Muted, " "+line)) } out = append(out, "") } if v.StartupAgentName != "" { appendSection("Agent", []string{v.StartupAgentName}) } appendSection("Context", v.StartupContextPaths) appendSection("Extensions", v.StartupExtensionNames) appendSection("Skills", v.StartupSkillNames) return out } // BuildWithAnchors is like Build but additionally reports the first // row occupied by each message in v.Messages. Callers that need to // scroll to a specific turn (the /jump dialog) use the anchor slice // to map a message index back to a row offset. func (v *View) BuildWithAnchors(width int) ([]string, []MessageAnchor) { themeKey := toolThemeKey(v.Theme) if v.MessagesRevision == 0 && v.messageCacheRevision != 0 { v.messageCache = nil v.messageCacheStart = 0 v.messageCacheRevision = 0 v.messageCacheLen = 0 v.messageCachePrefix = 0 v.messageCachePrefixOK = false } if v.MessagesRevision != 0 { if v.messageCacheRevision != 0 && v.messageCacheRevision != v.MessagesRevision { verifiedAppend := len(v.Messages) > v.messageCacheLen && v.messageCachePrefixOK && hashMessagePrefix(v.Messages, v.messageCacheLen) == v.messageCachePrefix if verifiedAppend { for _, m := range v.Messages[v.messageCacheLen:] { v.messageCachePrefix = appendMessageHash(v.messageCachePrefix, hashMessage(m)) } } else { v.messageCache = nil v.messageCacheStart = 0 v.messageCachePrefix = hashMessagePrefix(v.Messages, len(v.Messages)) } v.messageCachePrefixOK = true } if v.messageCacheRevision == 0 { v.messageCachePrefix = hashMessagePrefix(v.Messages, len(v.Messages)) v.messageCachePrefixOK = true } v.messageCacheRevision = v.MessagesRevision v.messageCacheLen = len(v.Messages) } if v.MessagesRevision == 0 || v.toolPathRevision != v.MessagesRevision { v.refreshToolPaths() v.toolPathRevision = v.MessagesRevision } if v.renderCache == nil { v.renderCache = make(map[msgCacheKey][]string) } if v.liveRenderCache == nil { v.liveRenderCache = make(map[liveToolCacheKey][]string) } v.prepareLiveToolState() // Pre-render every message (hits the cache for unchanged ones) so // we can allocate `out` in a single shot with the exact capacity. // Growing via append on a long transcript copies the backing array // log2(N) times; for a 2000-line scrollback that's enough memcpy // to visibly stutter while typing. rendered := make([][]string, len(v.Messages)) total := 0 // renderFrom is the first index we actually render. Anything // below it emits a zero-row placeholder; the message still gets // an anchor entry so /jump and scroll math keep working, and a // later Build with a higher TailLimit fills the gap. renderFrom := 0 if v.TailLimit > 0 && len(v.Messages) > v.TailLimit { renderFrom = len(v.Messages) - v.TailLimit } v.boundMessageCache(renderFrom, len(v.Messages)) for idx, m := range v.Messages { if idx < renderFrom { rendered[idx] = nil continue } // A turn is "open" once the assistant has started responding // to the most recent user prompt. Walk back over consecutive // assistant/tool messages: if any non-user message precedes // this one without a user message in between, we're inside // the same turn and should not draw a new "▍ zut" header. turnOpen := false if m.Role == provider.RoleAssistant { for j := idx - 1; j >= 0; j-- { prev := v.Messages[j] // Skip compaction summary messages — they're // rendered as a muted footer line (or hidden // entirely when collapsed) and don't open a turn. if prev.Meta["compaction"] == "true" { continue } if prev.Role == provider.RoleAssistant || prev.Role == provider.RoleTool { turnOpen = true } break } } lines := v.renderMessageCached(idx, m, width, turnOpen, themeKey) rendered[idx] = lines total += len(lines) + 1 // +1 for the blank separator row } out := make([]string, 0, total+16) out = append(out, v.renderStartupResources(width)...) anchors := make([]MessageAnchor, 0, len(v.Messages)) for idx := range v.Messages { anchors = append(anchors, MessageAnchor{MessageIdx: idx, Row: len(out)}) out = append(out, rendered[idx]...) // Skip the inter-message blank for messages that rendered // to nothing. An assistant message whose only content is // ToolCallBlock(s) has no rendered output of its own (each // tool result owns its box now), so emitting a blank for it // would compound with the blank from the next real message // and produce two blank rows between adjacent tool boxes. if len(rendered[idx]) == 0 { continue } out = append(out, "") } // Only render the streaming header/body when there's actual // text to show. An empty streaming block (StreamingActive with // Streaming="") appears when a turn starts with a tool_use // block instead of text — in that case the live tool-call // overlay below is the real content and a naked "zut" bar // above it reads as a stray empty message. if v.StreamingActive && strings.TrimSpace(v.Streaming) != "" { // Stream the partial assistant text through the same markdown // renderer used for finalised messages so code fences, diffs, // lists, and inline styles look the same while streaming and // don't suddenly reflow when the turn ends. No speaker header // is drawn; the indent matches the finalised assistant body in // renderMessage so the column stays consistent across the // stream/finalise transition. out = append(out, v.liveTextRows(v.Streaming, width)...) out = append(out, "") } // Live tool-call overlay: keep the in-flight box visible after // the assistant tool_use is appended, then hide it only when the // matching tool_result reaches the transcript. Assistant tool_use // blocks render no rows of their own, so suppressing the overlay // at that point makes the box disappear while the tool is running. finalised := map[string]bool{} for _, m := range v.Messages { for _, c := range m.Content { if b, ok := c.(provider.ToolResultBlock); ok { finalised[b.CallID] = true } } } for _, tc := range v.ToolCalls { if finalised[tc.ID] { continue } out = append(out, v.renderToolCallCached(tc, width, themeKey)...) out = append(out, "") } if v.Err != "" { out = append(out, v.renderErr(width)...) out = append(out, "") } return out, anchors } // prepareLiveToolState drops cached frames and height reservations that no // longer match an active tool version. Expansion changes also reset height // reservations so collapsed dimensions cannot leak into expanded content. func (v *View) prepareLiveToolState() { if v.liveBodyHigh == nil || v.liveBodyExpandAll != v.ExpandAll { v.liveBodyHigh = make(map[string]int) v.liveBodyExpandAll = v.ExpandAll } activeCalls := make(map[string]bool, len(v.ToolCalls)) activeVersions := make(map[liveToolCacheKey]struct{}, len(v.ToolCalls)) for _, tc := range v.ToolCalls { if tc.Result == "" { activeCalls[tc.ID] = true } activeVersions[liveToolCacheKey{ callID: tc.ID, revision: tc.Revision, content: hashLiveTool(tc), }] = struct{}{} } for id := range v.liveBodyHigh { if !activeCalls[id] { delete(v.liveBodyHigh, id) } } for key := range v.liveRenderCache { version := liveToolCacheKey{ callID: key.callID, revision: key.revision, content: key.content, } if _, ok := activeVersions[version]; !ok { delete(v.liveRenderCache, key) } } } // refreshToolPaths rebuilds the tool_use_id -> path map from the // current transcript. Called once per Build() so tool result blocks // (which may be cached) can look up their syntax language when they // were originally rendered. Walking the transcript here is O(N) but // cheap compared to markdown/chroma work it enables. func (v *View) refreshToolPaths() { v.toolPaths = map[string]string{} v.toolStartLines = map[string]int{} v.toolCallLabels = map[string]string{} v.toolCallNames = map[string]string{} v.toolCallArgs = map[string]json.RawMessage{} for _, m := range v.Messages { for _, c := range m.Content { if tc, ok := c.(provider.ToolCallBlock); ok { if p := pathFromToolArgs(tc.Arguments); p != "" { v.toolPaths[tc.ID] = p } if off := offsetFromToolArgs(tc.Arguments); off >= 1 { v.toolStartLines[tc.ID] = off } v.toolCallLabels[tc.ID] = tc.Name + " " + ShortArgs(tc.Name, tc.Arguments) v.toolCallNames[tc.ID] = tc.Name v.toolCallArgs[tc.ID] = tc.Arguments } } } } func (v *View) boundMessageCache(renderFrom, messageCount int) { if renderFrom < 0 { renderFrom = 0 } if renderFrom > messageCount { renderFrom = messageCount } if len(v.messageCache) == 0 { v.messageCacheStart = renderFrom return } start := v.messageCacheStart if renderFrom > start { drop := renderFrom - start if drop >= len(v.messageCache) { v.messageCache = nil v.messageCacheStart = renderFrom return } v.messageCache = v.messageCache[drop:] start = renderFrom } else if renderFrom < start { prefix := make([]messageRenderCacheEntry, start-renderFrom) v.messageCache = append(prefix, v.messageCache...) start = renderFrom } max := messageCount - renderFrom if len(v.messageCache) > max { v.messageCache = v.messageCache[:max] } v.messageCacheStart = start } // renderMessageCached returns the rendered line slice for m, using the // cache if the same (content hash, width, expandAll) combination has // been rendered before. The slice returned is shared — callers must // not mutate it; Build() only ever appends to its own `out` so the // shared slice is safe. func (v *View) renderMessageCached(idx int, m provider.Message, width int, turnOpen bool, themeKey uint64) []string { visual := msgVisualKey{ width: width, expandAll: v.ExpandAll, turnOpen: turnOpen, flatTools: v.FlatTools, compactMode: v.CompactMode, compactUser: v.CompactUser, imageProto: v.ImageProto, theme: themeKey, planRev: v.PlanRevision, } if v.MessagesRevision != 0 { cacheIdx := idx - v.messageCacheStart if cacheIdx >= 0 && cacheIdx < len(v.messageCache) { entry := v.messageCache[cacheIdx] if entry.valid && entry.visual == visual { return entry.lines } } lines := v.renderMessage(m, width, turnOpen) if cacheIdx < 0 { return lines } if cacheIdx >= len(v.messageCache) { v.messageCache = append(v.messageCache, make([]messageRenderCacheEntry, cacheIdx-len(v.messageCache)+1)...) } v.messageCache[cacheIdx] = messageRenderCacheEntry{visual: visual, lines: lines, valid: true} return lines } key := msgCacheKey{hash: hashMessage(m), visual: visual} if v.renderCache != nil { if lines, ok := v.renderCache[key]; ok { return lines } } lines := v.renderMessage(m, width, turnOpen) if v.renderCache != nil { // Bound the cache: 4x the current message count is enough to // survive /compact churn without leaking memory across a very // long session. max := len(v.Messages) * 4 if max < 32 { max = 32 } if len(v.renderCache) > max { // Drop half the entries. map iteration order gives us a // pseudo-LRU for free. dropped := 0 target := len(v.renderCache) / 2 for k := range v.renderCache { if dropped >= target { break } delete(v.renderCache, k) dropped++ } } v.renderCache[key] = lines } return lines } func appendMessageHash(h, messageHash uint64) uint64 { h = fnv64aWriteByte(h, 0x01) for shift := uint(0); shift < 64; shift += 8 { h = fnv64aWriteByte(h, byte(messageHash>>shift)) } return h } func hashMessagePrefix(messages []provider.Message, end int) uint64 { if end > len(messages) { end = len(messages) } h := fnv64aInit for i := 0; i < end; i++ { h = appendMessageHash(h, hashMessage(messages[i])) } return h } // hashMessage returns a 64-bit FNV-1a over the role + content blocks // of m. Serialising each block to its salient bytes is enough: two // messages with the same role and same content render identically. func hashMessage(m provider.Message) uint64 { h := fnv64aInit h = fnv64aWrite(h, []byte(m.Role)) h = fnv64aWriteByte(h, 0) for _, c := range m.Content { switch b := c.(type) { case provider.TextBlock: h = fnv64aWriteByte(h, 't') h = fnv64aWrite(h, []byte(b.Text)) case provider.ImageBlock: h = fnv64aWriteByte(h, 'i') h = fnv64aWrite(h, []byte(b.MimeType)) h = fnv64aWrite(h, b.Data) case provider.ToolCallBlock: h = fnv64aWriteByte(h, 'c') h = fnv64aWrite(h, []byte(b.ID)) h = fnv64aWrite(h, []byte(b.Name)) h = fnv64aWrite(h, []byte(b.Arguments)) case provider.ToolResultBlock: h = fnv64aWriteByte(h, 'r') h = fnv64aWrite(h, []byte(b.CallID)) if b.IsError { h = fnv64aWriteByte(h, 'E') } for _, inner := range b.Content { switch ib := inner.(type) { case provider.TextBlock: h = fnv64aWrite(h, []byte(ib.Text)) case provider.ImageBlock: h = fnv64aWrite(h, []byte(ib.MimeType)) h = fnv64aWrite(h, ib.Data) } } } h = fnv64aWriteByte(h, 0) } return h } // FNV-1a implementation inlined so we don't pay the interface cost of // hash.Hash64 on every Build(). The whole point here is speed. const ( fnv64aInit uint64 = 0xcbf29ce484222325 fnv64aPrime uint64 = 0x100000001b3 ) // assistantBodyRightPad is the blank gutter kept on the right // side of every assistant prose line. const assistantBodyRightPad = 2 // assistantBodyWidth returns the usable width for the assistant // message body (markdown prose + code fences). Uses the full // column width passed in minus assistantBodyRightPad, clamped // at 1 so wrap helpers don't divide by zero on absurdly narrow // terminals. The right-side padding keeps a small breathing // column to the terminal edge that mirrors the left indent. func assistantBodyWidth(outer int) int { outer -= assistantBodyRightPad if outer < 1 { return 1 } return outer } func fnv64aWriteByte(h uint64, b byte) uint64 { h ^= uint64(b) h *= fnv64aPrime return h } func fnv64aWriteUint64(h, value uint64) uint64 { for shift := uint(0); shift < 64; shift += 8 { h = fnv64aWriteByte(h, byte(value>>shift)) } return h } func fnv64aWrite(h uint64, p []byte) uint64 { for _, b := range p { h ^= uint64(b) h *= fnv64aPrime } return h } func (v *View) renderPlanUpdate(update core.PlanUpdate, width int) []string { lines := []string{v.Theme.FGColor(v.Theme.Muted, "• ") + Bold("Updated Plan")} bodyWidth := width - 4 if bodyWidth < 1 { bodyWidth = 1 } var body []string if update.Explanation != nil { if explanation := strings.TrimSpace(*update.Explanation); explanation != "" { for _, line := range wrapLine(sanitizeLine(explanation, codeTabStop), bodyWidth, "") { body = append(body, Dim(Italic(line))) } } } if len(update.Plan) == 0 { body = append(body, Dim(Italic("(no steps provided)"))) } else { for _, step := range update.Plan { marker := "□ " style := func(text string) string { return Dim(text) } switch step.Status { case core.PlanCompleted: marker = "✓ " case core.PlanInProgress: style = func(text string) string { return v.Theme.FGColor(v.Theme.Accent, Bold(text)) } } stepWidth := bodyWidth - 2 if stepWidth < 1 { stepWidth = 1 } wrapped := wrapLine(sanitizeLine(step.Step, codeTabStop), stepWidth, "") if len(wrapped) == 0 { wrapped = []string{""} } body = append(body, style(marker+wrapped[0])) for _, continuation := range wrapped[1:] { body = append(body, style(" "+continuation)) } } } for idx, line := range body { prefix := " " if idx == 0 { prefix = v.Theme.FGColor(v.Theme.Muted, " └ ") } lines = append(lines, prefix+line) } return lines } // isPlanToolName reports whether a tool name is a plan checklist tool. It // accepts the legacy update_plan name too, so a pre-rename transcript's // replayed snapshots keep rendering as checklists. func isPlanToolName(name string) bool { return name == "plan" || name == "update_plan" } // renderPlanCompact is the live-overlay fallback for a plan call whose // checklist snapshot does not exist yet: the arguments are still streaming, so // there is nothing to render but the action being invoked. func (v *View) renderPlanCompact(tc ToolCallView, width int) []string { action, _, _ := ExtractPartialStringField(tc.RawJSONBuf, "action") label := "plan" if action != "" { label = "plan: " + action } return []string{v.Theme.FGColor(v.Theme.Muted, "• "+truncateToWidth(label, width-2))} } func (v *View) renderMessage(m provider.Message, width int, turnOpen bool) []string { var lines []string // Compaction summary: render as a single muted line at the end // of the chat instead of as a user message. if m.Meta["compaction"] == "true" { if v.ExpandAll { return v.renderCompactionBlock(m, width) } // Collapsed: skip entirely. The status bar shows the info. return nil } switch m.Role { case provider.RoleUser: // User rows: no speaker label. Each line is painted with a // faint bubble background tint (UserBubble) so the user's // turns visually segment the chat without needing a header. // One tinted blank row above and below approximates CSS // padding-top / padding-bottom (terminals can't do fractional // rows, so a full row of bubble-coloured whitespace is the // closest analogue and gives the bubble visible breathing // room). // User rows: a 1-cell "▌" accent bar at column 0 painted on // the bubble bg, so the bar's cell shares the panel tint and // there is no visible gap between bar and panel. (Some terminals // — iTerm2, Apple Terminal — may smear the cell-0 bg into the // window inset to the left; that's a terminal-side rendering // quirk we accept in exchange for a clean bar-to-panel join.) const leftGutter = 0 // cells of bubble bg between bar and text const rightGutter = 2 // cells of bubble bg between text and right edge innerWidth := width - 2 - leftGutter - rightGutter // 2 = bar's two cells (▌ + trailing space) if innerWidth < 1 { innerWidth = 1 } bar := v.Theme.BG(v.Theme.UserBubbleBG, v.Theme.FGColor(v.Theme.Accent, "▌ ")) row := func(content string) string { inner := strings.Repeat(" ", leftGutter) + content padded := v.Theme.UserBubble(inner, width-2) return bar + padded } if v.CompactUser || v.CompactMode { // Compact: no tinted bubble background and no padding rows. // Each wrapped row is a quiet "▌ text" gutter line, matching // the flat tool-call header so user turns still segment the // chat without the loud panel. innerWidth = width - 2 if innerWidth < 1 { innerWidth = 1 } row = func(content string) string { if v.CompactMode { content = v.Theme.FGColor(v.Theme.Muted, content) } return v.Theme.FGColor(v.Theme.Accent, "▌ ") + content } } var bubble []string for _, c := range m.Content { switch b := c.(type) { case provider.TextBlock: for _, l := range splitLines(b.Text) { l = sanitizeLine(l, codeTabStop) for _, w := range wrapLine(l, innerWidth, "") { bubble = append(bubble, row(w)) } } case provider.ImageBlock: bubble = append(bubble, row(fmt.Sprintf("[image %s, %d bytes]", b.MimeType, len(b.Data)))) } } if len(bubble) > 0 { if v.CompactUser || v.CompactMode { // No tinted padding rows in compact mode; the inter-message // blank from Build() already gives the turn breathing room. lines = append(lines, bubble...) } else { lines = append(lines, row("")) lines = append(lines, bubble...) lines = append(lines, row("")) } } case provider.RoleAssistant: // Assistant rows: no speaker label either. Prose still gets a // small left indent so it visually aligns with tool box body // content, but no "zut" header. _ = turnOpen for _, c := range m.Content { switch b := c.(type) { case provider.TextBlock: lines = append(lines, renderAssistantText(b.Text, v.Theme, width)...) case provider.ToolCallBlock: // The whole box (top + body + bottom) is rendered by // the matching tool_result message so a single // assistant message that batches multiple tool_use // blocks doesn't produce a stack of unclosed top // edges. Each tool result owns its own box. _ = b } } case provider.RoleTool: for _, c := range m.Content { if tr, ok := c.(provider.ToolResultBlock); ok { if !tr.IsError && isPlanToolName(v.toolCallNames[tr.CallID]) { if update, ok := v.PlanUpdates[tr.CallID]; ok { lines = append(lines, v.renderPlanUpdate(update, width)...) continue } } color := v.Theme.ToolOut if tr.IsError { color = v.Theme.Error } path := "" if v.toolPaths != nil { path = v.toolPaths[tr.CallID] } startLine := 1 if v.toolStartLines != nil { if s := v.toolStartLines[tr.CallID]; s > 0 { startLine = s } } label := "" if v.toolCallLabels != nil { label = v.toolCallLabels[tr.CallID] } label += mutatedFilesSuffix(tr.Context.Mutates) // Each tool result owns a complete box: top edge with // the call's label, body rows wrapped in vertical // edges, bottom edge to close. The label is looked up // from the matching ToolCallBlock so multiple calls // in one assistant message render as N adjacent boxes // instead of stacking unclosed top edges. if v.FlatTools || v.CompactMode { if v.CompactMode { lines = append(lines, compactToolBlank(v.Theme, width)) } lines = append(lines, toolHeaderLine(v.Theme, label, width, v.CompactMode)) if tr.IsError { lines = append(lines, toolBodyLine(v.Theme, v.Theme.FGColor(color, " error"), width, v.CompactMode)) } forceDiff := astRewriteArgs(v.toolCallNames[tr.CallID], v.toolCallArgs[tr.CallID]) for _, line := range v.renderToolResultContent(tr.Content, width, color, path, startLine, forceDiff) { _, stripped := parseImageFootprint(line) lines = append(lines, toolBodyLine(v.Theme, stripped, width, v.CompactMode)) } if v.CompactMode { lines = append(lines, compactToolBlank(v.Theme, width)) } continue } lines = append(lines, toolBoxTop(v.Theme, label, width)) lines = append(lines, toolBoxSide(v.Theme, "", width)) if tr.IsError { lines = append(lines, toolBoxSide(v.Theme, v.Theme.FGColor(color, " error"), width)) } forceDiff := astRewriteArgs(v.toolCallNames[tr.CallID], v.toolCallArgs[tr.CallID]) for _, line := range v.renderToolResultContent(tr.Content, width, color, path, startLine, forceDiff) { // Image-footprint rows (the escape row, the blank // reservation rows beneath it, and the gap row // before the metadata caption) are tagged with the // imageFootprintSentinel by renderImageBlock. Strip // the tag, parse the optional width hint, then wrap // the row in the usual │ ... │ box edges so the // frame stays continuous around the image. imgCells, stripped := parseImageFootprint(line) if hasImageEscapeLine(stripped) { lines = append(lines, toolBoxSideWithImage(v.Theme, stripped, imgCells, width)) } else { lines = append(lines, toolBoxSide(v.Theme, stripped, width)) } } lines = append(lines, toolBoxSide(v.Theme, "", width)) lines = append(lines, toolBoxBottom(v.Theme, width)) } } } return lines } // RenderToolCall renders one live or completed tool call using the same // presentation as tool calls in the main transcript. Dialogs with isolated // agent loops use this to avoid maintaining a second tool renderer. func (v *View) RenderToolCall(tc ToolCallView, width int) []string { if v.liveBodyHigh == nil { v.liveBodyHigh = make(map[string]int) } return v.renderToolCallCached(tc, width, toolThemeKey(v.Theme)) } func (v *View) renderToolCallCached(tc ToolCallView, width int, themeKey uint64) []string { if v.liveRenderCache == nil { v.liveRenderCache = make(map[liveToolCacheKey][]string) } key := liveToolCacheKey{ callID: tc.ID, revision: tc.Revision, content: hashLiveTool(tc), width: width, expandAll: v.ExpandAll, flatTools: v.FlatTools, compact: v.CompactMode, imageProto: v.ImageProto, theme: themeKey, } if lines, ok := v.liveRenderCache[key]; ok { return lines } lines := v.renderToolCall(tc, width) if len(v.liveRenderCache) > 512 { for k := range v.liveRenderCache { delete(v.liveRenderCache, k) break } } v.liveRenderCache[key] = lines return lines } func hashLiveTool(tc ToolCallView) uint64 { if tc.Revision != 0 { return 0 } h := fnv64aInit for _, s := range []string{tc.ID, tc.Name, tc.Args, tc.Preview, tc.Result, tc.RawJSONBuf, tc.LivePath} { h = fnv64aWrite(h, []byte(s)) h = fnv64aWriteByte(h, 0) } if tc.Error { h = fnv64aWriteByte(h, 'e') } if tc.Done { h = fnv64aWriteByte(h, 'd') } if tc.Streaming { h = fnv64aWriteByte(h, 's') } return h } func toolThemeKey(theme Theme) uint64 { h := fnv64aInit for _, s := range []string{ theme.SyntaxBaseStyle, theme.Syntax.Keyword, theme.Syntax.KeywordConstant, theme.Syntax.KeywordDeclaration, theme.Syntax.KeywordNamespace, theme.Syntax.KeywordReserved, theme.Syntax.KeywordType, theme.Syntax.NameBuiltin, theme.Syntax.NameFunction, theme.Syntax.NameClass, theme.Syntax.NameDecorator, theme.Syntax.LiteralString, theme.Syntax.LiteralStringEscape, theme.Syntax.LiteralNumber, theme.Syntax.Comment, theme.Syntax.CommentPreproc, theme.Syntax.Operator, theme.Syntax.Punctuation, theme.Syntax.Text, } { h = fnv64aWrite(h, []byte(s)) h = fnv64aWriteByte(h, 0) } for _, c := range []TerminalColor{ theme.FG, theme.Muted, theme.Accent, theme.User, theme.UserBubbleBG, theme.UserBubbleFG, theme.Assistant, theme.Tool, theme.ToolOut, theme.Error, theme.Warning, theme.Spinner, theme.ThinkingMax, theme.SelectionBG, theme.SelectionFG, theme.Terminal.Foreground, theme.Terminal.Background, } { h = hashThemeColor(h, c) } if theme.Background == nil { h = fnv64aWriteByte(h, 0) } else { h = fnv64aWriteByte(h, 1) h = hashThemeColor(h, *theme.Background) } h = fnv64aWriteByte(h, 0xff) h = fnv64aWriteUint64(h, uint64(len(theme.Terminal.Palette))) h = fnv64aWriteByte(h, 0xff) for _, c := range theme.Terminal.Palette { h = hashThemeColor(h, c) } h = fnv64aWriteByte(h, byte(theme.Terminal.ColorDepth())) h = fnv64aWriteUint64(h, uint64(theme.Terminal.PaletteKnown)) for _, value := range []bool{ theme.UseTerminalPalette, theme.Terminal.HasForeground, theme.Terminal.HasBackground, theme.Terminal.TrueColor, theme.Terminal.Light, theme.Terminal.SchemeKnown, } { if value { h = fnv64aWriteByte(h, 1) } else { h = fnv64aWriteByte(h, 0) } } return h } func hashThemeColor(h uint64, c TerminalColor) uint64 { h = fnv64aWriteByte(h, byte(c.Mode)) h = fnv64aWriteByte(h, byte(c.Index)) h = fnv64aWriteByte(h, byte(c.R)) h = fnv64aWriteByte(h, byte(c.G)) h = fnv64aWriteByte(h, byte(c.B)) return h } func (v *View) renderToolCall(tc ToolCallView, width int) []string { if isPlanToolName(tc.Name) { // The checklist is materialized only once core accepts the call, so the // live overlay renders from the snapshot map. Before execution there is // no snapshot yet: name the action instead of inventing a list. if update, ok := v.PlanUpdates[tc.ID]; ok { return v.renderPlanUpdate(update, width) } if !tc.Done && !tc.Error { return v.renderPlanCompact(tc, width) } } var lines []string // Header label. While the call is still streaming, prefer the // live path extracted from the partial args so the user sees // the target file as soon as it's known, even before the full // JSON arrived. arg := tc.Args if arg == "" && tc.LivePath != "" { arg = tc.LivePath } label := tc.Name + " " + arg // No leading blank: Build()'s inter-message separator already // places one blank row between the previous transcript content // and the live overlay, matching the spacing the same call has // once it finalises into a transcript box. Adding another blank // here would double the gap during streaming and visibly tighten // when the overlay disappears. // Live body (write/edit): keep the streamed preview visible until // a proposed or real tool result arrives. The provider can finish the // tool_use JSON before zut has executed the tool, so keying this on // tc.Streaming makes write/edit boxes collapse for a moment between // EvToolUseEnd and confirmation or EvToolResult. if tc.Preview == "" && tc.Result == "" { if body := v.renderLiveToolBody(tc, width); len(body) > 0 { // Pad the live preview up to the call's high-water height so // it never shrinks mid-stream (e.g. between edit 1 and the // start of edit 2). Padding sits inside the box as real // box-side rows so the surrounding layout stays put. The // high-water mark is per call id and is dropped the moment a // Result lands (this branch only runs while Result == ""). high := len(body) // Live edits otherwise grow from a few rows to the collapsed // preview limit as newText arrives, moving the editor on every // line. Reserve the final collapsed height from the first visible // edit frame; ctrl+o still opts into unconstrained growth. if !v.ExpandAll && (tc.Name == "edit" || tc.Name == "Edit") && high < ToolCollapsePreview+2 { high = ToolCollapsePreview + 2 } if v.liveBodyHigh != nil { if prev := v.liveBodyHigh[tc.ID]; prev > high { high = prev } v.liveBodyHigh[tc.ID] = high } if v.FlatTools || v.CompactMode { for len(body) < high { body = append(body, "") } if v.CompactMode { lines = append(lines, compactToolBlank(v.Theme, width)) } lines = append(lines, toolHeaderLine(v.Theme, label, width, v.CompactMode)) lines = append(lines, body...) if v.CompactMode { lines = append(lines, compactToolBlank(v.Theme, width)) } return lines } for len(body) < high { body = append(body, toolBoxSide(v.Theme, "", width)) } lines = append(lines, toolBoxTop(v.Theme, label, width)) lines = append(lines, toolBoxSide(v.Theme, "", width)) lines = append(lines, body...) lines = append(lines, toolBoxSide(v.Theme, "", width)) lines = append(lines, toolBoxBottom(v.Theme, width)) return lines } } bodyText := tc.Result if bodyText == "" { bodyText = tc.Preview } // Finished tool call with no body: just the labelled top edge // directly closed by the bottom. Avoids a blank interior row // for no-output tools. if bodyText == "" { if v.FlatTools || v.CompactMode { if v.CompactMode { lines = append(lines, compactToolBlank(v.Theme, width)) } lines = append(lines, toolHeaderLine(v.Theme, label, width, v.CompactMode)) if v.CompactMode { lines = append(lines, compactToolBlank(v.Theme, width)) } return lines } lines = append(lines, toolBoxTop(v.Theme, label, width)) lines = append(lines, toolBoxBottom(v.Theme, width)) return lines } // Finished tool call with a result body. Top edge embeds the // label; body lines go through toolBoxSide so each row gets // vertical edges; bottom edge closes the box. Blank interior // rows after the top and before the bottom give the body a bit // of breathing room from the corners. color := v.Theme.ToolOut if tc.Error { color = v.Theme.Error } if v.FlatTools || v.CompactMode { if v.CompactMode { lines = append(lines, compactToolBlank(v.Theme, width)) } lines = append(lines, toolHeaderLine(v.Theme, label, width, v.CompactMode)) body := v.renderLiveToolResult(bodyText, flatToolBodyRenderWidth(width), color, tc.LivePath) for _, l := range v.collapseToolBody(body, false) { _, stripped := parseImageFootprint(l) lines = append(lines, toolBodyLine(v.Theme, stripped, width, v.CompactMode)) } if v.CompactMode { lines = append(lines, compactToolBlank(v.Theme, width)) } return lines } lines = append(lines, toolBoxTop(v.Theme, label, width)) lines = append(lines, toolBoxSide(v.Theme, "", width)) body := v.renderLiveToolResult(bodyText, toolBoxBodyRenderWidth(width), color, tc.LivePath) for _, l := range v.collapseToolBody(body, false) { imgCells, stripped := parseImageFootprint(l) if hasImageEscapeLine(stripped) { lines = append(lines, toolBoxSideWithImage(v.Theme, stripped, imgCells, width)) continue } lines = append(lines, toolBoxSide(v.Theme, stripped, width)) } lines = append(lines, toolBoxSide(v.Theme, "", width)) lines = append(lines, toolBoxBottom(v.Theme, width)) return lines } // renderLiveToolResult uses the transcript's text renderer so every rich // confirmation preview immediately gets the same colors, numbered gutters, // and syntax highlighting as its final tool result. func (v *View) renderLiveToolResult(text string, width int, color TerminalColor, sourcePath string) []string { full := v.renderToolText(text, width, color, sourcePath, 1) if v.ExpandAll || len(full) <= ToolCollapseLines { return full } return v.collapseToolBodyWithTotal(full, false, len(full)) } // renderLiveToolBody renders the in-flight preview of a streaming // tool call. Supported tools: // // - write: shows the partial `content` field, syntax-highlighted // by the target path's language. // - edit: shows the partial `newText` of the edit currently being // streamed, prefixed with a "editing foo.ts (edit 2)" header so // the user can see which of a multi-edit batch is in progress. // - bash: shows the full command as soon as the JSON args are known, // before stdout/stderr has arrived, so long-running commands are legible. // // Anything else returns nil and only the tool-call header shows. func (v *View) renderLiveToolBody(tc ToolCallView, width int) []string { switch tc.Name { case "write", "Write": partial, ok, _ := ExtractPartialStringField(tc.RawJSONBuf, "content") if !ok || partial == "" { return nil } return v.wrapLiveBody(v.renderRawFile(partial, tc.LivePath, 1), width) case "edit", "Edit": partial, ok, _, idx := ExtractLastNewText(tc.RawJSONBuf) if !ok || partial == "" { return nil } // Header line hints which edit is streaming and, when more // than one has landed, how many the model is doing. hint := fmt.Sprintf("edit %d (streaming)", idx) body := []string{" " + v.Theme.FGColor(v.Theme.Muted, hint), ""} body = append(body, v.renderRawFile(partial, tc.LivePath, 1)...) return v.wrapLiveBody(body, width) case "bash", "Bash": command, ok, _ := ExtractPartialStringField(tc.RawJSONBuf, "command") if !ok || strings.TrimSpace(command) == "" { return nil } return v.wrapLiveBody(v.renderLiveBashCommand(command, width), width) } return nil } func (v *View) renderLiveBashCommand(command string, width int) []string { inner := toolBoxBodyRenderWidth(width) if v.FlatTools || v.CompactMode { inner = flatToolBodyRenderWidth(width) } prompt := v.Theme.FGColor(v.Theme.Muted, "$ ") var out []string for i, line := range splitLines(sanitizeText(command, terminalTabStop)) { firstPrefix := " " if i == 0 { firstPrefix += prompt } else { firstPrefix += " " } contPrefix := " " wrapWidth := inner - visibleWidth(firstPrefix) if wrapWidth < 10 { wrapWidth = 10 } for j, wrapped := range wrapANSILine(v.Theme.FGColor(v.Theme.ToolOut, line), wrapWidth) { prefix := firstPrefix if j > 0 { prefix = contPrefix } out = append(out, prefix+wrapped) } } return out } // wrapLiveBody returns the streaming body content as a list of // box-side rows: each line wrapped in │ ... │ with right padding so // the closing edge sits at column width-1. The caller (renderToolCall) // supplies the surrounding top/bottom edges so the live overlay // renders as a closed box matching the finalised transcript form. func (v *View) wrapLiveBody(body []string, width int) []string { body = v.collapseToolBody(body, false) out := make([]string, 0, len(body)) if v.FlatTools || v.CompactMode { for _, l := range body { _, stripped := parseImageFootprint(l) out = append(out, toolBodyLine(v.Theme, stripped, width, v.CompactMode)) } return out } for _, l := range body { imgCells, stripped := parseImageFootprint(l) if hasImageEscapeLine(stripped) { out = append(out, toolBoxSideWithImage(v.Theme, stripped, imgCells, width)) continue } out = append(out, toolBoxSide(v.Theme, stripped, width)) } return out } // toolBoxInnerPad is the number of blank cells kept between a box // edge (┌, │, └) and the content inside it. One cell of breathing // room makes the label read as part of the box rather than fused to // the corner, and gives body lines a tiny gutter from the left side. const toolBoxInnerPad = 1 // toolBoxOuterMargin is the number of blank cells kept to the left of // the opening corner and to the right of the closing corner. Aligns // the box's frame with the column where user-bubble text begins, so // the conversation reads as one column instead of having tool boxes // running edge-to-edge while user/assistant rows sit indented. const toolBoxOuterMargin = 2 // toolBoxTop renders the labelled top edge of a tool block: // // ┌─ bash xcrun simctl list devices ─────────────────────────────┐ // // The label is the tool name + short args (the same string previously // shown on its own row beneath the rule). Padding fills the remainder // of the line so the right corner sits at column width-1, matching the // closing edge. func toolBoxTop(th Theme, label string, width int) string { label = oneLineToolLabel(label) w := width - 2*toolBoxOuterMargin if w < 12 { w = 12 } margin := strings.Repeat(" ", toolBoxOuterMargin) innerPad := strings.Repeat(" ", toolBoxInnerPad) // "┌─" + innerPad + " " + label + " " + innerPad = used; pad // with ─ to width-1, then "┐". prefix := "┌─" + innerPad + " " suffix := " " + innerPad used := visibleWidth(prefix) + visibleWidth(label) + visibleWidth(suffix) fill := w - used - 1 // -1 for the closing corner if fill < 0 { // Label overflows; truncate with an ellipsis so the right // corner still lands on the right edge. Rare on real // terminals (the chat column is usually wide enough). label = truncateWithEllipsis(label, visibleWidth(label)+fill) used = visibleWidth(prefix) + visibleWidth(label) + visibleWidth(suffix) fill = w - used - 1 if fill < 0 { fill = 0 } } fillStr := strings.Repeat("─", fill) name, rest := splitToolLabel(label) return margin + th.FGColor(th.Muted, prefix) + th.FGColor(th.FG, name) + th.FGColor(th.Muted, rest+suffix+fillStr+"┐") + margin } // truncateWithEllipsis shortens plain text to at most limit terminal // cells, ending with "..." when it had to cut. Counting cells rather than // runes keeps wide and zero-width characters from pushing the row past // limit. func truncateWithEllipsis(s string, limit int) string { if visibleWidth(s) <= limit { return s } if limit <= 3 { return strings.Repeat(".", max(0, limit)) } return truncateToWidth(s, limit-3) + "..." } func oneLineToolLabel(label string) string { return strings.Join(strings.Fields(sanitizeLine(label, 1)), " ") } func splitToolLabel(label string) (name, rest string) { label = strings.TrimLeft(label, " ") if label == "" { return "", "" } idx := strings.IndexAny(label, " \t") if idx < 0 { return label, "" } return label[:idx], label[idx:] } // flatToolHeader renders the boxless header line for a tool call: // // ▌ bash xcrun simctl list devices // // It carries the same information as toolBoxTop (tool name + short // args) but drops the frame: a single accent gutter glyph, the tool // name in the foreground color, and the argument summary muted. The // line is left-aligned at the same column the box's opening corner // used (toolBoxOuterMargin) so flat and box renders share a column. func toolHeaderLine(th Theme, label string, width int, compact bool) string { if compact { return compactToolHeader(th, label, width) } return flatToolHeader(th, label, width) } func flatToolHeader(th Theme, label string, width int) string { label = oneLineToolLabel(label) name, rest := splitToolLabel(label) margin := strings.Repeat(" ", toolBoxOuterMargin) gutter := th.FGColor(th.Assistant, "▌") + " " // Budget the visible text to the content width so a very long // argument summary doesn't run off the right edge; truncate with // an ellipsis like the box header does. avail := width - toolBoxOuterMargin - visibleWidth("▌ ") if avail < 12 { avail = 12 } if visibleWidth(name+rest) > avail { if visibleWidth(name)+3 < avail { rest = truncateWithEllipsis(rest, avail-visibleWidth(name)) } else if visibleWidth(name) <= avail { rest = "" } } return margin + gutter + th.FGColor(th.FG, name) + th.FGColor(th.Muted, rest) } func compactToolBlank(th Theme, width int) string { if width < 1 { width = 1 } return th.sgrBGColor(th.UserBubbleBG) + strings.Repeat(" ", width) + reset } func compactToolHeader(th Theme, label string, width int) string { label = oneLineToolLabel(label) name, rest := splitToolLabel(label) pad := " " avail := width - visibleWidth(pad)*2 if avail < 12 { avail = 12 } if visibleWidth(name+rest) > avail { if visibleWidth(name)+3 < avail { rest = truncateWithEllipsis(rest, avail-visibleWidth(name)) } else if visibleWidth(name) <= avail { rest = "" } } plain := pad + name + rest visible := visibleWidth(plain) fill := "" if visible < width { fill = strings.Repeat(" ", width-visible) } bg := th.sgrBGColor(th.UserBubbleBG) return bg + pad + th.fgPrefix(th.FG) + name + bg + th.fgPrefix(th.Muted) + rest + bg + fill + reset } func toolBodyLine(th Theme, line string, width int, compact bool) string { line = flatToolBody(th, line) if compact { return compactToolBody(th, line, width) } return line } // flatToolBody indents a single body line for boxless rendering. It // trims the same leading padding box bodies do, then prepends a fixed // gutter indent so output sits in a readable column under the header // without any vertical frame. ANSI styling in the line is preserved. func flatToolBody(th Theme, line string) string { _ = th line = trimLeadingSpaces(line, toolBoxBodyTrimLeft) return strings.Repeat(" ", toolBoxOuterMargin+2) + line } func compactToolBody(th Theme, line string, width int) string { visible := visibleWidth(line) fill := "" if visible < width { fill = strings.Repeat(" ", width-visible) } bg := th.sgrBGColor(th.UserBubbleBG) return bg + strings.ReplaceAll(line, reset, reset+bg) + bg + fill + reset } // toolBoxBottom renders the bottom edge of a tool block: // // └─────────────────────────────────────────────────────────────────────────────┘ // // Spans the same width as toolBoxTop so the corners line up. func toolBoxBottom(th Theme, width int) string { w := width - 2*toolBoxOuterMargin if w < 12 { w = 12 } margin := strings.Repeat(" ", toolBoxOuterMargin) line := "└" + strings.Repeat("─", w-2) + "┘" return margin + th.FGColor(th.Muted, line) + margin } // hasImageEscapeLine reports whether s contains a Kitty (\x1b_G) or // iTerm2 (\x1b]1337;File=) inline-image escape. Such rows draw into // the terminal's graphics layer and shouldn't be wrapped with text // box edges. func hasImageEscapeLine(s string) bool { return strings.Contains(s, "\x1b]1337;File=") || strings.Contains(s, "\x1b_G") } // imageFootprintSentinel marks rows that belong to an inline-image's // reserved footprint — the escape row plus the blank rows below it // plus the gap row before the metadata caption. Any consumer that // wraps content in box edges (│ ... │) detects the sentinel, strips // it, and emits the row — the image graphics rectangle paints over // whatever was drawn there. Uses a non-printing C0 control byte so // it can never appear in normal text or in an ANSI escape sequence // body. // // The escape row carries an extra width hint between two sentinels: // "\x1e\x1e". Wrappers parse the cells count // to know how wide the image is in terminal cells and how many cells // of trailing padding are needed before the closing box edge. const imageFootprintSentinel = "\x1e" // parseImageFootprint splits a footprint-tagged row into (cellsWide, // rest). cellsWide is 0 when the row carries no width hint (the // reservation blanks and gap row don't, only the escape row does). func parseImageFootprint(s string) (int, string) { if !strings.HasPrefix(s, imageFootprintSentinel) { return 0, s } rest := s[len(imageFootprintSentinel):] end := strings.Index(rest, imageFootprintSentinel) if end < 0 { return 0, rest } w, err := strconv.Atoi(rest[:end]) if err != nil { return 0, rest } return w, rest[end+len(imageFootprintSentinel):] } // toolBoxBodyTrimLeft is the number of leading literal spaces stripped // from each body line as it enters toolBoxSide. Body renderers // (renderToolText, renderRawFile, the diff/bash helpers, ...) all // emit a 4-cell indent so the content aligns with the assistant body // column when there's no box around it. Inside a box the box edge + // inner pad already supply most of that gutter, so we drop a couple // of cells from the body's own indent to keep the content from // drifting too far right. const toolBoxBodyTrimLeft = 2 // toolBoxBodyRenderWidth is the width body renderers should target before // their rows are wrapped by toolBoxSide. Most body renderers emit a four-cell // indent; toolBoxSide trims toolBoxBodyTrimLeft cells from that indent and // then adds the box edge/padding. Passing the full terminal width lets long // bash/heredoc lines overrun the inner box and makes terminals soft-wrap, // visually breaking the right edge. This returns the maximum renderer width // whose post-trim visible width fits inside the box. func toolBoxBodyRenderWidth(width int) int { w := width - 2*toolBoxOuterMargin if w < 12 { w = 12 } inner := w - 2 - 2*toolBoxInnerPad if inner < 1 { inner = 1 } return inner + toolBoxBodyTrimLeft } // flatToolBodyRenderWidth is the width body renderers should target in // flat mode. There's no box frame, just the flatToolBody indent // (toolBoxOuterMargin+2 cells), so the renderable width is the // terminal width minus that indent. Renderers emit a 4-cell indent of // their own which flatToolBody trims toolBoxBodyTrimLeft cells from, // so add that back like toolBoxBodyRenderWidth does. func flatToolBodyRenderWidth(width int) int { inner := width - (toolBoxOuterMargin + 2) if inner < 1 { inner = 1 } return inner + toolBoxBodyTrimLeft } // toolBoxSide wraps a single body line with vertical box edges: // // │ foo bar baz │ // // The middle preserves the line verbatim (including any ANSI escapes) // after stripping toolBoxBodyTrimLeft leading literal spaces. One cell // of inner padding sits on each side so the content breathes from the // edges; the right padding fills to column width-1. func toolBoxSide(th Theme, line string, width int) string { w := width - 2*toolBoxOuterMargin if w < 12 { w = 12 } margin := strings.Repeat(" ", toolBoxOuterMargin) mutedEdge := th.FGColor(th.Muted, "│") left := mutedEdge + strings.Repeat(" ", toolBoxInnerPad) right := strings.Repeat(" ", toolBoxInnerPad) + mutedEdge inner := w - 2 - 2*toolBoxInnerPad // available between the two pads line = trimLeadingSpaces(line, toolBoxBodyTrimLeft) cur := visibleWidth(line) if cur > inner { // Last-resort truncation must retain syntax foreground escapes. // Stripping ANSI here made long read results switch from their // configured syntax color to the terminal's default foreground // whenever the window became narrow enough to clip a line. line = truncateToWidth(line, inner) cur = visibleWidth(line) } pad := inner - cur if pad < 0 { pad = 0 } return margin + left + line + strings.Repeat(" ", pad) + right + margin } // toolBoxSideWithImage wraps a row that carries an inline-image // escape sequence. The image protocols are configured to not move // the cursor after rendering (iTerm: doNotMoveCursor=1, kitty/ // ghostty: C=1), so the only visible cell advance on the row is // from the leading indent spaces before the escape. We pad from // there to the right edge column and draw the closing │ there. // imgCells is unused (kept in the signature for compatibility with // the parseImageFootprint width hint). func toolBoxSideWithImage(th Theme, line string, imgCells, width int) string { _ = imgCells w := width - 2*toolBoxOuterMargin if w < 12 { w = 12 } margin := strings.Repeat(" ", toolBoxOuterMargin) mutedEdge := th.FGColor(th.Muted, "│") left := mutedEdge + strings.Repeat(" ", toolBoxInnerPad) right := strings.Repeat(" ", toolBoxInnerPad) + mutedEdge inner := w - 2 - 2*toolBoxInnerPad line = trimLeadingSpaces(line, toolBoxBodyTrimLeft) // Count leading spaces (visible cells consumed before the escape). leading := 0 for leading < len(line) && line[leading] == ' ' { leading++ } pad := inner - leading if pad < 0 { pad = 0 } return margin + left + line + strings.Repeat(" ", pad) + right + margin } // trimLeadingSpaces removes up to n literal space characters from the // start of s without touching anything else. ANSI CSI escapes that // appear before the spaces are skipped over (so a coloured indent // like "\x1b[2m text" still loses its first n cells of indent). // Stops as soon as it hits a non-space character. func trimLeadingSpaces(s string, n int) string { if n <= 0 || s == "" { return s } var b strings.Builder b.Grow(len(s)) trimmed := 0 i := 0 for i < len(s) { // Pass through any ANSI escape sequence verbatim. if s[i] == 0x1b && i+1 < len(s) && s[i+1] == '[' { end := i + 2 for end < len(s) { c := s[end] end++ if c >= 0x40 && c <= 0x7e { break } } b.WriteString(s[i:end]) i = end continue } if trimmed < n && s[i] == ' ' { trimmed++ i++ continue } b.WriteString(s[i:]) return b.String() } return b.String() } // renderToolResultContent renders the body of a tool result block. // Text blocks get the usual rules-wrapped treatment; text that looks // like a unified diff gets +/- coloring. Image blocks are rendered // inline when the terminal supports a protocol, else as a text // placeholder with dimensions. func (v *View) renderToolResultContent(blocks []provider.Content, width int, color TerminalColor, sourcePath string, startLine int, forceDiff bool) []string { var body []string hasImage := false for _, b := range blocks { if _, ok := b.(provider.ImageBlock); ok { hasImage = true break } } total := 0 for _, b := range blocks { switch bb := b.(type) { case provider.TextBlock: bodyWidth := toolBoxBodyRenderWidth(width) if v.FlatTools || v.CompactMode { bodyWidth = flatToolBodyRenderWidth(width) } estimateWidth := effectiveToolTextWidth(bb.Text, bodyWidth) if forceDiff { estimateWidth = max(1, bodyWidth-5) } estimated := estimateToolTextLines(bb.Text, estimateWidth) total += estimated text := bb.Text // A collapsed result only exposes its first ten rendered rows. // Render a bounded source preview in that case; syntax // highlighting and wrapping the omitted tail is wasted work. if !v.ExpandAll && !hasImage && estimated > ToolCollapseLines { text = previewToolText(text, estimateWidth) } if forceDiff { body = append(body, v.renderUnifiedDiff(text, bodyWidth, sourcePath)...) } else { body = append(body, v.renderToolText(text, bodyWidth, color, sourcePath, startLine)...) } case provider.ImageBlock: body = append(body, v.renderImageBlock(bb, width)...) } } return v.collapseToolBodyWithTotal(body, hasImage, total) } // collapseToolBody trims lines to the configured preview size when the // view is not in ExpandAll mode, appending a muted "... ctrl+o to // expand" footer. Image blocks never collapse — they're short in text // rows but represent real content the user wants to see. func (v *View) collapseToolBody(lines []string, hasImage bool) []string { return v.collapseToolBodyWithTotal(lines, hasImage, len(lines)) } func (v *View) collapseToolBodyWithTotal(lines []string, hasImage bool, total int) []string { if v.ExpandAll || hasImage { return lines } if total < len(lines) { total = len(lines) } if total <= ToolCollapseLines { return lines } kept := lines if len(kept) > ToolCollapsePreview { kept = kept[:ToolCollapsePreview] } hidden := total - ToolCollapsePreview footer := fmt.Sprintf(" ... (%d more lines, %d total, ctrl+o to expand)", hidden, total) footer = v.Theme.FGColor(v.Theme.Muted, footer) out := append([]string(nil), kept...) out = append(out, "") return append(out, footer) } func effectiveToolTextWidth(text string, width int) int { // renderToolText prefixes ordinary rows with a four-cell gutter before // wrapping them. Diff rows also carry a five-cell read gutter. Estimating // against the outer body width undercounts long rows and produces an // incorrect collapse total. width -= 4 if looksLikeUnifiedDiff(text) { width -= 5 } if width < 1 { return 1 } return width } func estimateToolTextLines(text string, width int) int { if width < 1 { width = 1 } total := 0 for _, line := range strings.Split(text, "\n") { cells := runewidth.StringWidth(line) if cells == 0 { total++ continue } total += (cells + width - 1) / width } if total == 0 { return 1 } return total } func previewToolText(text string, width int) string { if width < 1 { width = 1 } maxChars := width * (ToolCollapsePreview + 2) if maxChars < 256 { maxChars = 256 } if maxChars > 8192 { maxChars = 8192 } end := 0 for count := 0; end < len(text) && count < maxChars; count++ { _, size := utf8.DecodeRuneInString(text[end:]) if size == 0 { break } end += size } preview := text[:end] lines := strings.SplitAfter(preview, "\n") if len(lines) > ToolCollapsePreview+2 { preview = strings.Join(lines[:ToolCollapsePreview+2], "") } return preview } // renderToolText renders a text block inside a tool result. If the // text contains a unified-diff section (lines starting with "--- " / // "+++ " / "+" / "-"/" "), those rows are styled with add/remove // colors matching git diff conventions. func (v *View) renderToolText(text string, width int, defaultColor TerminalColor, sourcePath string, startLine int) []string { // Legacy path: transcripts saved before we dropped line numbers // from the read tool still carry " 1\t..." prefixes. Detect and // strip them, then fall through to the highlighter. if looksLikeNumberedFile(text) { return v.renderNumberedFile(text, sourcePath) } // If the result embeds a unified diff (the edit tool's output // starts with a short "applied N edit(s)" line and then a // standard --- / +++ / +/- patch), render the patch with // add/remove coloring. This takes priority over the file-like // detector below because a diff technically has many lines of // "file content" but what the user cares about is what changed, // not a dump of the post-edit file. if looksLikeUnifiedDiff(text) { return v.renderUnifiedDiff(text, width, sourcePath) } // Current path: text came from `read` as raw file bytes. When a // source path is known (the call had a `path` arg), render with // a synthetic line-number gutter starting at startLine so the // on-screen view still looks like cat -n. Doesn't apply to non- // file tool outputs (bash stdout, display notes, etc.). if sourcePath != "" && looksLikeFileContent(text) { return v.renderRawFile(text, sourcePath, startLine) } // No truncation — the full tool output is rendered into chat and // becomes part of the scrollback you can page back through. lines := splitLines(text) // Bash-result styling: when the first row looks like a shell // prompt line ("$ ...") emitted by the bash tool, style the // prompt line in accent and the trailing "[exit N] Took X.Ys" // line in muted type so the command + timing read at a glance. // Everything between is left on the default tool-output color. if len(lines) > 0 && strings.HasPrefix(lines[0], "$ ") { return v.renderBashResult(lines, width, defaultColor) } inDiff := false oldLine, newLine := 1, 1 var out []string for _, l := range lines { // Detect diff header: "--- name" followed somewhere by "+++ name". if strings.HasPrefix(l, "--- ") || strings.HasPrefix(l, "+++ ") { inDiff = true out = append(out, " "+v.Theme.FGColor(v.Theme.Muted, sanitizeLine(l, terminalTabStop))) continue } // Hunk header "@@ -a,b +c,d @@" resets the counters so patches // that skip around in the file still get correct numbering. if inDiff && strings.HasPrefix(l, "@@") { if o, n, ok := parseHunkHeader(l); ok { oldLine, newLine = o, n } out = append(out, " "+v.Theme.FGColor(v.Theme.Muted, sanitizeLine(l, terminalTabStop))) continue } if inDiff && strings.TrimSpace(l) == "..." { out = append(out, "") out = append(out, " "+v.Theme.FGColor(v.Theme.Muted, "...")) continue } if inDiff && len(l) > 0 { switch l[0] { case '+': out = append(out, v.renderDiffRow(l, width, v.Theme.Tool, newLine, '+', sourcePath)...) newLine++ continue case '-': out = append(out, v.renderDiffRow(l, width, v.Theme.Error, oldLine, '-', sourcePath)...) oldLine++ continue case ' ': out = append(out, v.renderDiffRow(l, width, v.Theme.Muted, newLine, ' ', sourcePath)...) oldLine++ newLine++ continue } } // Regular line. for _, w := range wrapLine(sanitizeLine(l, terminalTabStop), width-4, " ") { out = append(out, " "+v.Theme.FGColor(defaultColor, w)) } } return out } // parseHunkHeader extracts the starting old/new line from a unified // diff hunk header ("@@ -12,5 +12,7 @@ ..."). Returns ok=false if the // header is malformed or missing numbers. func parseHunkHeader(l string) (oldStart, newStart int, ok bool) { // Skip "@@ " rest := strings.TrimPrefix(l, "@@") rest = strings.TrimSpace(rest) if !strings.HasPrefix(rest, "-") { return 0, 0, false } rest = rest[1:] space := strings.IndexByte(rest, ' ') if space < 0 { return 0, 0, false } oldPart := rest[:space] rest = strings.TrimSpace(rest[space+1:]) if !strings.HasPrefix(rest, "+") { return 0, 0, false } rest = rest[1:] if sp := strings.IndexAny(rest, " \t"); sp >= 0 { rest = rest[:sp] } parseStart := func(s string) (int, bool) { if c := strings.IndexByte(s, ','); c >= 0 { s = s[:c] } n, err := strconv.Atoi(s) if err != nil || n < 1 { return 0, false } return n, true } o, ok1 := parseStart(oldPart) n, ok2 := parseStart(rest) if !ok1 || !ok2 { return 0, 0, false } return o, n, true } // renderDiffRow renders one unified-diff source line as one or more visual // rows. The first row carries the read-style line-number gutter; continuation // rows use a blank gutter so wrapped content cannot be mistaken for another // source line. Code is syntax-highlighted if sourcePath hints at a known // language and otherwise uses the diff color. func (v *View) renderDiffRow(line string, width int, color TerminalColor, lineNo int, mark byte, sourcePath string) []string { if len(line) == 0 { return []string{""} } code := sanitizeLine(line[1:], codeTabStop) // strip the leading marker // Gutter shape: sign + number share a color so they read as one // visual token ("+123") instead of a neutral line number next to // a stray marker. Unchanged context lines get a muted gutter and // a leading space so column alignment stays consistent with +/- // rows. var gutterText string switch mark { case '+': gutterText = fmt.Sprintf("+%3d ", lineNo) case '-': gutterText = fmt.Sprintf("-%3d ", lineNo) default: gutterText = fmt.Sprintf(" %3d ", lineNo) } gutterColor := color if mark == ' ' { gutterColor = v.Theme.Muted } maxCode := width - visibleWidth(" "+gutterText) if maxCode < 1 { maxCode = 1 } chunks := wrapCodeLine(code, maxCode) lang := LanguageFromPath(sourcePath) out := make([]string, 0, len(chunks)) for i, chunk := range chunks { rowGutter := gutterText if i > 0 { rowGutter = strings.Repeat(" ", visibleWidth(gutterText)) } codeRendered := "" if lang != "" { if h := v.Theme.HighlightCode(chunk, lang); len(h) == 1 { codeRendered = h[0] } } if codeRendered == "" { codeColor := color if mark == ' ' { codeColor = v.Theme.Muted } codeRendered = v.Theme.FGColor(codeColor, chunk) } out = append(out, " "+v.Theme.FGColor(gutterColor, rowGutter)+codeRendered) } return out } // wrapCodeLine splits code to a terminal cell width without dropping any // characters. It prefers breaking after whitespace, then falls back to a hard // rune boundary for long tokens. Keeping the whitespace on the preceding row // means joining the returned chunks reconstructs the expanded source exactly. func wrapCodeLine(line string, width int) []string { if width < 1 || visibleWidth(line) <= width { return []string{line} } runes := []rune(line) var out []string for start := 0; start < len(runes); { cells := 0 end := start lastBreak := -1 for end < len(runes) { runeWidth := runewidth.RuneWidth(runes[end]) if cells+runeWidth > width && end > start { break } cells += runeWidth end++ if runes[end-1] == ' ' || runes[end-1] == '\t' { lastBreak = end } if cells >= width { break } } if end == start { // A rune wider than the requested width. end++ } if end < len(runes) && lastBreak > start { end = lastBreak } out = append(out, string(runes[start:end])) start = end } return out } // renderImageBlock returns the lines for one image, inline if possible. // // Inline image escapes paint into multiple terminal rows but the zut // renderer treats each slice entry as a single row. To prevent chat // content from being drawn on top of the image, we pad with blank rows // so the image's real footprint is reflected in the frame height. func (v *View) renderImageBlock(b provider.ImageBlock, width int) []string { w, h := ImageDimensions(b.Data) kb := len(b.Data) / 1024 // Four-space indent matches every other tool-output row // (renderRawFile, renderBashResult, diff rows, "... N more // lines" footers). Keeps the image's metadata line vertically // aligned with the file content above / below it. info := fmt.Sprintf(" image - %s - %dx%d - %d KB", b.MimeType, w, h, kb) if v.ImageProto != ImageProtocolNone { // Clamp rendered width so the image never overflows the chat // column. Subtract a 4-cell indent for the tool result block, // cap at 60 cells, floor at 10. cells := width - 4 if cells > 60 { cells = 60 } if cells < 10 { cells = 10 } const maxRows = 20 if seq := RenderInlineImageScaled(v.ImageProto, b.Data, b.MimeType, cells, maxRows); seq != "" { rows, actualCells := InlineImageFootprint(b.Data, cells, maxRows) if rows < 1 { rows = 1 } if actualCells < 1 { actualCells = cells } // Reserve the image footprint first, then place the escape // in the top-left of that blank rectangle. Keeping the // metadata above the image made some terminals blend text // and graphics layers during full-screen repaints; metadata // below the reserved rectangle is more stable and easier to // read. One extra blank row before the metadata gives the // caption breathing room from the image's last pixel row. // // Every footprint row (escape, reservations, gap) is tagged // with imageFootprintSentinel so callers wrapping content in // box edges can recognise these rows and emit them bare — // the image's graphics rectangle paints over them and any // │ character drawn alongside would visibly bleed through. // 4 leading cells push the escape past the box's left // edge plus a small interior gutter so the image rectangle // sits visibly inside the frame instead of kissing the │. // The escape row carries a width hint after the sentinel // ("\x1e\x1e...") so toolBoxSide knows how many // cells the image occupies and can pad to the right edge. widthHint := fmt.Sprintf("%s%d%s", imageFootprintSentinel, actualCells, imageFootprintSentinel) out := make([]string, 0, rows+3) out = append(out, widthHint+" "+seq) for i := 1; i < rows; i++ { out = append(out, imageFootprintSentinel) } out = append(out, imageFootprintSentinel) out = append(out, v.Theme.FGColor(v.Theme.Muted, info)) return out } } return []string{v.Theme.FGColor(v.Theme.Muted, info)} } // looksLikeNumberedFile returns true when text matches the `read` // tool's " N\tcontent" format for most of its lines. func looksLikeNumberedFile(text string) bool { lines := strings.Split(text, "\n") if len(lines) == 0 { return false } hits := 0 scanned := 0 for _, l := range lines { if l == "" { continue } scanned++ if scanned > 20 { break } if numberedLineRE.MatchString(l) { hits++ } } if scanned == 0 { return false } return hits*2 >= scanned // majority of non-empty lines match } var numberedLineRE = regexp.MustCompile(`^\s*\d+\t`) // renderNumberedFile strips line numbers, highlights the code, and // re-attaches the line numbers in muted color. func (v *View) renderNumberedFile(text, sourcePath string) []string { lines := strings.Split(text, "\n") gutters := make([]string, 0, len(lines)) codes := make([]string, 0, len(lines)) for _, l := range lines { idx := strings.IndexByte(l, '\t') if idx < 0 || !numberedLineRE.MatchString(l) { // Non-code footer (e.g. "[truncated at 2000 lines]"). gutters = append(gutters, "") codes = append(codes, sanitizeLine(l, terminalTabStop)) continue } gutter := sanitizeLine(l[:idx], 1) + " " // replace tab with single space code := sanitizeLine(l[idx+1:], codeTabStop) gutters = append(gutters, gutter) codes = append(codes, code) } lang := LanguageFromPath(sourcePath) var highlighted []string if lang != "" { highlighted = v.Theme.HighlightCode(strings.Join(codes, "\n"), lang) // Chroma sometimes collapses the trailing empty line. Pad to // align with the gutter slice so per-line zipping works. for len(highlighted) < len(codes) { highlighted = append(highlighted, "") } if len(highlighted) > len(codes) { highlighted = highlighted[:len(codes)] } } else { // No lexer for this file type — render in the ToolOut color so // code is visually distinct from the muted gutter. highlighted = make([]string, len(codes)) for i, c := range codes { highlighted[i] = v.Theme.FGColor(v.Theme.ToolOut, c) } } out := make([]string, 0, len(codes)) for i, code := range highlighted { g := gutters[i] if g == "" { out = append(out, " "+v.Theme.FGColor(v.Theme.Muted, code)) continue } out = append(out, " "+v.Theme.FGColor(v.Theme.Muted, g)+code) } return out } // looksLikeFileContent is a cheap guard to distinguish a read-tool // result from bash stdout or a status message. File content usually // contains characters that status messages don't (code punctuation, // longer lines, multiple lines) and rarely starts with the " >"- // or "error:"-style prefixes tools emit. False positives are OK, // the worst case is a line-number gutter on something that isn't // really code. // renderBashResult styles a bash tool result: the "$ command" first // line in the accent color, the trailing "[exit N] Took X.Ys" line // in muted type, everything else on the default tool-output color. // Called from renderToolText when the first line starts with "$ ". func (v *View) renderBashResult(lines []string, width int, defaultColor TerminalColor) []string { for i, l := range lines { lines[i] = sanitizeLine(l, terminalTabStop) } // Identify the footer line (exit + timing). The bash tool writes // it as the last non-empty line of the result. footerIdx := -1 for i := len(lines) - 1; i >= 0; i-- { trimmed := strings.TrimSpace(lines[i]) if trimmed == "" { continue } if strings.HasPrefix(trimmed, "[exit ") { footerIdx = i } break } var out []string for i, l := range lines { switch { case i == 0 && strings.HasPrefix(l, "$ "): // Style the $ and the command text in accent. No // further processing / wrapping so the shell-style // prompt reads at a glance. for _, w := range wrapLine(l, width-4, " ") { out = append(out, " "+v.Theme.FGColor(v.Theme.Accent, w)) } case i == footerIdx: for _, w := range wrapLine(l, width-4, " ") { out = append(out, " "+v.Theme.FGColor(v.Theme.Muted, w)) } default: for _, w := range wrapLine(l, width-4, " ") { out = append(out, " "+v.Theme.FGColor(defaultColor, w)) } } } return out } // mutatedFilesSuffix labels a tool result that changed several files, so a // project-wide rewrite shows its scope. Single-file edits stay unlabeled. func mutatedFilesSuffix(paths []string) string { if len(paths) < 2 { return "" } return fmt.Sprintf(" · %d files changed", len(paths)) } // astRewriteArgs reports whether a completed AST call requested mutation. func astRewriteArgs(name string, raw json.RawMessage) bool { if !strings.EqualFold(name, "ast") { return false } var args struct { Rewrite string `json:"rewrite"` } return json.Unmarshal(raw, &args) == nil && args.Rewrite != "" } // looksLikeUnifiedDiff reports whether text is a context diff as // emitted by edit and ast. Every non-empty row must start with a diff // marker, except for the bounded-output truncation notice. The presence // of at least one '+' or '-' row distinguishes a diff from ordinary text. func looksLikeUnifiedDiff(text string) bool { lines := strings.Split(text, "\n") if len(lines) < 2 { return false } sawChange := false for _, l := range lines { if l == "" { continue } if l == "..." { continue } switch l[0] { case '+', '-': sawChange = true case ' ': // context, ok default: return false } } return sawChange } // renderUnifiedDiff renders the edit tool's context diff. Each // kept row shows a line-number gutter plus a marker column: '+' // for additions (colored like add), '-' for deletions (colored // like remove), and unmarked context in muted type. A literal // "..." line between hunks renders as an ellipsis in muted type, // indicating skipped unchanged rows. The old and new line // counters advance so each row carries its actual position in // the pre- or post-edit file. Heuristic: when we hit a "...", we // can't know where the next hunk starts, so we don't reset the // counters — they stay approximate in the rare multi-hunk case. func (v *View) renderUnifiedDiff(text string, width int, sourcePath string) []string { lines := strings.Split(text, "\n") if n := len(lines); n > 0 && lines[n-1] == "" { lines = lines[:n-1] } oldLine, newLine := 1, 1 var out []string for _, l := range lines { if l == "" { out = append(out, "") continue } if l == "..." { out = append(out, " "+v.Theme.FGColor(v.Theme.Muted, "...")) continue } switch l[0] { case '+': out = append(out, v.renderDiffRow(l, width, v.Theme.Tool, newLine, '+', sourcePath)...) newLine++ case '-': out = append(out, v.renderDiffRow(l, width, v.Theme.Error, oldLine, '-', sourcePath)...) oldLine++ case ' ': out = append(out, v.renderDiffRow(l, width, v.Theme.Muted, newLine, ' ', sourcePath)...) oldLine++ newLine++ default: for _, w := range wrapLine(sanitizeLine(l, terminalTabStop), width-4, " ") { out = append(out, " "+v.Theme.FGColor(v.Theme.Muted, w)) } } } return out } func looksLikeFileContent(text string) bool { if strings.TrimSpace(text) == "" { return false } lines := strings.Split(text, "\n") return len(lines) >= 2 } // renderRawFile renders file content received without embedded line // numbers (the current read-tool output). Draws a muted gutter like // "%6d \t" starting at startLine, highlights the code using the // source path's language, and returns the formatted lines. func (v *View) renderRawFile(text, sourcePath string, startLine int) []string { lines := strings.Split(text, "\n") // Drop the trailing empty line that Split produces when text ends // in "\n" so the gutter doesn't show a phantom last number. if n := len(lines); n > 0 && lines[n-1] == "" { lines = lines[:n-1] } // Split code from trailing footer lines ("... [truncated ...]") // so we don't number the footer. A single blank separator line // immediately before the footer is also pulled out so the // gutter doesn't render a phantom number for it. codeEnd := len(lines) for i := len(lines) - 1; i >= 0; i-- { if strings.HasPrefix(lines[i], "...") { codeEnd = i continue } break } if codeEnd > 0 && codeEnd < len(lines) && lines[codeEnd-1] == "" { codeEnd-- } code := lines[:codeEnd] footer := lines[codeEnd:] for i, c := range code { code[i] = sanitizeLine(c, codeTabStop) } for i, f := range footer { footer[i] = sanitizeLine(f, terminalTabStop) } lang := LanguageFromPath(sourcePath) var highlighted []string if lang != "" { highlighted = v.Theme.HighlightCode(strings.Join(code, "\n"), lang) for len(highlighted) < len(code) { highlighted = append(highlighted, "") } if len(highlighted) > len(code) { highlighted = highlighted[:len(code)] } } else { highlighted = make([]string, len(code)) for i, c := range code { highlighted[i] = v.Theme.FGColor(v.Theme.ToolOut, c) } } out := make([]string, 0, len(lines)) for i, c := range highlighted { gutter := fmt.Sprintf("%4d ", startLine+i) out = append(out, " "+v.Theme.FGColor(v.Theme.Muted, gutter)+c) } for _, f := range footer { out = append(out, " "+v.Theme.FGColor(v.Theme.Muted, f)) } return out } // defaultToolArgWidth is the number of cells the tool-call header // truncates the primary argument (path/command/query) to when the // ZUT_TOOL_ARG_WIDTH environment variable is unset or invalid. const defaultToolArgWidth = 60 // toolArgWidth returns the maximum cell width for the truncated // primary argument shown in a tool-call header. It defaults to // defaultToolArgWidth but can be raised or lowered with the // ZUT_TOOL_ARG_WIDTH environment variable, which is useful for wide // terminals where the default clips long queries too aggressively. // Values outside the sane range [20, 500] are ignored so a stray or // malformed setting can never produce an unreadable header or an // out-of-range slice. func toolArgWidth() int { v := strings.TrimSpace(os.Getenv("ZUT_TOOL_ARG_WIDTH")) if v == "" { return defaultToolArgWidth } n, err := strconv.Atoi(v) if err != nil || n < 20 || n > 500 { return defaultToolArgWidth } return n } // ShortArgs renders a tool call's arguments into a one-line // suffix for the "tool name " header. tool is the tool // name so we can add shape-specific decorations: for read we // append the requested line range (e.g. "path:1-200") pulled // from the offset/limit args, and for glob we prefer the pattern // with an " in " suffix. Other tools keep the legacy // "path, file_path, or command, truncated" shape. // // The truncation width defaults to 60 cells but can be tuned via // the ZUT_TOOL_ARG_WIDTH environment variable (see toolArgWidth). // // Exported because the interactive mode pre-populates the // ToolCallView.Args field with this value as soon as the tool // call is announced, so the live overlay's header matches what // the finalised transcript will later render. func ShortArgs(tool string, raw json.RawMessage) string { width := toolArgWidth() var v any if err := json.Unmarshal(raw, &v); err != nil { return "" } x, ok := v.(map[string]any) if !ok { b, _ := json.Marshal(v) s := oneLineToolLabel(string(b)) if len(s) > width { s = s[:width-3] + "..." } return s } if tool == "ask_user_question" { if qs, ok := x["questions"].([]any); ok { return fmt.Sprintf("· %d questions", len(qs)) } } var primary string // Glob leads with its pattern; every other tool keeps the legacy // path-first shape so tools carrying both (e.g. ast's pattern+path) // still label with the file path. keys := []string{"path", "file_path", "command"} if strings.EqualFold(tool, "glob") { keys = []string{"pattern", "path", "file_path", "command"} } for _, k := range keys { if s, ok := x[k].(string); ok { primary = s break } } if primary == "" { b, _ := json.Marshal(v) s := oneLineToolLabel(string(b)) if len(s) > width { s = s[:width-3] + "..." } return s } primary = oneLineToolLabel(primary) // Tool-specific decoration. suffix := "" switch strings.ToLower(tool) { case "read": start := 1 if n, ok := toInt(x["offset"]); ok && n >= 1 { start = n } if lim, ok := toInt(x["limit"]); ok && lim > 0 { end := start + lim - 1 suffix = fmt.Sprintf(":%d-%d", start, end) } else if start > 1 { suffix = fmt.Sprintf(":%d-", start) } case "glob": if dir, ok := x["path"].(string); ok && dir != "" && dir != "." { suffix = " in " + dir } } // Truncate the primary arg leaving room for the suffix so the // range stays visible even on absurdly long paths. max := width - len(suffix) if max < 10 { max = 10 } if len(primary) > max { primary = primary[:max-3] + "..." } return primary + suffix } // toInt coerces a json.Unmarshal'd number (float64) or a string // containing a number into an int. Returns ok=false if the value // is neither. Used by shortArgs to survive model quirks where // numeric args come back as strings. func toInt(v any) (int, bool) { switch n := v.(type) { case float64: return int(n), true case int: return n, true case string: i, err := strconv.Atoi(strings.TrimSpace(n)) if err != nil { return 0, false } return i, true } return 0, false } // renderCompactionBlock renders a compaction summary as a distinct // visual block in the chat. When collapsed it shows a one-line label // with the pre-compaction token count; when expanded (ctrl+o) it // shows the full summary text. func (v *View) renderCompactionBlock(m provider.Message, width int) []string { th := v.Theme const indent = " " tokens := m.Meta["tokens_before"] if tokens == "" { tokens = "?" } if v.ExpandAll { var lines []string header := th.FGColor(th.Muted, fmt.Sprintf("compacted from ~%s tokens", tokens)) lines = append(lines, indent+header) lines = append(lines, "") for _, c := range m.Content { if tb, ok := c.(provider.TextBlock); ok { text := tb.Text if idx := strings.Index(text, "\n\n"); idx >= 0 && strings.HasPrefix(text, "## Context Summary") { text = text[idx+2:] } md := RenderMarkdown(text, th, width-4) for _, l := range strings.Split(md, "\n") { if len(l) > 0 && l[0] == FlushLeftSentinel { l = l[1:] } lines = append(lines, indent+l) } } } return lines } // Collapsed: single line, no banner. line := th.FGColor(th.Muted, fmt.Sprintf("compacted from ~%s tokens (ctrl+o to expand)", tokens)) return []string{indent + line} } // StatusBarParams groups the many bits of state the status bar needs. // Grew from a flat argument list once we settled on the layout. type StatusBarParams struct { Theme Theme Provider string Model string Reasoning string // "" means thinking off ModelProfile int // 1–9 identifies the active model/reasoning favorite FastMode bool // show the provider's opt-in fast tier when enabled Busy bool BusyPrefix string // spinner + funny line when busy CWD string Locked bool // sandbox on? NoYolo bool // confirmation mode enabled? GoalStatus string // autonomous goal lifecycle, empty when none PlanCurrent int // one-based in-progress plan step; 0 hides plan progress PlanTotal int // Cumulative session usage and cost. Usage provider.Usage // Subscription is true when the credential is an OAuth token (claude // pro/max, chatgpt plus/pro) rather than a paid api key. We still // compute an API-equivalent cost for visibility, not an actual charge. Subscription bool // WeeklyUsage is the account's remaining weekly allowance, already // formatted by the caller (empty for providers without this meter). WeeklyUsage string // Last turn's input+cache tokens (approximates current live context). ContextUsed int ContextMax int // model's context window; 0 disables the percentage // AutoCompacting is true when the agent is currently running a // model-triggered condense pass. Surfaces as "(auto)" after the // context percentage so it's clear where the spinner is coming from. AutoCompacting bool // Telegram true when the telegram bridge is connected. Adds a // small "- tg -" tag to the cwd line so the user can tell at a // glance that dms are being mirrored into this session. Telegram bool Cols int // terminal width; drives status wrapping } // StatusBar builds the status shown above the editor. The cwd stays on its // own final line, indented to match the stats column. Narrow terminals split // model and usage onto additional lines rather than hard-wrapping metrics. // // Layout: // // model:reasoning stats <- line 1 // cwd <- line 2 (2-space indent) // // The old "ctrl+c exit - /help" / "esc cancel" hint is gone entirely. // The slash-command popup and the queued/sliding-in chips already // cover the discoverability of those keybindings. func StatusBar(p StatusBarParams) []string { th := p.Theme stats := UsageStatsParts(UsageStatsParams{Usage: p.Usage, Subscription: p.Subscription, Compact: true}) for idx, stat := range stats { stats[idx] = th.FGColor(th.Muted, stat) } if p.WeeklyUsage != "" { if label, value, ok := strings.Cut(p.WeeklyUsage, ":"); ok { stats = append(stats, statusLabelValue(th, label+":", value, th.FG)) } else { stats = append(stats, th.FGColor(th.Muted, p.WeeklyUsage)) } } if p.GoalStatus != "" { stats = append(stats, statusLabelValue(th, "goal:", p.GoalStatus, th.FG)) } if p.PlanCurrent > 0 && p.PlanTotal > 0 { stats = append(stats, statusLabelValue(th, "plan:", fmt.Sprintf("%d/%d", p.PlanCurrent, p.PlanTotal), th.FG)) } // Context %. Color-coded: yellow >70, red >90. ctx, ctxColor := contextUsage(th, p.ContextUsed, p.ContextMax) if ctx != "" { if ctxColor == th.Muted { ctxColor = th.FG } value := ctx suffix := "" if p.ContextMax > 0 { value = fmt.Sprintf("%.0f%%", float64(p.ContextUsed)/float64(p.ContextMax)*100) suffix = "/" + formatTokens(p.ContextMax) } if p.AutoCompacting { suffix += " (auto)" } stats = append(stats, statusLabelValueSuffix(th, "ctx", value, suffix, ctxColor)) } // Layout uses exactly 2 spaces of horizontal padding everywhere: // 2 spaces gpt-5.4:medium $0.00 ctx0%/400k // matches the editor prompt's left inset so the bar lines up // vertically with the conversation column. const pad = " " // 2 spaces reasoning := reasoningLevelLabel(p.Reasoning) modelLabel := p.Model if p.ModelProfile >= 1 && p.ModelProfile <= 9 { modelLabel = fmt.Sprintf("[%d] %s", p.ModelProfile, modelLabel) } left := renderStatusModel(th, modelLabel, reasoning) fastText := "" if p.FastMode { fastText = th.FGColor(th.Muted, "fast mode") } statsText := strings.Join(stats, " ") middleParts := make([]string, 0, 3) if fastText != "" { middleParts = append(middleParts, fastText) } if statsText != "" { middleParts = append(middleParts, statsText) } middle := strings.Join(middleParts, " ") var leftBuilder strings.Builder if p.BusyPrefix != "" { // Don't re-color the payload itself — the caller already set // fg colors on the spinner glyph, message, and elapsed // segments. Wrapping the whole thing here would override // those choices. The pad itself needs no color (it's spaces). leftBuilder.WriteString(pad + p.BusyPrefix) // Exactly one pad (2 spaces) between the busy segment and // the model block. The leading pad above covers // the left indent. leftBuilder.WriteString(pad) } else { // Idle path: a single pad of left inset so the line // aligns with the conversation column on its left edge // (" you" / " zut" message markers). Without the busy // prefix there's no trailing separator to double-pad. leftBuilder.WriteString(pad) } leftBuilder.WriteString(left) if middle != "" { leftBuilder.WriteString(pad) leftBuilder.WriteString(middle) } cwd := shortenHome(p.CWD) tags := "" if p.NoYolo { tags += "yolo mode disabled " } if p.Locked { tags += "jailed " } if p.Telegram { tags += "telegram connected " } if tags != "" && cwd != "" { cwd = tags + "- " + cwd } primary := leftBuilder.String() // On narrow terminals the single line wraps badly. If the visible // width exceeds cols and we have a busy prefix, split: keep the // busy prefix on line 1, then put model and (if still needed) // stats on their own rows. This mirrors the idle split below. if p.Cols > 0 && p.BusyPrefix != "" && visibleWidth(primary) > p.Cols { busyLine := pad + p.BusyPrefix modelLine := pad + left lines := []string{busyLine} if visibleWidth(modelLine+pad+middle) > p.Cols { lines = appendWrappedStatusLines(lines, th, pad, modelLabel, reasoning, fastText, stats, p.Cols) } else { var infoBuilder strings.Builder infoBuilder.WriteString(modelLine) if middle != "" { infoBuilder.WriteString(pad) infoBuilder.WriteString(middle) } lines = append(lines, infoBuilder.String()) } if cwd != "" { lines = append(lines, pad+th.FGColor(th.Muted, cwd)) } return lines } // Idle narrow split: keep model/reasoning on the first status line, // move usage/cost/context stats to the next, then cwd below. This // avoids the terminal's hard wrap cutting the stats or pushing cwd // into an awkward position on small widths. if p.Cols > 0 && p.BusyPrefix == "" && visibleWidth(primary) > p.Cols { var lines []string lines = appendWrappedStatusLines(lines, th, pad, modelLabel, reasoning, fastText, stats, p.Cols) if cwd != "" { lines = append(lines, pad+th.FGColor(th.Muted, cwd)) } return lines } if cwd == "" { return []string{primary} } // Second line: indent with the same 2-space pad so the cwd lines // up under the model column on line 1. cwdRendered := pad + th.FGColor(th.Muted, cwd) return []string{primary, cwdRendered} } func statusLabelValue(th Theme, label, value string, valueColor TerminalColor) string { return statusLabelValueSuffix(th, label, value, "", valueColor) } func statusLabelValueSuffix(th Theme, label, value, suffix string, valueColor TerminalColor) string { var rendered strings.Builder if label != "" { rendered.WriteString(th.FGColor(th.Muted, label)) } if value != "" { rendered.WriteString(th.FGColor(valueColor, value)) } if suffix != "" { rendered.WriteString(th.FGColor(th.Muted, suffix)) } return rendered.String() } func renderStatusModel(th Theme, model, reasoning string) string { rendered := th.FGColor(th.Muted, model) if reasoning == "" { return rendered } valueColor := th.FG if reasoning == "max" { valueColor = th.ThinkingMax } return rendered + statusLabelValue(th, ":", reasoning, valueColor) } func appendWrappedStatusLines(lines []string, th Theme, pad, model, reasoning, fastText string, stats []string, cols int) []string { modelLine := pad + renderStatusModel(th, model, reasoning) if fastText != "" && visibleWidth(modelLine+pad+fastText) <= cols { modelLine += pad + fastText } else if fastText != "" { stats = append([]string{fastText}, stats...) } lines = append(lines, truncateToWidth(modelLine, cols)) return appendWrappedUsageStats(lines, th, pad, stats, cols) } func appendWrappedUsageStats(lines []string, th Theme, pad string, stats []string, cols int) []string { if len(stats) == 0 { return lines } available := cols - visibleWidth(pad) if cols <= 0 || available <= 0 { return append(lines, pad+strings.Join(stats, " ")) } current := make([]string, 0, len(stats)) flush := func() { if len(current) == 0 { return } line := pad + strings.Join(current, " ") lines = append(lines, truncateToWidth(line, cols)) current = current[:0] } for _, stat := range stats { candidate := append(append([]string(nil), current...), stat) if len(current) > 0 && visibleWidth(strings.Join(candidate, " ")) > available { flush() } current = append(current, stat) } flush() return lines } func reasoningLevelLabel(level string) string { switch strings.ToLower(strings.TrimSpace(level)) { case "", "off", "none", "no", "false", "disabled": return "" case "minimum", "minimal", "min": return "minimal" case "maximum", "xhigh": return "xhigh" case "max": return "max" default: return strings.ToLower(strings.TrimSpace(level)) } } // contextUsage renders the "N%/ctxMax" fragment, returning the // rendered string plus the colour to wrap it in. func contextUsage(th Theme, used, max int) (string, TerminalColor) { if max <= 0 { if used <= 0 { return "", th.Muted } return formatTokens(used), th.Muted } pct := float64(used) / float64(max) * 100 text := fmt.Sprintf("%.1f%%/%s", pct, formatTokens(max)) switch { case pct > 90: return text, th.Error case pct > 70: return text, th.Warning } return text, th.Muted } // formatTokens footer formatter: // // < 1000 -> "42" // < 10_000 -> "2.7k" // < 1_000_000 -> "35k" // < 10M -> "1.1M" // else -> "12M" func formatTokens(n int) string { switch { case n < 0: return "0" case n < 1000: return fmt.Sprintf("%d", n) case n < 10000: return fmt.Sprintf("%.1fk", float64(n)/1000) case n < 1_000_000: return fmt.Sprintf("%dk", (n+500)/1000) case n < 10_000_000: return fmt.Sprintf("%.1fM", float64(n)/1_000_000) default: return fmt.Sprintf("%dM", (n+500_000)/1_000_000) } } // shortenHome replaces the user's $HOME prefix with "~" for readability. func shortenHome(p string) string { if p == "" { return "" } home := osUserHome() if home == "" { return p } if p == home { return "~" } if strings.HasPrefix(p, home+"/") { return "~" + p[len(home):] } return p } // osUserHome is a tiny wrapper around os.UserHomeDir so tests can mock it. var osUserHome = func() string { if h, err := osUserHomeDir(); err == nil { return h } return "" }