xonecas.io
60255
Zn

Zoea-Nova / commit a6e5d7f

fix(tui): prevent JSON tree overflow by constraining line width

- Add maxWidth parameter to renderJSONTree and all render functions
- Truncate long JSON values to fit within available width
- Pass contentWidth from renderLogEntryImpl to JSON tree renderer
- Add test for long value truncation
- Fixes overflow issue where JSON lines exceeded viewport width

xonecas2026-02-05 a6e5d7f668f0434071863355adb89c91d8e41241 parent 564c71b

3 files changed

internal/tui/focus.go+2−2
@@ -262,8 +262,8 @@ func renderLogEntryImpl(entry LogEntry, maxWidth int, verbose bool) []string {
262262 // Detect JSON content in tool messages and render as tree
263263 var wrappedLines []string
264264 if entry.Role == "tool" && isJSON(entry.Content) {
265- // Render JSON as tree structure
266- treeStr, err := renderJSONTree(entry.Content, verbose)
265+ // Render JSON as tree structure with width constraint
266+ treeStr, err := renderJSONTree(entry.Content, verbose, contentWidth)
267267 if err == nil {
268268 // Tree rendering succeeded
269269 wrappedLines = strings.Split(treeStr, "\n")
internal/tui/json_tree.go+54−15
@@ -24,32 +24,45 @@ const (
2424 // renderJSONTree renders JSON as a Unicode tree structure with smart truncation.
2525 // If verbose is true, all items are shown. Otherwise, arrays with more than 6 items
2626 // show first 3, "[x more]", and last 3.
27-func renderJSONTree(jsonStr string, verbose bool) (string, error) {
27+// maxWidth constrains the width of rendered lines (0 = no limit).
28+func renderJSONTree(jsonStr string, verbose bool, maxWidth int) (string, error) {
2829 var data interface{}
2930 if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
3031 return "", fmt.Errorf("invalid JSON: %w", err)
3132 }
3233
3334 var lines []string
34- renderValue(data, "", true, verbose, &lines)
35+ renderValue(data, "", true, verbose, maxWidth, &lines)
3536 return strings.Join(lines, "\n"), nil
3637 }
3738
3839 // renderValue recursively renders a JSON value as tree lines
39-func renderValue(value interface{}, prefix string, isLast bool, verbose bool, lines *[]string) {
40+func renderValue(value interface{}, prefix string, isLast bool, verbose bool, maxWidth int, lines *[]string) {
4041 switch v := value.(type) {
4142 case map[string]interface{}:
42- renderObject(v, prefix, isLast, verbose, lines)
43+ renderObject(v, prefix, isLast, verbose, maxWidth, lines)
4344 case []interface{}:
44- renderArray(v, prefix, isLast, verbose, lines)
45+ renderArray(v, prefix, isLast, verbose, maxWidth, lines)
4546 default:
46- // Primitive value
47- *lines = append(*lines, prefix+fmt.Sprintf("%v", v))
47+ // Primitive value - wrap if needed
48+ valueStr := fmt.Sprintf("%v", v)
49+ if maxWidth > 0 {
50+ prefixWidth := lipgloss.Width(prefix)
51+ availableWidth := maxWidth - prefixWidth
52+ if availableWidth < 10 {
53+ availableWidth = 10
54+ }
55+ if lipgloss.Width(valueStr) > availableWidth {
56+ // Truncate long values
57+ valueStr = truncateToWidth(valueStr, availableWidth-3) + "..."
58+ }
59+ }
60+ *lines = append(*lines, prefix+valueStr)
4861 }
4962 }
5063
5164 // renderObject renders a JSON object
52-func renderObject(obj map[string]interface{}, prefix string, isLast bool, verbose bool, lines *[]string) {
65+func renderObject(obj map[string]interface{}, prefix string, isLast bool, verbose bool, maxWidth int, lines *[]string) {
5366 if len(obj) == 0 {
5467 *lines = append(*lines, prefix+"{}")
5568 return
@@ -87,12 +100,25 @@ func renderObject(obj map[string]interface{}, prefix string, isLast bool, verbos
87100 switch v := val.(type) {
88101 case map[string]interface{}:
89102 *lines = append(*lines, valuePrefix)
90- renderObject(v, fieldPrefix+treeSpace, isLastField, verbose, lines)
103+ renderObject(v, fieldPrefix+treeSpace, isLastField, verbose, maxWidth, lines)
91104 case []interface{}:
92105 *lines = append(*lines, valuePrefix)
93- renderArray(v, fieldPrefix+treeSpace, isLastField, verbose, lines)
106+ renderArray(v, fieldPrefix+treeSpace, isLastField, verbose, maxWidth, lines)
94107 default:
95- *lines = append(*lines, valuePrefix+fmt.Sprintf("%v", v))
108+ // Primitive value - wrap if needed
109+ valueStr := fmt.Sprintf("%v", v)
110+ if maxWidth > 0 {
111+ prefixWidth := lipgloss.Width(valuePrefix)
112+ availableWidth := maxWidth - prefixWidth
113+ if availableWidth < 10 {
114+ availableWidth = 10
115+ }
116+ if lipgloss.Width(valueStr) > availableWidth {
117+ // Truncate long values
118+ valueStr = truncateToWidth(valueStr, availableWidth-3) + "..."
119+ }
120+ }
121+ *lines = append(*lines, valuePrefix+valueStr)
96122 }
97123 }
98124
@@ -106,7 +132,7 @@ func renderObject(obj map[string]interface{}, prefix string, isLast bool, verbos
106132 }
107133
108134 // renderArray renders a JSON array with smart truncation
109-func renderArray(arr []interface{}, prefix string, isLast bool, verbose bool, lines *[]string) {
135+func renderArray(arr []interface{}, prefix string, isLast bool, verbose bool, maxWidth int, lines *[]string) {
110136 if len(arr) == 0 {
111137 *lines = append(*lines, prefix+"[]")
112138 return
@@ -176,12 +202,25 @@ func renderArray(arr []interface{}, prefix string, isLast bool, verbose bool, li
176202 switch v := val.(type) {
177203 case map[string]interface{}:
178204 *lines = append(*lines, itemLine)
179- renderObject(v, itemPrefix+treeSpace, isLastItem, verbose, lines)
205+ renderObject(v, itemPrefix+treeSpace, isLastItem, verbose, maxWidth, lines)
180206 case []interface{}:
181207 *lines = append(*lines, itemLine)
182- renderArray(v, itemPrefix+treeSpace, isLastItem, verbose, lines)
208+ renderArray(v, itemPrefix+treeSpace, isLastItem, verbose, maxWidth, lines)
183209 default:
184- *lines = append(*lines, itemLine+fmt.Sprintf("%v", v))
210+ // Primitive value - wrap if needed
211+ valueStr := fmt.Sprintf("%v", v)
212+ if maxWidth > 0 {
213+ prefixWidth := lipgloss.Width(itemLine)
214+ availableWidth := maxWidth - prefixWidth
215+ if availableWidth < 10 {
216+ availableWidth = 10
217+ }
218+ if lipgloss.Width(valueStr) > availableWidth {
219+ // Truncate long values
220+ valueStr = truncateToWidth(valueStr, availableWidth-3) + "..."
221+ }
222+ }
223+ *lines = append(*lines, itemLine+valueStr)
185224 }
186225 }
187226
internal/tui/json_tree_test.go+32−4
@@ -4,12 +4,14 @@ import (
44 "encoding/json"
55 "strings"
66 "testing"
7+
8+ "github.com/charmbracelet/lipgloss"
79 )
810
911 func TestRenderJSONTree_SimpleObject(t *testing.T) {
1012 jsonStr := `{"name": "mysis-1", "state": "running", "id": "abc123"}`
1113
12- tree, err := renderJSONTree(jsonStr, false)
14+ tree, err := renderJSONTree(jsonStr, false, 80)
1315 if err != nil {
1416 t.Fatalf("Failed to render JSON tree: %v", err)
1517 }
@@ -32,7 +34,7 @@ func TestRenderJSONTree_ArrayTruncation(t *testing.T) {
3234 }
3335 jsonBytes, _ := json.Marshal(items)
3436
35- tree, err := renderJSONTree(string(jsonBytes), false)
37+ tree, err := renderJSONTree(string(jsonBytes), false, 80)
3638 if err != nil {
3739 t.Fatalf("Failed to render JSON tree: %v", err)
3840 }
@@ -65,7 +67,7 @@ func TestRenderJSONTree_VerboseMode(t *testing.T) {
6567 }
6668 jsonBytes, _ := json.Marshal(items)
6769
68- tree, err := renderJSONTree(string(jsonBytes), true)
70+ tree, err := renderJSONTree(string(jsonBytes), true, 80)
6971 if err != nil {
7072 t.Fatalf("Failed to render JSON tree: %v", err)
7173 }
@@ -87,8 +89,34 @@ func TestRenderJSONTree_VerboseMode(t *testing.T) {
8789 func TestRenderJSONTree_InvalidJSON(t *testing.T) {
8890 jsonStr := `{invalid json`
8991
90- _, err := renderJSONTree(jsonStr, false)
92+ _, err := renderJSONTree(jsonStr, false, 80)
9193 if err == nil {
9294 t.Error("Expected error for invalid JSON")
9395 }
9496 }
97+
98+func TestRenderJSONTree_LongValueTruncation(t *testing.T) {
99+ // Create JSON with a very long string value
100+ longValue := strings.Repeat("a", 200)
101+ jsonStr := `{"id": "` + longValue + `", "name": "test"}`
102+
103+ maxWidth := 50
104+ tree, err := renderJSONTree(jsonStr, false, maxWidth)
105+ if err != nil {
106+ t.Fatalf("Failed to render JSON tree: %v", err)
107+ }
108+
109+ lines := strings.Split(tree, "\n")
110+ for i, line := range lines {
111+ // Use lipgloss.Width for proper Unicode display width
112+ width := lipgloss.Width(line)
113+ if width > maxWidth {
114+ t.Errorf("Line %d exceeds maxWidth %d: got %d display width\nLine: %s", i, maxWidth, width, stripANSI(line))
115+ }
116+ }
117+
118+ // Should contain truncation indicator
119+ if !strings.Contains(tree, "...") {
120+ t.Error("Expected truncation indicator '...' for long value")
121+ }
122+}