Skip to content

Commit 1ead6cc

Browse files
committed
feat: use regex instead
1 parent dafb6ab commit 1ead6cc

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

lib/msgfmt/message_box.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package msgfmt
22

33
import (
4+
"regexp"
45
"strings"
56
)
67

8+
var genericSlimMessageBoxPattern = regexp.MustCompile(`(?m)^.*─{15,}.*\n.*[|│❯].*\n(?:.*\n)?.*─{15,}.*`)
9+
710
// Usually something like
811
// ───────────────
912
// >
@@ -26,16 +29,25 @@ func findGreaterThanMessageBox(lines []string) int {
2629
// |
2730
// ───────────────
2831
func findGenericSlimMessageBox(lines []string) int {
29-
for i := len(lines) - 3; i >= max(len(lines)-9, 0); i-- {
30-
if strings.Contains(lines[i], "───────────────") &&
31-
(strings.Contains(lines[i+1], "|") || strings.Contains(lines[i+1], "│") || strings.Contains(lines[i+1], "❯")) {
32-
if (i+2 < len(lines) && strings.Contains(lines[i+2], "───────────────")) ||
33-
(i+3 < len(lines) && strings.Contains(lines[i+3], "───────────────")) {
34-
return i
35-
}
36-
}
32+
// genericSlimMessageBoxPattern matches a message box pattern like:
33+
// ───────────────
34+
// | or │ or ❯
35+
//
36+
// (optional line(s))
37+
// ───────────────
38+
39+
// Search within the last ~9 lines for the message box pattern
40+
startIdx := max(len(lines)-9, 0)
41+
searchText := strings.Join(lines[startIdx:], "\n")
42+
43+
loc := genericSlimMessageBoxPattern.FindStringIndex(searchText)
44+
if loc == nil {
45+
return -1
3746
}
38-
return -1
47+
48+
// Count newlines before the match to find the line number
49+
linesBeforeMatch := strings.Count(searchText[:loc[0]], "\n")
50+
return startIdx + linesBeforeMatch
3951
}
4052

4153
func removeMessageBox(msg string) string {

0 commit comments

Comments
 (0)