fix(history): fuzzy search not working (#44)

quick fix, please read commit I'm too lazy to write description of
changes
This commit is contained in:
VERSE
2026-07-27 20:05:44 +07:00
committed by GitHub
parent 078e5e817c
commit 84f2c8f49a
+33 -6
View File
@@ -208,12 +208,20 @@ func SearchHistory(query string, aliases map[string]string) ([]HistResult, error
var results []HistResult var results []HistResult
seenCmds := make(map[string]bool) seenCmds := make(map[string]bool)
addMatches := func(q string) { addMatches := func(q string, subcmdFilter bool) {
qLow := strings.ToLower(q) qLow := strings.ToLower(q)
queryFirstWord := "" queryFirstWord := ""
querySecondWord := ""
if strings.IndexByte(qLow, ' ') != -1 { if strings.IndexByte(qLow, ' ') != -1 {
if fields := strings.Fields(qLow); len(fields) > 0 { if fields := strings.Fields(qLow); len(fields) > 0 {
queryFirstWord = fields[0] queryFirstWord = fields[0]
// find first non-flag token after the command as the subcommand
for _, f := range fields[1:] {
if !strings.HasPrefix(f, "-") {
querySecondWord = f
break
}
}
} }
} }
@@ -224,9 +232,10 @@ func SearchHistory(query string, aliases map[string]string) ([]HistResult, error
} }
// filter results by command name match // filter results by command name match
fields := strings.Fields(m.Str)
firstWord := m.Str firstWord := m.Str
if idx := strings.IndexByte(m.Str, ' '); idx != -1 { if len(fields) > 0 {
firstWord = m.Str[:idx] firstWord = fields[0]
} }
firstWordLow := strings.ToLower(firstWord) firstWordLow := strings.ToLower(firstWord)
@@ -234,6 +243,16 @@ func SearchHistory(query string, aliases map[string]string) ([]HistResult, error
if firstWordLow != queryFirstWord { if firstWordLow != queryFirstWord {
continue continue
} }
// when query has a non-flag second token, filter by subcommand prefix
if subcmdFilter && querySecondWord != "" {
if len(fields) < 2 {
continue
}
secondWordLow := strings.ToLower(fields[1])
if !strings.HasPrefix(secondWordLow, querySecondWord) {
continue
}
}
} else { } else {
if !strings.HasPrefix(firstWordLow, qLow) { if !strings.HasPrefix(firstWordLow, qLow) {
continue continue
@@ -249,10 +268,18 @@ func SearchHistory(query string, aliases map[string]string) ([]HistResult, error
} }
} }
addMatches(query) addMatches(query, true)
for _, altQ := range alternativeQueries { for _, altQ := range alternativeQueries {
addMatches(altQ) addMatches(altQ, true)
}
// fallback: if subcommand filter produced nothing, retry without it
// so typos like "git chckout" still surface fuzzy matches
if len(results) == 0 {
addMatches(query, false)
for _, altQ := range alternativeQueries {
addMatches(altQ, false)
}
} }
getTier := func(cmd, q string) int { getTier := func(cmd, q string) int {