-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.go
140 lines (113 loc) · 2.72 KB
/
history.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
package rofi
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"path"
)
const maxHistoryCount = 5
func SortUsingHistory(opts []Option, namespace string) []Option {
cache, err := getCachePath(namespace)
if err != nil {
log.Printf("Error while finding cache: %s\n", err)
return opts
}
f, err := os.OpenFile(cache, os.O_RDONLY, 0666)
if err != nil {
if !os.IsNotExist(err) {
log.Printf("Error while opening cache: %s\n", err)
}
return opts
}
defer f.Close()
history, err := readHistory(f)
if err != nil {
log.Printf("Error while reading history: %s", err)
return opts
}
prio := []Option{}
for _, h := range history {
for i, opt := range opts {
if h == opt.Value {
opts = append(opts[:i], opts[i+1:]...)
prio = append(prio, opt)
}
}
}
return append(prio, opts...)
}
func SaveToHistory(namespace, value string) {
isNewFile := false
var history []string
cache, err := getCachePath(namespace)
if err != nil {
log.Printf("Error while finding cache: %s\n", err)
return
}
if err := os.MkdirAll(path.Dir(cache), os.ModePerm); err != nil {
log.Printf("Error while creating path: %s\n", err)
return
}
_, err = os.Stat(cache)
if err != nil && os.IsNotExist(err) {
isNewFile = true
}
f, err := os.OpenFile(cache, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
log.Printf("Error while opening cache: %s\n", err)
return
}
defer f.Close()
if !isNewFile {
history, err = readHistory(f)
if err != nil {
log.Printf("Error while reading history: %s\n", err)
}
// shifting
for i, val := range history {
if val == value {
history = append(history[:i], history[i+1:]...)
}
}
}
nextHistory := []string{value}
if len(history) >= maxHistoryCount {
nextHistory = append(nextHistory, history[:maxHistoryCount-1]...)
} else {
nextHistory = append(nextHistory, history...)
}
if err := writeHistory(f, nextHistory); err != nil {
log.Printf("Error while saving history: %s\n", err)
}
}
func getCachePath(namespace string) (string, error) {
cache, err := os.UserCacheDir()
if err != nil {
return "", err
}
return path.Join(cache, "/rofi/", namespace+".json"), nil
}
func readHistory(f *os.File) ([]string, error) {
var content []string
b, err := io.ReadAll(f)
if err != nil {
return content, err
}
err = json.Unmarshal(b, &content)
return content, err
}
func writeHistory(f *os.File, content []string) error {
b, err := json.MarshalIndent(content, "", " ")
if err != nil {
return fmt.Errorf("error while marshalling history: %w", err)
}
if err := f.Truncate(0); err != nil {
return fmt.Errorf("error while clearing history: %w", err)
}
if _, err := f.WriteAt(b, 0); err != nil {
return fmt.Errorf("error while writing history: %w", err)
}
return nil
}