feat: truncate string values and add ellipsis#24
Conversation
Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>
|
No significant code changes identified for Unit Test Generation. Please Click here to retry. |
There was a problem hiding this comment.
Pull Request Overview
This PR adds intelligent string truncation functionality to improve readability of JSON diffs by preventing overwhelming console output when dealing with long string values. The changes implement ANSI color-aware truncation with ellipsis indicators.
- Introduces
truncateStringWithEllipsisfunction with ANSI escape sequence handling for colorized diff output - Updates diff comparison logic to truncate colorized string differences
- Adds fallback truncation for serialized values in
writeKeyValuePair
| if len(string(serializedValue)) > 30 { | ||
| formattedValue = string(serializedValue[:30]) + "..." |
There was a problem hiding this comment.
The magic number 30 should be defined as a named constant to improve maintainability and make the truncation length configurable.
| if len(string(serializedValue)) > 30 { | |
| formattedValue = string(serializedValue[:30]) + "..." | |
| if len(string(serializedValue)) > truncationLength { | |
| formattedValue = string(serializedValue[:truncationLength]) + "..." |
| if len(string(serializedValue)) > 30 { | ||
| formattedValue = string(serializedValue[:30]) + "..." |
There was a problem hiding this comment.
This byte-based truncation can break UTF-8 multi-byte characters, potentially causing invalid strings. Use utf8.ValidString() or implement rune-aware truncation similar to truncatePlain().
| if len(string(serializedValue)) > 30 { | |
| formattedValue = string(serializedValue[:30]) + "..." | |
| if utf8.RuneCountInString(formattedValue) > 30 { | |
| runes := []rune(formattedValue) | |
| formattedValue = string(runes[:30]) + "..." |
| out.WriteString(truncatePlain(val[prev:r.Start], 20, "...")) | ||
| out.WriteString(truncateANSISegment(val[r.Start:r.End], 20, colorEllipsis)) | ||
| prev = r.End | ||
| } | ||
| out.WriteString(truncatePlain(val[prev:], 20, "...")) |
There was a problem hiding this comment.
The magic number 20 is hardcoded in multiple places. Consider making this a configurable parameter or constant.
| out.WriteString(truncatePlain(val[prev:r.Start], 20, "...")) | |
| out.WriteString(truncateANSISegment(val[r.Start:r.End], 20, colorEllipsis)) | |
| prev = r.End | |
| } | |
| out.WriteString(truncatePlain(val[prev:], 20, "...")) | |
| out.WriteString(truncatePlain(val[prev:r.Start], truncateLength, "...")) | |
| out.WriteString(truncateANSISegment(val[r.Start:r.End], truncateLength, colorEllipsis)) | |
| prev = r.End | |
| } | |
| out.WriteString(truncatePlain(val[prev:], truncateLength, "...")) |
| out.WriteString(truncatePlain(val[prev:r.Start], 20, "...")) | ||
| out.WriteString(truncateANSISegment(val[r.Start:r.End], 20, colorEllipsis)) | ||
| prev = r.End | ||
| } | ||
| out.WriteString(truncatePlain(val[prev:], 20, "...")) |
There was a problem hiding this comment.
The magic number 20 is hardcoded in multiple places. Consider making this a configurable parameter or constant.
| out.WriteString(truncatePlain(val[prev:r.Start], 20, "...")) | |
| out.WriteString(truncateANSISegment(val[r.Start:r.End], 20, colorEllipsis)) | |
| prev = r.End | |
| } | |
| out.WriteString(truncatePlain(val[prev:], 20, "...")) | |
| out.WriteString(truncatePlain(val[prev:r.Start], truncationLength, "...")) | |
| out.WriteString(truncateANSISegment(val[r.Start:r.End], truncationLength, colorEllipsis)) | |
| prev = r.End | |
| } | |
| out.WriteString(truncatePlain(val[prev:], truncationLength, "...")) |
| out.WriteString(truncatePlain(val[prev:r.Start], 20, "...")) | ||
| out.WriteString(truncateANSISegment(val[r.Start:r.End], 20, colorEllipsis)) | ||
| prev = r.End | ||
| } | ||
| out.WriteString(truncatePlain(val[prev:], 20, "...")) |
There was a problem hiding this comment.
The magic number 20 is hardcoded in multiple places. Consider making this a configurable parameter or constant.
| out.WriteString(truncatePlain(val[prev:r.Start], 20, "...")) | |
| out.WriteString(truncateANSISegment(val[r.Start:r.End], 20, colorEllipsis)) | |
| prev = r.End | |
| } | |
| out.WriteString(truncatePlain(val[prev:], 20, "...")) | |
| out.WriteString(truncatePlain(val[prev:r.Start], truncateLength, "...")) | |
| out.WriteString(truncateANSISegment(val[r.Start:r.End], truncateLength, colorEllipsis)) | |
| prev = r.End | |
| } | |
| out.WriteString(truncatePlain(val[prev:], truncateLength, "...")) |
Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>
Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>
|
Instead of showing errors - |
Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>
This pull request introduces improvements to the handling and display of long string values in JSON diffs, especially when colorized output is involved. The main enhancement is the addition of smart truncation logic that ensures long values are presented in a readable, concise way, with ellipses indicating truncation, while preserving ANSI color formatting. This makes diffs more readable and prevents overwhelming output in the console.
String truncation and formatting improvements:
truncateStringWithEllipsisfunction to intelligently truncate long string values, preserving ANSI color segments and appending ellipses as needed. Supporting helpers for handling ANSI segments and truncation were also added. (jsondiff.go)comparefunction to use the new truncation method for colorized differences, ensuring truncated, readable output for both expected and actual values. (jsondiff.go)writeKeyValuePairto truncate long serialized values to a maximum length, improving summary readability. (jsondiff.go)Dependency and import updates:
"unicode/utf8"import to support rune-aware string truncation. (jsondiff.go)