Skip to content

Commit b97d410

Browse files
author
Motta Kin
committed
feat(repl): clarify displayed search patterns
1 parent df243c0 commit b97d410

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

internal/cli/repl/output/output.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ func formatSearchInput(input map[string]any, workingDir string) string {
290290
pattern, _ := input["pattern"].(string)
291291
if pattern != "" {
292292
pattern = compactDisplayValue(pattern, maxDisplayPatternRunes)
293+
pattern = formatSearchPatternForDisplay(pattern)
293294
}
294295
path, _ := input["path"].(string)
295296
if path != "" {
@@ -308,6 +309,18 @@ func formatSearchInput(input map[string]any, workingDir string) string {
308309
}
309310
}
310311

312+
func formatSearchPatternForDisplay(pattern string) string {
313+
var b strings.Builder
314+
b.Grow(len(pattern))
315+
for i := 0; i < len(pattern); i++ {
316+
if pattern[i] == '\\' && i+1 < len(pattern) && strings.ContainsRune(`\\.^$*+?()[]{}|`, rune(pattern[i+1])) {
317+
i++
318+
}
319+
b.WriteByte(pattern[i])
320+
}
321+
return b.String()
322+
}
323+
311324
func formatGenericInput(input map[string]any) string {
312325
keys := make([]string, 0, len(input))
313326
for k := range input {

internal/cli/repl/output/output_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ func TestFormatToolInput_GrepShowsQuotedPatternInPathBaseName(t *testing.T) {
145145
}
146146
}
147147

148+
func TestFormatToolInput_GrepUnescapesLiteralRegexPunctuationForDisplay(t *testing.T) {
149+
pattern := `\[Unreleased\]|\[0\.38\.1\] \\d+ \\w+ \\s+ \\b`
150+
got := FormatToolInput("grep", map[string]any{"pattern": pattern, "path": "CHANGELOG.md"}, "/tmp/project")
151+
152+
want := `[Unreleased]|[0.38.1] \d+ \w+ \s+ \b in CHANGELOG.md`
153+
if got != want {
154+
t.Fatalf("expected display-only literal unescaping, got %q, want %q", got, want)
155+
}
156+
if pattern != `\[Unreleased\]|\[0\.38\.1\] \\d+ \\w+ \\s+ \\b` {
157+
t.Fatalf("expected original pattern to remain unchanged, got %q", pattern)
158+
}
159+
}
160+
148161
func TestFormatToolInput_CallMCPToolShowsOnlyServerAndTool(t *testing.T) {
149162
got := FormatToolInput("call_mcp_tool", map[string]any{
150163
"server": "context7",

0 commit comments

Comments
 (0)