-
Notifications
You must be signed in to change notification settings - Fork 1
/
buffer_stack.go
51 lines (43 loc) · 1.29 KB
/
buffer_stack.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
package pulse
// bufferStack represents the buffers that have been opened during a coding session.
type bufferStack struct {
buffers []Buffer
}
// newBufferStack creates a new buffer stack.
func newBufferStack() *bufferStack {
buffers := make([]Buffer, 0)
return &bufferStack{buffers}
}
// push pushes a buffer onto the stack.
func (b *bufferStack) push(f Buffer) {
b.buffers = append(b.buffers, f)
}
// peek returns a pointer to the most recent buffer.
func (b *bufferStack) peek() *Buffer {
if len(b.buffers) == 0 {
return nil
}
return &b.buffers[len(b.buffers)-1]
}
// files takes the stack of buffers, merges them by filepath,
// and returns the result in the order they were opened in.
func (b *bufferStack) files() Files {
sortOrder := make([]string, 0)
pathFile := make(map[string]File)
// Merge the buffers by filepath.
for _, buffer := range b.buffers {
if file, exists := pathFile[buffer.Filepath]; !exists {
sortOrder = append(sortOrder, buffer.Filepath)
pathFile[buffer.Filepath] = fileFromBuffer(buffer)
} else {
file.DurationMs += buffer.Duration()
pathFile[buffer.Filepath] = file
}
}
// Return the buffers in the original order.
files := make(Files, 0, len(pathFile))
for _, path := range sortOrder {
files = append(files, pathFile[path])
}
return files
}