Skip to content

feat: truncate string values and add ellipsis#24

Merged
Sarthak160 merged 4 commits intomainfrom
feat/truncate-strings
Aug 19, 2025
Merged

feat: truncate string values and add ellipsis#24
Sarthak160 merged 4 commits intomainfrom
feat/truncate-strings

Conversation

@ayush3160
Copy link
Copy Markdown
Contributor

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:

  • Added the truncateStringWithEllipsis function 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)
  • Updated the diff output logic in the compare function to use the new truncation method for colorized differences, ensuring truncated, readable output for both expected and actual values. (jsondiff.go)
  • Modified the fallback serialization of values in writeKeyValuePair to truncate long serialized values to a maximum length, improving summary readability. (jsondiff.go)

Dependency and import updates:

  • Added the "unicode/utf8" import to support rune-aware string truncation. (jsondiff.go)

Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>
Copilot AI review requested due to automatic review settings August 11, 2025 02:35
@keploy
Copy link
Copy Markdown

keploy Bot commented Aug 11, 2025

No significant code changes identified for Unit Test Generation. Please Click here to retry.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 truncateStringWithEllipsis function 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

Comment thread jsondiff.go Outdated
Comment on lines +254 to +255
if len(string(serializedValue)) > 30 {
formattedValue = string(serializedValue[:30]) + "..."
Copy link

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 30 should be defined as a named constant to improve maintainability and make the truncation length configurable.

Suggested change
if len(string(serializedValue)) > 30 {
formattedValue = string(serializedValue[:30]) + "..."
if len(string(serializedValue)) > truncationLength {
formattedValue = string(serializedValue[:truncationLength]) + "..."

Copilot uses AI. Check for mistakes.
Comment thread jsondiff.go Outdated
Comment on lines +254 to +255
if len(string(serializedValue)) > 30 {
formattedValue = string(serializedValue[:30]) + "..."
Copy link

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Suggested change
if len(string(serializedValue)) > 30 {
formattedValue = string(serializedValue[:30]) + "..."
if utf8.RuneCountInString(formattedValue) > 30 {
runes := []rune(formattedValue)
formattedValue = string(runes[:30]) + "..."

Copilot uses AI. Check for mistakes.
Comment thread jsondiff.go Outdated
Comment on lines +851 to +855
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, "..."))
Copy link

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 20 is hardcoded in multiple places. Consider making this a configurable parameter or constant.

Suggested change
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, "..."))

Copilot uses AI. Check for mistakes.
Comment thread jsondiff.go Outdated
Comment on lines +851 to +855
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, "..."))
Copy link

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 20 is hardcoded in multiple places. Consider making this a configurable parameter or constant.

Suggested change
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, "..."))

Copilot uses AI. Check for mistakes.
Comment thread jsondiff.go Outdated
Comment on lines +851 to +855
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, "..."))
Copy link

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 20 is hardcoded in multiple places. Consider making this a configurable parameter or constant.

Suggested change
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, "..."))

Copilot uses AI. Check for mistakes.
Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>
Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>
@Sarthak160
Copy link
Copy Markdown
Contributor

Instead of showing errors - ERROR failed to read the test-set report {"reportName": "test-set-10-report", "session": "test-run-26", "error": "failed to read the file: open /home/shubham/sarthak_workspace/python-mysql-big-payload-3/keploy/reports/test-run-26/test-set-10-report.yaml: no such file or directory"} we should show the test-sets selected in an array at the beginning.

Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>
@Sarthak160 Sarthak160 merged commit 67e89b1 into main Aug 19, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants