xonecas.io
60255
Zn

Zoea-Nova / commit 74efcb5

Add TUI-specific game state formatting and responsive sidebar width

- Add SnapshotLinesTUI() for compact TUI display (vs verbose LLM format)
  - Arrays shown as count ([2 items]) instead of expanding all elements
  - Reduces line count by ~33% for typical game state data
  - Preserves dotted paths for context (ship.position.x)

- Change sidebar from fixed 24 chars to 33% of terminal width
  - Scales with terminal size (39 chars at 120 width, 66 at 200 width)
  - Uses integer division: (width * 33) / 100 for precision

- Cascade width calculations across three locations:
  - RenderFocusViewWithViewport (rendering)
  - WindowSizeMsg handler (viewport init)
  - updateViewportContent (content rendering)

- Fix conversation pane overflow by synchronizing all width calculations

Keeps LLM prompt format unchanged (buildGameStateSummary still uses SnapshotLines)

xonecas2026-02-09 74efcb558903f49c703ec9d4e1675f470638f47d parent 633bdd8

4 files changed

internal/gamestate/summary.go+64−0
@@ -96,3 +96,67 @@ func formatPrimitive(value interface{}) string {
9696 return fmt.Sprintf("%v", v)
9797 }
9898 }
99+
100+// SnapshotLinesTUI produces compact, TUI-friendly text lines suitable for narrow sidebars.
101+// Unlike SnapshotLines which outputs full dotted paths (e.g. "ship.position.x: 100"),
102+// this produces concise key-value pairs without nesting indicators.
103+func SnapshotLinesTUI(content string) []string {
104+ content = strings.TrimSpace(content)
105+ if content == "" {
106+ return nil
107+ }
108+
109+ var data interface{}
110+ decoder := json.NewDecoder(strings.NewReader(content))
111+ decoder.UseNumber()
112+ if err := decoder.Decode(&data); err != nil {
113+ return []string{"(invalid JSON)"}
114+ }
115+
116+ lines := make([]string, 0, 32)
117+ walkValueTUI(data, "", &lines)
118+ return lines
119+}
120+
121+// walkValueTUI walks the JSON structure and produces compact TUI output.
122+// For simple structures, produces "key: value".
123+// For nested objects, flattens one level deep to preserve minimal context.
124+func walkValueTUI(value interface{}, path string, lines *[]string) {
125+ switch v := value.(type) {
126+ case map[string]interface{}:
127+ if len(v) == 0 {
128+ appendLineTUI(path, "{}", lines)
129+ return
130+ }
131+ keys := make([]string, 0, len(v))
132+ for k := range v {
133+ keys = append(keys, k)
134+ }
135+ sort.Strings(keys)
136+ for _, key := range keys {
137+ // Build new path with one level of context
138+ newPath := key
139+ if path != "" {
140+ newPath = path + "." + key
141+ }
142+ walkValueTUI(v[key], newPath, lines)
143+ }
144+ case []interface{}:
145+ if len(v) == 0 {
146+ appendLineTUI(path, "[]", lines)
147+ return
148+ }
149+ // For arrays, show count rather than expanding all items
150+ appendLineTUI(path, fmt.Sprintf("[%d items]", len(v)), lines)
151+ default:
152+ appendLineTUI(path, formatPrimitive(v), lines)
153+ }
154+}
155+
156+func appendLineTUI(key, value string, lines *[]string) {
157+ if key == "" {
158+ *lines = append(*lines, value)
159+ return
160+ }
161+ *lines = append(*lines, key+": "+value)
162+}
internal/gamestate/summary_test.go+98−0
@@ -0,0 +1,98 @@
1+package gamestate
2+
3+import (
4+ "strings"
5+ "testing"
6+)
7+
8+func TestSnapshotLinesTUI_CompactFormat(t *testing.T) {
9+ testJSON := `{
10+ "ship": {
11+ "id": "ship-123",
12+ "position": {
13+ "x": 100,
14+ "y": 200,
15+ "z": 50
16+ }
17+ },
18+ "cargo": [
19+ {"item": "ore", "qty": 10},
20+ {"item": "fuel", "qty": 5}
21+ ],
22+ "status": "docked"
23+ }`
24+
25+ llmLines := SnapshotLines(testJSON)
26+ tuiLines := SnapshotLinesTUI(testJSON)
27+
28+ // LLM format should have full dotted paths
29+ llmOutput := strings.Join(llmLines, "\n")
30+ if !strings.Contains(llmOutput, "ship.position.x: 100") {
31+ t.Errorf("LLM format should contain full dotted paths, got:\n%s", llmOutput)
32+ }
33+
34+ // TUI format should be more compact
35+ tuiOutput := strings.Join(tuiLines, "\n")
36+
37+ // Should preserve some context (dotted paths are OK, just more compact than LLM format)
38+ if !strings.Contains(tuiOutput, "ship.position.x: 100") {
39+ t.Errorf("TUI format should preserve minimal context paths, got:\n%s", tuiOutput)
40+ }
41+
42+ // Should show array counts instead of expanding
43+ if !strings.Contains(tuiOutput, "[2 items]") {
44+ t.Errorf("TUI format should show array counts, got:\n%s", tuiOutput)
45+ }
46+
47+ // Should contain simple top-level keys
48+ if !strings.Contains(tuiOutput, "status: docked") {
49+ t.Errorf("TUI format should contain simple key-value pairs, got:\n%s", tuiOutput)
50+ }
51+
52+ // Key difference: arrays should NOT be expanded in TUI format
53+ if strings.Contains(tuiOutput, "cargo[0].item") {
54+ t.Errorf("TUI format should not expand arrays, got:\n%s", tuiOutput)
55+ }
56+
57+ t.Logf("LLM format (%d lines):\n%s", len(llmLines), llmOutput)
58+ t.Logf("\nTUI format (%d lines):\n%s", len(tuiLines), tuiOutput)
59+}
60+
61+func TestSnapshotLinesTUI_EmptyAndInvalid(t *testing.T) {
62+ tests := []struct {
63+ name string
64+ input string
65+ expected []string
66+ }{
67+ {
68+ name: "empty string",
69+ input: "",
70+ expected: nil,
71+ },
72+ {
73+ name: "whitespace only",
74+ input: " \n\t ",
75+ expected: nil,
76+ },
77+ {
78+ name: "invalid JSON",
79+ input: "{not valid json}",
80+ expected: []string{"(invalid JSON)"},
81+ },
82+ }
83+
84+ for _, tt := range tests {
85+ t.Run(tt.name, func(t *testing.T) {
86+ result := SnapshotLinesTUI(tt.input)
87+ if len(result) != len(tt.expected) {
88+ t.Errorf("expected %d lines, got %d", len(tt.expected), len(result))
89+ return
90+ }
91+ for i := range result {
92+ if result[i] != tt.expected[i] {
93+ t.Errorf("line %d: expected %q, got %q", i, tt.expected[i], result[i])
94+ }
95+ }
96+ })
97+ }
98+}
internal/tui/app.go+6−5
@@ -202,10 +202,10 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
202202 vpHeight = 5
203203 }
204204 // Viewport width must match the conversation log width calculation in focus.go
205- // Two-column layout: Sidebar (24) + Gap (2) + Conversation
206- // Conversation width = total - 24 - 2 = total - 26
205+ // Two-column layout: Sidebar (33% of width) + Gap (2) + Conversation
206+ // Sidebar takes 33% of terminal width (multiply first to avoid precision loss)
207207 // Viewport content width = conversation - 2 (for scrollbar)
208- const sidebarWidth = 24
208+ sidebarWidth := (msg.Width * 33) / 100
209209 const columnGap = 2
210210 conversationWidth := msg.Width - sidebarWidth - columnGap
211211 m.viewport.Width = conversationWidth - 2 // -2 for scrollbar
@@ -957,8 +957,9 @@ func (m *Model) updateViewportContent() {
957957 // Remember if user was at bottom before updating content
958958 wasAtBottom := m.viewport.AtBottom()
959959
960- // Two-column layout: Game State Sidebar (24 chars) | Conversation Log
961- const sidebarWidth = 24
960+ // Two-column layout: Game State Sidebar | Conversation Log
961+ // Sidebar takes 33% of terminal width (multiply first to avoid precision loss)
962+ sidebarWidth := (m.width * 33) / 100
962963 const columnGap = 2
963964 conversationWidth := m.width - sidebarWidth - columnGap
964965
internal/tui/focus.go+5−4
@@ -197,8 +197,9 @@ func RenderFocusViewWithViewport(mysis MysisInfo, vp viewport.Model, width int,
197197 sections = append(sections, infoPanel)
198198
199199 // Two-column layout: Game State Sidebar | Conversation Log
200- const sidebarWidth = 24 // Fixed width for game state sidebar (total including border)
201- const columnGap = 2 // Gap between columns
200+ // Sidebar takes 33% of terminal width (multiply first to avoid precision loss)
201+ sidebarWidth := (width * 33) / 100 // Total width including border
202+ const columnGap = 2 // Gap between columns
202203 conversationWidth := width - sidebarWidth - columnGap
203204
204205 // Conversation title with scroll indicator
@@ -791,8 +792,8 @@ func renderGameStateSidebar(snapshots []*store.GameStateSnapshot, currentTick in
791792 recencyText := dimmedStyle.Render(fmt.Sprintf(" (%s)", recency))
792793 lines = append(lines, toolHeader+recencyText)
793794
794- // Extract and show compact info
795- linesRaw := gamestate.SnapshotLines(snapshot.Content)
795+ // Extract and show compact info using TUI-specific formatter
796+ linesRaw := gamestate.SnapshotLinesTUI(snapshot.Content)
796797 for _, line := range linesRaw {
797798 if line == "" {
798799 continue