Skip to content

Commit

Permalink
New line additions and deletions params
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed Jan 24, 2024
1 parent 7ec63d7 commit cc360c9
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 17 deletions.
4 changes: 4 additions & 0 deletions cmd/heartbeat/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ func buildHeartbeats(params paramscmd.Params) []heartbeat.Heartbeat {
params.Heartbeat.IsWrite,
params.Heartbeat.Language,
params.Heartbeat.LanguageAlternate,
params.Heartbeat.LineAdditions,
params.Heartbeat.LineDeletions,
params.Heartbeat.LineNumber,
params.Heartbeat.LinesInFile,
params.Heartbeat.LocalFile,
Expand All @@ -210,6 +212,8 @@ func buildHeartbeats(params paramscmd.Params) []heartbeat.Heartbeat {
h.IsWrite,
h.Language,
h.LanguageAlternate,
h.LineAdditions,
h.LineDeletions,
h.LineNumber,
h.Lines,
h.LocalFile,
Expand Down
4 changes: 4 additions & 0 deletions cmd/offline/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func buildHeartbeats(params paramscmd.Params) []heartbeat.Heartbeat {
params.Heartbeat.IsWrite,
params.Heartbeat.Language,
params.Heartbeat.LanguageAlternate,
params.Heartbeat.LineAdditions,
params.Heartbeat.LineDeletions,
params.Heartbeat.LineNumber,
params.Heartbeat.LinesInFile,
params.Heartbeat.LocalFile,
Expand All @@ -115,6 +117,8 @@ func buildHeartbeats(params paramscmd.Params) []heartbeat.Heartbeat {
h.IsWrite,
h.Language,
h.LanguageAlternate,
h.LineAdditions,
h.LineDeletions,
h.LineNumber,
h.Lines,
h.LocalFile,
Expand Down
33 changes: 31 additions & 2 deletions cmd/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type (
IsWrite any `json:"is_write"`
Language *string `json:"language"`
LanguageAlternate string `json:"alternate_language"`
LineAdditions any `json:"line_additions"`
LineDeletions any `json:"line_deletions"`
LineNumber any `json:"lineno"`
Lines any `json:"lines"`
Project string `json:"project"`
Expand All @@ -106,6 +108,8 @@ type (
IsWrite *bool
Language *string
LanguageAlternate string
LineAdditions *int
LineDeletions *int
LineNumber *int
LinesInFile *int
LocalFile string
Expand Down Expand Up @@ -401,6 +405,16 @@ func LoadHeartbeatParams(v *viper.Viper) (Heartbeat, error) {
isWrite = heartbeat.PointerTo(b)
}

var lineAdditions *int
if num := v.GetInt("line-additions"); v.IsSet("line-additions") {
lineAdditions = heartbeat.PointerTo(num)
}

Check warning on line 411 in cmd/params/params.go

View check run for this annotation

Codecov / codecov/patch

cmd/params/params.go#L410-L411

Added lines #L410 - L411 were not covered by tests

var lineDeletions *int
if num := v.GetInt("line-deletions"); v.IsSet("line-deletions") {
lineDeletions = heartbeat.PointerTo(num)
}

Check warning on line 416 in cmd/params/params.go

View check run for this annotation

Codecov / codecov/patch

cmd/params/params.go#L415-L416

Added lines #L415 - L416 were not covered by tests

var lineNumber *int
if num := v.GetInt("lineno"); v.IsSet("lineno") {
lineNumber = heartbeat.PointerTo(num)
Expand Down Expand Up @@ -442,6 +456,8 @@ func LoadHeartbeatParams(v *viper.Viper) (Heartbeat, error) {
IsWrite: isWrite,
Language: language,
LanguageAlternate: vipertools.GetString(v, "alternate-language"),
LineAdditions: lineAdditions,
LineDeletions: lineDeletions,
LineNumber: lineNumber,
LinesInFile: linesInFile,
LocalFile: vipertools.GetString(v, "local-file"),
Expand Down Expand Up @@ -971,6 +987,16 @@ func (p Heartbeat) String() string {
language = *p.Language
}

var lineAdditions string
if p.LineAdditions != nil {
lineAdditions = strconv.Itoa(*p.LineAdditions)
}

var lineDeletions string
if p.LineDeletions != nil {
lineDeletions = strconv.Itoa(*p.LineDeletions)
}

var lineNumber string
if p.LineNumber != nil {
lineNumber = strconv.Itoa(*p.LineNumber)
Expand All @@ -984,8 +1010,9 @@ func (p Heartbeat) String() string {
return fmt.Sprintf(
"category: '%s', cursor position: '%s', entity: '%s', entity type: '%s',"+
" num extra heartbeats: %d, guess language: %t, is unsaved entity: %t,"+
" is write: %t, language: '%s', line number: '%s', lines in file: '%s',"+
" time: %.5f, filter params: (%s), project params: (%s), sanitize params: (%s)",
" is write: %t, language: '%s', line additions: '%s', line deletions: '%s',"+
" line number: '%s', lines in file: '%s', time: %.5f, filter params: (%s),"+
" project params: (%s), sanitize params: (%s)",
p.Category,
cursorPosition,
p.Entity,
Expand All @@ -995,6 +1022,8 @@ func (p Heartbeat) String() string {
p.IsUnsavedEntity,
isWrite,
language,
lineAdditions,
lineDeletions,
lineNumber,
linesInFile,
p.Time,
Expand Down
7 changes: 5 additions & 2 deletions cmd/params/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,8 @@ func TestHeartbeat_String(t *testing.T) {
IsUnsavedEntity: true,
IsWrite: heartbeat.PointerTo(true),
Language: heartbeat.PointerTo("Golang"),
LineAdditions: heartbeat.PointerTo(123),
LineDeletions: heartbeat.PointerTo(456),
LineNumber: heartbeat.PointerTo(4),
LinesInFile: heartbeat.PointerTo(56),
Time: 1585598059,
Expand All @@ -2470,8 +2472,9 @@ func TestHeartbeat_String(t *testing.T) {
t,
"category: 'coding', cursor position: '15', entity: 'path/to/entity.go', entity type: 'file',"+
" num extra heartbeats: 3, guess language: true, is unsaved entity: true, is write: true,"+
" language: 'Golang', line number: '4', lines in file: '56', time: 1585598059.00000, filter"+
" params: (exclude: '[]', exclude unknown project: false, include: '[]', include only with"+
" language: 'Golang', line additions: '123', line deletions: '456', line number: '4',"+
" lines in file: '56', time: 1585598059.00000, filter params: (exclude: '[]',"+
" exclude unknown project: false, include: '[]', include only with"+
" project file: false), project params: (alternate: '', branch alternate: '', map patterns:"+
" '[]', override: '', git submodules disabled: '[]', git submodule project map: '[]'), sanitize"+
" params: (hide branch names: '[]', hide project folder: false, hide file names: '[]',"+
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func setFlags(cmd *cobra.Command, v *viper.Viper) {
0,
"Optional lines in the file. Normally, this is detected automatically but"+
" can be provided manually for performance, accuracy, or when using --local-file.")
flags.Int("line-additions", 0, "Optional number of lines added since last heartbeat in the current file.")
flags.Int("line-deletions", 0, "Optional number of lines deleted since last heartbeat in the current file.")

Check warning on line 157 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L156-L157

Added lines #L156 - L157 were not covered by tests
flags.String(
"local-file",
"",
Expand Down
6 changes: 6 additions & 0 deletions pkg/heartbeat/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Heartbeat struct {
IsWrite *bool `json:"is_write,omitempty"`
Language *string `json:"language,omitempty"`
LanguageAlternate string `json:"-"`
LineAdditions *int `json:"line_additions,omitempty"`
LineDeletions *int `json:"line_deletions,omitempty"`
LineNumber *int `json:"lineno,omitempty"`
Lines *int `json:"lines,omitempty"`
LocalFile string `json:"-"`
Expand Down Expand Up @@ -58,6 +60,8 @@ func New(
isWrite *bool,
language *string,
languageAlternate string,
lineAdditions *int,
lineDeletions *int,
lineNumber *int,
lines *int,
localFile string,
Expand All @@ -78,6 +82,8 @@ func New(
IsWrite: isWrite,
Language: language,
LanguageAlternate: languageAlternate,
LineAdditions: lineAdditions,
LineDeletions: lineDeletions,
LineNumber: lineNumber,
Lines: lines,
LocalFile: localFile,
Expand Down
6 changes: 6 additions & 0 deletions pkg/heartbeat/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func TestNew(t *testing.T) {
heartbeat.PointerTo(true),
heartbeat.PointerTo("Go"),
"Golang",
heartbeat.PointerTo(2),
heartbeat.PointerTo(3),
heartbeat.PointerTo(42),
nil,
"/path/to/file",
Expand All @@ -50,6 +52,8 @@ func TestNew(t *testing.T) {
IsWrite: heartbeat.PointerTo(true),
Language: heartbeat.PointerTo("Go"),
LanguageAlternate: "Golang",
LineAdditions: heartbeat.PointerTo(2),
LineDeletions: heartbeat.PointerTo(3),
LineNumber: heartbeat.PointerTo(42),
LocalFile: "/path/to/file",
ProjectAlternate: "billing",
Expand Down Expand Up @@ -94,6 +98,8 @@ func TestHeartbeat_JSON(t *testing.T) {
EntityType: heartbeat.FileType,
IsWrite: heartbeat.PointerTo(true),
Language: heartbeat.PointerTo("Go"),
LineAdditions: heartbeat.PointerTo(123),
LineDeletions: heartbeat.PointerTo(456),
LineNumber: heartbeat.PointerTo(42),
Lines: heartbeat.PointerTo(100),
Project: heartbeat.PointerTo("wakatime"),
Expand Down
28 changes: 15 additions & 13 deletions pkg/heartbeat/testdata/heartbeat.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"branch": "heartbeat",
"category": "coding",
"cursorpos": 12,
"dependencies": ["dep1", "dep2"],
"entity": "/tmp/main.go",
"is_write": true,
"language": "Go",
"lineno": 42,
"lines": 100,
"project": "wakatime",
"type": "file",
"time": 1585598060.1,
"user_agent": "wakatime/13.0.7"
"branch": "heartbeat",
"category": "coding",
"cursorpos": 12,
"dependencies": ["dep1", "dep2"],
"entity": "/tmp/main.go",
"is_write": true,
"language": "Go",
"lineno": 42,
"lines": 100,
"line_additions": 123,
"line_deletions": 456,
"project": "wakatime",
"type": "file",
"time": 1585598060.1,
"user_agent": "wakatime/13.0.7"
}

0 comments on commit cc360c9

Please sign in to comment.