-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.go
More file actions
134 lines (110 loc) · 2.83 KB
/
history.go
File metadata and controls
134 lines (110 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// history.go
// history management module
//
// Copyright (c) 2024-2026 jjb
// All rights reserved.
//
// This source code is licensed under the MIT license found
// in the root directory of this source tree.
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)
// loadHistory reads the history file and returns recent entries.
func loadHistory(historyFile string) []string {
home, err := os.UserHomeDir()
if err != nil {
return []string{}
}
historyPath := filepath.Join(home, RCDIRNAME, historyFile)
file, err := os.OpenFile(historyPath, os.O_RDONLY|os.O_CREATE, 0600)
if err != nil {
return []string{}
}
defer file.Close()
var history []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
history = append(history, scanner.Text())
}
// If history is too large, keep only the most recent entries
if len(history) > maxHistorySize {
history = history[len(history)-maxHistorySize:]
}
return history
}
// saveHistory writes history entries to disk with trimming and cleanup.
func saveHistory(history []string, historyFile string) {
if len(history) == 0 {
return
}
// Clean history in-place to avoid new slice allocation
n := 0
for _, entry := range history {
if historyFile != searchHistory {
entry = strings.TrimSpace(entry)
}
if entry != "" {
history[n] = entry
n++
}
}
history = history[:n]
if len(history) == 0 {
return
}
// Ensure history doesn't exceed max size
if len(history) > maxHistorySize {
history = history[len(history)-maxHistorySize:]
}
home, err := os.UserHomeDir()
if err != nil {
return
}
historyPath := filepath.Join(home, RCDIRNAME, historyFile)
file, err := os.OpenFile(historyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return
}
defer file.Close()
writer := bufio.NewWriter(file)
for _, cmd := range history {
fmt.Fprintln(writer, cmd)
}
writer.Flush()
}
// updateDirHistory records directory changes in the directory history.
func updateDirHistory(savDir, curDir string) {
// save the previous directory
updateHistory(savDir, dirHistory)
// save the current directory
updateHistory(curDir, dirHistory)
}
// updateHistory appends a single entry to the specified history file.
func updateHistory(newEntry, historyFile string) {
if historyFile != searchHistory {
newEntry = strings.TrimSpace(newEntry)
}
if newEntry == "" {
return
}
if historyFile == commHistory || historyFile == searchHistory {
newEntry = unQuote(newEntry)
} else {
if strings.ContainsAny(newEntry, " ") && !strings.ContainsAny(newEntry, "'") {
newEntry = "'" + newEntry + "'"
}
}
history := loadHistory(historyFile)
// Remove duplicate consecutive entries
if len(history) > 0 && history[len(history)-1] == newEntry {
return
}
history = append(history, newEntry)
saveHistory(history, historyFile)
}
// vim: set ts=4 sw=4 noet: