xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / gamestate / summary.go

3.7 KB
1package gamestate
2
3import (
4	"encoding/json"
5	"fmt"
6	"sort"
7	"strconv"
8	"strings"
9)
10
11func SnapshotLines(content string) []string {
12	content = strings.TrimSpace(content)
13	if content == "" {
14		return nil
15	}
16
17	var data interface{}
18	decoder := json.NewDecoder(strings.NewReader(content))
19	decoder.UseNumber()
20	if err := decoder.Decode(&data); err != nil {
21		return []string{"(invalid JSON)"}
22	}
23
24	lines := make([]string, 0, 64)
25	walkValue(data, "", &lines)
26	return lines
27}
28
29func SnapshotString(content string) string {
30	lines := SnapshotLines(content)
31	return strings.Join(lines, "\n")
32}
33
34func walkValue(value interface{}, path string, lines *[]string) {
35	switch v := value.(type) {
36	case map[string]interface{}:
37		if len(v) == 0 {
38			appendLine(path, "{}", lines)
39			return
40		}
41		keys := make([]string, 0, len(v))
42		for k := range v {
43			keys = append(keys, k)
44		}
45		sort.Strings(keys)
46		for _, key := range keys {
47			walkValue(v[key], joinPath(path, key), lines)
48		}
49	case []interface{}:
50		if len(v) == 0 {
51			appendLine(path, "[]", lines)
52			return
53		}
54		for i, item := range v {
55			walkValue(item, fmt.Sprintf("%s[%d]", path, i), lines)
56		}
57	default:
58		appendLine(path, formatPrimitive(v), lines)
59	}
60}
61
62func appendLine(path, value string, lines *[]string) {
63	if path == "" {
64		*lines = append(*lines, value)
65		return
66	}
67	*lines = append(*lines, path+": "+value)
68}
69
70func joinPath(prefix, key string) string {
71	if prefix == "" {
72		return key
73	}
74	return prefix + "." + key
75}
76
77func formatPrimitive(value interface{}) string {
78	switch v := value.(type) {
79	case nil:
80		return "null"
81	case json.Number:
82		return v.String()
83	case float64:
84		return strconv.FormatFloat(v, 'f', -1, 64)
85	case float32:
86		return strconv.FormatFloat(float64(v), 'f', -1, 32)
87	case int, int8, int16, int32, int64:
88		return fmt.Sprintf("%d", v)
89	case uint, uint8, uint16, uint32, uint64:
90		return fmt.Sprintf("%d", v)
91	case bool:
92		return strconv.FormatBool(v)
93	case string:
94		return v
95	default:
96		return fmt.Sprintf("%v", v)
97	}
98}
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.
103func 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.
124func 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
156func appendLineTUI(key, value string, lines *[]string) {
157	if key == "" {
158		*lines = append(*lines, value)
159		return
160	}
161	*lines = append(*lines, key+": "+value)
162}