xonecas.io
5119
Lh

lazy-history / commit aefb1fe

initial: adds basic idea to search the history file

Sean2025-05-02 aefb1febe9519820e8108f28853ef6c5a7bce24c parent c69065f

4 files changed

go.mod+3−0
@@ -0,0 +1,3 @@
1+module github.com/sacenox/lazy-history
2+
3+go 1.24.0
lazy-history.go+59−0
@@ -0,0 +1,59 @@
1+package main
2+
3+import (
4+ "bufio"
5+ "os"
6+ "path/filepath"
7+
8+ lib "github.com/sacenox/lazy-history/lib"
9+)
10+
11+func main() {
12+
13+ query := os.Args[1] // args without program name
14+
15+ lib.Debugf("Query: %s", query)
16+
17+ history := []string{} // one cmd per entry
18+
19+ historyFilePaths := []string{
20+ filepath.Join(os.Getenv("HOME"), ".history"),
21+ filepath.Join(os.Getenv("HOME"), ".zsh_history"),
22+ filepath.Join(os.Getenv("HOME"), ".bash_history"),
23+ }
24+
25+ for _, historyFilePath := range historyFilePaths {
26+ if _, err := os.Stat(historyFilePath); os.IsNotExist(err) {
27+ lib.Debugf("History file %s does not exist", historyFilePath)
28+ continue
29+ }
30+
31+ historyFile, err := os.Open(historyFilePath)
32+ if err != nil {
33+ lib.Fatalf("Failed to open history file: %v", err)
34+ }
35+
36+ scanner := bufio.NewScanner(historyFile)
37+ // Read each line from the history file
38+ for scanner.Scan() {
39+ line := scanner.Text()
40+ history = append(history, line)
41+ }
42+
43+ lib.Debugf("Read %d lines from history file %s", len(history), historyFilePath)
44+
45+ // Check for scanner errors
46+ if err := scanner.Err(); err != nil {
47+ lib.Debugf("Error reading history file %s: %v", historyFilePath, err)
48+ }
49+
50+ historyFile.Close()
51+ }
52+
53+ results := lib.Search(history, query)
54+ // Print each line of the search results
55+ for _, result := range results {
56+ lib.Debugf("found: %s", result)
57+ }
58+
59+}
lib/log-util.go+22−0
@@ -0,0 +1,22 @@
1+package lib
2+
3+import (
4+ "log"
5+ "os"
6+)
7+
8+func Debug(v ...interface{}) {
9+ if os.Getenv("DEBUG") != "" {
10+ log.Println(v...)
11+ }
12+}
13+
14+func Debugf(format string, v ...interface{}) {
15+ if os.Getenv("DEBUG") != "" {
16+ log.Printf(format, v...)
17+ }
18+}
19+
20+func Fatalf(format string, v ...interface{}) {
21+ log.Fatalf(format, v...)
22+}
lib/search-util.go+41−0
@@ -0,0 +1,41 @@
1+package lib
2+
3+import (
4+ "slices"
5+ "sort"
6+)
7+
8+func FindOne(history []string, query string) (string, int) {
9+ // search using a binary search
10+ i := sort.Search(len(history), func(i int) bool {
11+ Debugf("history[%d]: %q", i, history[i])
12+ return history[i] >= query
13+ })
14+ if i < len(history) {
15+ Debugf("found %q at index %d\n", query, i)
16+ return history[i], i
17+ } else {
18+ Debugf("%q not found\n", query)
19+ return "", -1
20+ }
21+}
22+
23+func Search(history []string, query string) []string {
24+ results := []string{}
25+ // sort the array
26+ // note: am using an explicit sort function because unknown types in actual array, and not using a stable sort function because don't care that items that are equal to one another remain in the same order.
27+ slices.Sort(history)
28+
29+ // Keep searching for matches until we don't find any more
30+ for {
31+ result, i := FindOne(history, query)
32+ if result == "" {
33+ break
34+ }
35+
36+ results = append(results, result)
37+ history = slices.Delete(history, i, i+1)
38+ }
39+
40+ return results
41+}