-
Notifications
You must be signed in to change notification settings - Fork 0
/
dedup_cursor.go
53 lines (47 loc) · 976 Bytes
/
dedup_cursor.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
package histutil
import "github.com/markusbkk/elvish/pkg/store/storedefs"
// NewDedupCursor returns a cursor that skips over all duplicate entries.
func NewDedupCursor(c Cursor) Cursor {
return &dedupCursor{c, 0, nil, make(map[string]bool)}
}
type dedupCursor struct {
c Cursor
current int
stack []storedefs.Cmd
occ map[string]bool
}
func (c *dedupCursor) Prev() {
if c.current < len(c.stack)-1 {
c.current++
return
}
for {
c.c.Prev()
cmd, err := c.c.Get()
if err != nil {
c.current = len(c.stack)
break
}
if !c.occ[cmd.Text] {
c.current = len(c.stack)
c.stack = append(c.stack, cmd)
c.occ[cmd.Text] = true
break
}
}
}
func (c *dedupCursor) Next() {
if c.current >= 0 {
c.current--
}
}
func (c *dedupCursor) Get() (storedefs.Cmd, error) {
switch {
case c.current < 0:
return storedefs.Cmd{}, ErrEndOfHistory
case c.current < len(c.stack):
return c.stack[c.current], nil
default:
return c.c.Get()
}
}