-
Notifications
You must be signed in to change notification settings - Fork 4
/
buffer.go
66 lines (56 loc) · 1.72 KB
/
buffer.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 pulse
import (
"cmp"
"fmt"
"time"
)
// Buffer rerpresents a buffer that has been edited during a coding session.
type Buffer struct {
OpenedAt time.Time `json:"-"`
ClosedAt time.Time `json:"-"`
Duration time.Duration `json:"duration"`
Filename string `json:"filename"`
Filepath string `json:"filepath"`
Filetype string `json:"filetype"`
Repository string `json:"repository"`
}
// NewBuffer creates a new buffer.
func NewBuffer(filename, repo, filetype, filepath string, openedAt time.Time) Buffer {
return Buffer{
OpenedAt: openedAt,
Filename: filename,
Filepath: filepath,
Filetype: filetype,
Repository: repo,
}
}
// Close should be called when the coding session ends, or another buffer is opened.
func (b *Buffer) Close(closedAt time.Time) {
b.ClosedAt = closedAt
b.Duration = b.ClosedAt.Sub(b.OpenedAt)
}
// Key returns a unique identifier for the buffer.
func (b *Buffer) Key() string {
return fmt.Sprintf("%s_%s_%s", b.OpenedAt.Format("2006-01-02"), b.Repository, b.Filepath)
}
// Merge takes two buffers, merges them, and returns the result.
func (b *Buffer) Merge(other Buffer) Buffer {
return Buffer{
Filename: cmp.Or(b.Filename, other.Filename),
Filepath: cmp.Or(b.Filepath, other.Filepath),
Filetype: cmp.Or(b.Filetype, other.Filetype),
Repository: cmp.Or(b.Repository, other.Repository),
Duration: b.Duration + other.Duration,
}
}
// Buffers represents a slice of buffers that have been edited during a coding session.
type Buffers []Buffer
func (b Buffers) Len() int {
return len(b)
}
func (b Buffers) Less(i, j int) bool {
return b[i].OpenedAt.Before(b[j].OpenedAt)
}
func (b Buffers) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}