@@ -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