fix(history): should show in priority

This commit is contained in:
verse91
2026-04-19 11:49:13 +07:00
parent 38118a9a47
commit 8032448cb6
+22 -13
View File
@@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
"sync" "sync"
@@ -68,23 +69,19 @@ func SearchHistory(query string) ([]HistResult, error) {
} }
defer file.Close() defer file.Close()
seen := make(map[string]bool) var allCmds []string
counter := 1
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
cmd := line cmd := line
if shellName == "zsh" { if shellName == "zsh" {
// zsh history format: : <timestamp>;<command>
parts := strings.SplitN(line, ";", 2) parts := strings.SplitN(line, ";", 2)
if len(parts) == 2 { if len(parts) == 2 {
cmd = parts[1] cmd = parts[1]
} }
} else if shellName == "bash" { } else if shellName == "bash" {
// bash history format can include timestamps starting with #
if strings.HasPrefix(line, "#") && len(line) > 1 { if strings.HasPrefix(line, "#") && len(line) > 1 {
isTimestamp := true isTimestamp := true
for _, c := range line[1:] { for _, c := range line[1:] {
if c < '0' || c > '9' { if c < '0' || c > '9' {
@@ -97,7 +94,6 @@ func SearchHistory(query string) ([]HistResult, error) {
} }
} }
} else if shellName == "fish" { } else if shellName == "fish" {
// simple parse for fish history (YAML-like)
if strings.HasPrefix(line, "- cmd: ") { if strings.HasPrefix(line, "- cmd: ") {
cmd = strings.TrimPrefix(line, "- cmd: ") cmd = strings.TrimPrefix(line, "- cmd: ")
} else { } else {
@@ -107,14 +103,22 @@ func SearchHistory(query string) ([]HistResult, error) {
cmd = strings.TrimSpace(cmd) cmd = strings.TrimSpace(cmd)
if cmd != "" { if cmd != "" {
if !seen[cmd] { allCmds = append(allCmds, cmd)
historyCache = append(historyCache, cmd)
seen[cmd] = true
}
idMapCache[cmd] = counter
} }
counter++
} }
// build historyCache backwards so newest commands come first
seen := make(map[string]bool)
for i := len(allCmds) - 1; i >= 0; i-- {
cmd := allCmds[i]
if !seen[cmd] {
historyCache = append(historyCache, cmd)
seen[cmd] = true
// we assign the ID as the original line number (1-indexed based on allCmds length)
idMapCache[cmd] = i + 1
}
}
searcherCache = fuzzyvn.NewPlainSearcher(historyCache) searcherCache = fuzzyvn.NewPlainSearcher(historyCache)
} }
@@ -126,7 +130,7 @@ func SearchHistory(query string) ([]HistResult, error) {
} }
for i := 0; i < limit; i++ { for i := 0; i < limit; i++ {
cmd := historyCache[len(historyCache)-1-i] cmd := historyCache[i]
results = append(results, HistResult{ results = append(results, HistResult{
ID: idMapCache[cmd], ID: idMapCache[cmd],
Cmd: cmd, Cmd: cmd,
@@ -145,5 +149,10 @@ func SearchHistory(query string) ([]HistResult, error) {
}) })
} }
// Sort results by ID descending (most recent first) to act as a tie-breaker
sort.SliceStable(results, func(i, j int) bool {
return results[i].ID > results[j].ID
})
return results, nil return results, nil
} }