1
1
package pulse
2
2
3
+ import "time"
4
+
3
5
// CodingSession represents an ongoing coding session.
4
6
type CodingSession struct {
5
7
// startStops is a slice of timestamps representing the start and stop times of
6
8
// an ongoing coding session. This ensures accurate time tracking when switching
7
9
// between multiple editor processes. We only count time for one at a time.
8
- startStops []int64
10
+ startStops []time. Time
9
11
bufStack * bufferStack
10
12
EditorID string
11
- StartedAt int64
13
+ StartedAt time. Time
12
14
OS string
13
15
Editor string
14
16
}
15
17
16
18
// StartSession creates a new coding session.
17
- func StartSession (editorID string , startedAt int64 , os , editor string ) * CodingSession {
19
+ func StartSession (editorID string , startedAt time. Time , os , editor string ) * CodingSession {
18
20
return & CodingSession {
19
- startStops : []int64 {startedAt },
21
+ startStops : []time. Time {startedAt },
20
22
bufStack : newBufferStack (),
21
23
EditorID : editorID ,
22
24
StartedAt : startedAt ,
@@ -26,23 +28,23 @@ func StartSession(editorID string, startedAt int64, os, editor string) *CodingSe
26
28
}
27
29
28
30
// Pause should be called when another editor process gains focus.
29
- func (s * CodingSession ) Pause (time int64 ) {
31
+ func (s * CodingSession ) Pause (time time. Time ) {
30
32
if currentBuffer := s .bufStack .peek (); currentBuffer != nil {
31
33
currentBuffer .Close (time )
32
34
}
33
35
s .startStops = append (s .startStops , time )
34
36
}
35
37
36
38
// PauseTime is used to determine which coding session to resume.
37
- func (s * CodingSession ) PauseTime () int64 {
39
+ func (s * CodingSession ) PauseTime () time. Time {
38
40
if len (s .startStops ) == 0 {
39
- return 0
41
+ return time. Time {}
40
42
}
41
43
return s .startStops [len (s .startStops )- 1 ]
42
44
}
43
45
44
46
// Resume should be called when the editor regains focus.
45
- func (s * CodingSession ) Resume (time int64 ) {
47
+ func (s * CodingSession ) Resume (time time. Time ) {
46
48
if currentBuffer := s .bufStack .peek (); currentBuffer != nil {
47
49
currentBuffer .Open (time )
48
50
}
@@ -64,10 +66,10 @@ func (s *CodingSession) HasBuffers() bool {
64
66
}
65
67
66
68
// Duration returns the total duration of the coding session.
67
- func (s * CodingSession ) Duration () int64 {
68
- var duration int64
69
+ func (s * CodingSession ) Duration () time. Duration {
70
+ var duration time. Duration
69
71
for i := 0 ; i < len (s .startStops ); i += 2 {
70
- duration += s .startStops [i + 1 ] - s .startStops [i ]
72
+ duration += s .startStops [i + 1 ]. Sub ( s .startStops [i ])
71
73
}
72
74
return duration
73
75
}
@@ -79,7 +81,7 @@ func (s *CodingSession) Active() bool {
79
81
80
82
// End ends the active coding sessions. It sets the total duration in
81
83
// milliseconds, and turns the stack of buffers into a slice of files.
82
- func (s * CodingSession ) End (endedAt int64 ) Session {
84
+ func (s * CodingSession ) End (endedAt time. Time ) Session {
83
85
if currentBuffer := s .bufStack .peek (); currentBuffer != nil && currentBuffer .IsOpen () {
84
86
currentBuffer .Close (endedAt )
85
87
}
@@ -89,11 +91,11 @@ func (s *CodingSession) End(endedAt int64) Session {
89
91
}
90
92
91
93
return Session {
92
- StartedAt : s .StartedAt ,
93
- EndedAt : endedAt ,
94
- DurationMs : s .Duration (),
95
- OS : s .OS ,
96
- Editor : s .Editor ,
97
- Files : s .bufStack .files (),
94
+ StartedAt : s .StartedAt ,
95
+ EndedAt : endedAt ,
96
+ Duration : s .Duration (),
97
+ OS : s .OS ,
98
+ Editor : s .Editor ,
99
+ Files : s .bufStack .files (),
98
100
}
99
101
}
0 commit comments