forked from shazow/ssh-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.go
59 lines (49 loc) · 1.04 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
// TODO: Split this out into its own module, it's kinda neat.
package main
import "sync"
// History contains the history entries
type History struct {
entries []string
head int
size int
lock sync.Mutex
}
// NewHistory constructs a new history of the given size
func NewHistory(size int) *History {
return &History{
entries: make([]string, size),
}
}
// Add adds the given entry to the entries in the history
func (h *History) Add(entry string) {
h.lock.Lock()
defer h.lock.Unlock()
max := cap(h.entries)
h.head = (h.head + 1) % max
h.entries[h.head] = entry
if h.size < max {
h.size++
}
}
// Len returns the number of entries in the history
func (h *History) Len() int {
return h.size
}
// Get the entry with the given number
func (h *History) Get(num int) []string {
h.lock.Lock()
defer h.lock.Unlock()
max := cap(h.entries)
if num > h.size {
num = h.size
}
r := make([]string, num)
for i := 0; i < num; i++ {
idx := (h.head - i) % max
if idx < 0 {
idx += max
}
r[num-i-1] = h.entries[idx]
}
return r
}