From ab41926fc0a33ea34540df0ec6f2940b4ad8188c Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Sun, 30 Mar 2025 00:28:23 -0400 Subject: [PATCH] feat(parser/diff): option to hide common prefix --- internal/parser/diff/meta.go | 17 +++++++++-------- internal/parser/diff/parser.go | 25 ++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/internal/parser/diff/meta.go b/internal/parser/diff/meta.go index 563b564..cd7ea42 100644 --- a/internal/parser/diff/meta.go +++ b/internal/parser/diff/meta.go @@ -14,14 +14,15 @@ type Conf struct { ForceQuitOnFailed bool `default:"false"` Cases []struct { Outputs []struct { - Score int - FileName string - AnswerPath string - CompareSpace bool - AlwaysHide bool - ForceQuitOnDiff bool - MaxDiffLength int `default:"2048"` // just for reference - MaxDiffLines int `default:"50"` // just for reference + Score int + FileName string + AnswerPath string + CompareSpace bool + AlwaysHide bool + ForceQuitOnDiff bool + MaxDiffLength int `default:"2048"` // just for reference + MaxDiffLines int `default:"50"` // just for reference + HideCommonPrefix bool } } } diff --git a/internal/parser/diff/parser.go b/internal/parser/diff/parser.go index 1799f22..7cbce0b 100644 --- a/internal/parser/diff/parser.go +++ b/internal/parser/diff/parser.go @@ -85,6 +85,23 @@ func (*Diff) Run(results []stage.ExecutorResult, confAny any) ( } answerLines := strings.Split(answerStr, "\n") resultLines := strings.Split(resultStr, "\n") + commonPreixLineCount := 0 + if output.HideCommonPrefix { + n := 0 + for ; n < len(answerLines) && + n < len(resultLines) && + stringsEqual( + answerLines[n], + resultLines[n], + output.CompareSpace, + ); n += 1 { + } + if n > 0 { + answerLines = answerLines[n:] + resultLines = resultLines[n:] + commonPreixLineCount = n + } + } if len(answerLines) > output.MaxDiffLines { answerLines = answerLines[:output.MaxDiffLines] truncated = true @@ -104,12 +121,18 @@ func (*Diff) Run(results []stage.ExecutorResult, confAny any) ( if truncated { diffOutput += "\n\n(truncated)" } + if commonPreixLineCount > 0 { + diffOutput = fmt.Sprintf( + "(%d line(s) of common prefix hidden)\n\n", + commonPreixLineCount, + ) + diffOutput + } comment += fmt.Sprintf( "```diff\n%s\n```\n", diffOutput, ) } else { - comment += "(Content hidden.)\n" + comment += "(content hidden)\n" } } }