-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zshist.go
170 lines (139 loc) · 3.21 KB
/
zshist.go
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"regexp"
"sort"
"strings"
)
var cmdCt = 0 // command count
var fileCt = 0 // file count
var nomatch = 1 // command that doesn't match zsh format
var keys = []string{} // dedup keys
var lines = map[string]string{} // the zsh history lines
var zshline = regexp.MustCompile(`^(?sm)(\: \d{10,}:\d{1,};)(.*)`) // zsh history line pattern
var v = flag.Bool("v", false, "Shows version")
var home = flag.String("home", os.Getenv("HOME"), "Home dir, defaults to $HOME env")
var Version = "0.0.1" // injected
func main() {
flag.Parse()
if *v {
fmt.Println(Version)
return
}
if *home == "" {
log.Fatal("Please set $HOME env, or pass in as -home")
}
src := "/.zsh_history"
bak := "/.zsh_history.bak"
handle(src, bak)
fmt.Printf("Parsed and merged %d files with %d commands\n", fileCt, cmdCt)
fmt.Printf("Saved into ~%s with %d commands\n", src, len(keys))
fmt.Printf("Backed up into ~%s\n", bak)
}
// handler does all the operation
func handle(src, bak string) {
zsh, err := os.OpenFile(*home+src, os.O_RDWR, 0)
if err != nil {
log.Fatal(err)
}
parse(zsh)
defer zsh.Close()
// zsh pre history
pre := *home + "/.zsh_history.pre-oh-my-zsh"
if hist, err := os.Open(pre); err == nil {
parse(hist)
hist.Close()
os.Rename(pre, pre+".bak")
}
// bash history
if hist, err := os.Open(*home + "/.bash_history"); err == nil {
defer hist.Close()
parse(hist)
}
// Backup+save
if err = backup(zsh, *home+bak); err == nil {
err = save(zsh, keys, lines)
}
if err != nil {
log.Fatal(err)
}
}
// parse parses given file handler
func parse(file *os.File) {
scan := bufio.NewScanner(file)
for scan.Scan() {
line := strings.Trim(scan.Text(), " \t")
if line == "" || line[0] == '#' {
continue
}
// Multiline command
for line[len(line)-1:] == `\` && scan.Scan() {
line += "\n" + scan.Text()
}
cmd, ts := "", ""
sub := zshline.FindAllStringSubmatch(line, 1)
if len(sub) > 0 {
cmd, ts = strings.Trim(sub[0][2], " \t"), sub[0][1]
} else {
cmd, ts, nomatch = line, fmt.Sprintf("%d", nomatch), nomatch+1
}
cmdCt++
if cmd == "" || cmd[0] == '#' {
continue
}
ots, ok := lines[cmd]
if !ok {
keys = append(keys, cmd)
}
if !ok || ts > ots {
lines[cmd] = ts
}
}
fileCt++
}
// backup takes a backup with .bak ext
func backup(zsh *os.File, bak string) error {
zshb, err := os.Create(bak)
if err != nil {
return err
}
defer zshb.Close()
// Copy and sync
zsh.Seek(0, io.SeekStart)
if _, err = io.Copy(zshb, zsh); err != nil {
return err
}
return zshb.Sync()
}
// save saves the zsh file
func save(zsh *os.File, keys []string, lines map[string]string) (err error) {
if len(keys) == 0 || len(lines) == 0 {
return nil
}
// Empty
if err = zsh.Truncate(0); err == nil {
_, err = zsh.Seek(0, io.SeekStart)
}
if err != nil {
return
}
// Sort
sort.SliceStable(keys, func(i, j int) bool { return lines[keys[i]] < lines[keys[j]] })
// Write by line
for _, val := range keys {
key := lines[val]
if key[0] != ':' {
key = ""
}
if _, err = zsh.WriteString(key + val + "\n"); err != nil {
return err
}
}
// Sync
return zsh.Sync()
}