-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
linecache.go
66 lines (61 loc) · 1.33 KB
/
linecache.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
// Package linecache provides a writer that stores n lines of text at once, overwriting old content as it reaches its capacity. Its contents can be read from with a String() method.
package linecache
import (
"strings"
"sync"
)
// LineCache provides an io.Writer that stores a fixed number of lines of text.
type LineCache struct {
count int
lines [][]byte
current int
lock *sync.Mutex
}
// NewLineCache returns a new line cache of capacity (n) lines.
func NewLineCache(n int) *LineCache {
return &LineCache{
current: 0,
count: n,
lines: make([][]byte, n),
lock: &sync.Mutex{},
}
}
// Write writes a given byte array to the cache.
func (l *LineCache) Write(p []byte) (n int, err error) {
l.lock.Lock()
defer l.lock.Unlock()
lines := strings.Split(string(p), "\n")
for _, line := range lines {
if string(line) == "" {
continue
}
if l.current == l.count {
l.current = 0
}
l.lines[l.current] = []byte(line)
l.current++
}
n = len(p)
return
}
// String returns a string representation of the cache contents.
func (l *LineCache) String() string {
i := 0
if l.lines[l.count-1] != nil && l.current != l.count {
i = l.current
}
out := ""
for {
if l.lines[i] == nil {
return out
}
out += string(l.lines[i]) + "\n"
i++
if i == l.current {
return out
}
if i == l.count {
i = 0
}
}
}