-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.go
161 lines (143 loc) · 4.15 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// history
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"sort"
"strconv"
"strings"
"time"
)
// SortHistoryCmdText : sort history by time or frequency
const SortHistoryCmdText = "Sort by time(ctrl+t) / frequency(ctrl+f)"
// MaxWordHistoryLimit : Max word history size
const MaxWordHistoryLimit = 5000
const historyPerPage = 10
var historyFile string = "aleng_history.txt"
var wordHistory []WordHistoryData
var curHistoryIndex int
var curHistoryPageIndex int
// SortType : sort type
type SortType int
// enum sortType
const (
SortByTime SortType = 1 + iota
SortBySearchFrequency
)
// ReadHistoryFile : read the history file
func ReadHistoryFile() {
wordHistory = nil
curHistoryPageIndex = 0
usr, err := user.Current()
if err != nil {
log.Fatal("Can't find user current() err=>", err.Error())
}
historyFile = usr.HomeDir + "/" + historyFile
if _, err = os.Stat(historyFile); os.IsNotExist(err) {
_, err = os.Create(historyFile)
if err != nil {
log.Fatalf("Can't create file:(%v) err=> %v", historyFile, err.Error())
}
fmt.Printf("%v file created.\n", historyFile)
}
history, err := ioutil.ReadFile(historyFile)
if err != nil {
log.Fatalf("Can't read file:(%v) err=> %v", historyFile, err.Error())
return
}
spWord := strings.Split(string(history), "--\n")
for i := 0; i < len(spWord); i++ {
curWord := (strings.Split(spWord[i], "\n"))
whd := WordHistoryData{}
if len(curWord) < 5 {
continue
}
whd.date, _ = time.Parse(time.RFC3339, curWord[0])
whd.searchFrequency, _ = strconv.Atoi(curWord[1])
whd.wd.word = curWord[2]
whd.wd.pronounce = curWord[3]
whd.wd.meanings = strings.Join(curWord[4:], "\n")
wordHistory = append(wordHistory, whd)
}
// limit max history size
if len(wordHistory) > MaxWordHistoryLimit {
wordHistory = wordHistory[:MaxWordHistoryLimit]
}
}
// SortWordHistoryData : sort word history data by date or frequency...
func SortWordHistoryData(whd []WordHistoryData, sortType SortType) {
if sortType == SortByTime {
sort.Slice(whd, func(a, b int) bool {
return whd[a].date.Unix() > whd[b].date.Unix()
})
return
}
if sortType == SortBySearchFrequency {
sort.Slice(whd, func(a, b int) bool {
if whd[a].searchFrequency == whd[b].searchFrequency {
return whd[a].wd.word < whd[b].wd.word
}
return whd[a].searchFrequency > whd[b].searchFrequency
})
return
}
}
// WordHistoryData2String : make one line string message from word data.
func WordHistoryData2String(whd []WordHistoryData) string {
out := ""
wc := len(whd)
for i := 0; i < wc; i++ {
out += strings.TrimSpace(whd[i].date.Format(time.RFC3339)+"\n"+strconv.Itoa(whd[i].searchFrequency)+"\n"+whd[i].wd.word+"\n"+whd[i].wd.pronounce+"\n"+whd[i].wd.meanings) + "\n"
if wc > 1 && i < wc-1 {
out += "--\n"
}
}
return out
}
// GetNextHistoryPageIndex : get next word history index
func GetNextHistoryPageIndex() int {
curHistoryPageIndex = curHistoryPageIndex + historyPerPage
if curHistoryPageIndex >= len(wordHistory) {
curHistoryPageIndex = 0
}
return curHistoryPageIndex
}
// GetPreHistoryPagaeIndex : get previous word history index
func GetPreHistoryPagaeIndex() int {
curHistoryPageIndex = curHistoryPageIndex - historyPerPage
if curHistoryPageIndex < 0 {
curHistoryPageIndex = ((len(wordHistory) - 1) / historyPerPage) * historyPerPage
}
return curHistoryPageIndex
}
func getWordHistoryInPage(startIdx int) (int, []WordHistoryData) {
whd := []WordHistoryData{}
if startIdx < 0 {
return 0, whd
}
whLen := len(wordHistory)
if len(wordHistory) > 0 {
for i := startIdx; i < startIdx+historyPerPage; i++ {
if i >= whLen {
break
}
whd = append(whd, wordHistory[i])
}
}
return startIdx, whd
}
// GetWordHistoryInPage : get words in a specific page
func GetWordHistoryInPage(startIdx int) (int, []WordHistoryData) {
return getWordHistoryInPage(startIdx)
}
// GetPreWordHistoryInPage : get previous words in a page
func GetPreWordHistoryInPage() (int, []WordHistoryData) {
return getWordHistoryInPage(GetPreHistoryPagaeIndex())
}
// GetNextWordHistoryInPage : get next words in a page
func GetNextWordHistoryInPage() (int, []WordHistoryData) {
return getWordHistoryInPage(GetNextHistoryPageIndex())
}