Skip to content
21 changes: 20 additions & 1 deletion plugin/activitywatch.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ let s:file = ''
let s:language = ''
let s:project = ''

let s:last_branch_update = 0
let s:is_changed_branch = 0
let s:branch = ''

let s:connected = 0
let s:apiurl_host = get(g:, 'aw_apiurl_host', '127.0.0.1')
let s:apiurl_port = get(g:, 'aw_apiurl_port', '5600')
Expand Down Expand Up @@ -105,6 +109,17 @@ function! s:CreateBucket()
call HTTPPostJson(s:bucket_apiurl, l:body)
endfunc

function! s:RefreshGitBranch(localtime)
if a:localtime - s:last_branch_update > 5
let s:last_branch_update = a:localtime
let l:cmd_result = systemlist('git branch --show-current')[0]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I said before, systemlist is a synchronous operation and needs to be replaced with something asynchronous such as jobstart/job_start.

let l:current_branch = (cmd_result =~ '^fatal: ') ? 'N/A' : cmd_result
let s:is_changed_branch = l:current_branch == s:branch ? 0 : 1
let s:branch = l:current_branch
endif
"echo printf('branch %d is_changed_branch %s', s:branch, s:is_changed_branch)
endfunc

function! s:Heartbeat()
" Only send heartbeats if we can connect to aw-server
if s:connected < 1
Expand All @@ -116,11 +131,13 @@ function! s:Heartbeat()
let l:file = expand('%p')
let l:language = &filetype
let l:project = getcwd()
call s:RefreshGitBranch(l:localtime)
" Only send heartbeat if data was changed or more than 1 second has passed
" since last heartbeat
if s:file != l:file ||
\ s:language != l:language ||
\ s:project != l:project ||
\ s:is_changed_branch == 1 ||
\ l:localtime - s:last_heartbeat > 1

let l:req_body = {
Expand All @@ -129,7 +146,8 @@ function! s:Heartbeat()
\ 'data': {
\ 'file': l:file,
\ 'language': l:language,
\ 'project': l:project
\ 'project': l:project,
\ 'branch': s:branch
\ }
\}
call HTTPPostJson(s:heartbeat_apiurl, l:req_body)
Expand All @@ -142,6 +160,7 @@ endfunc

function! AWStart()
call s:CreateBucket()
call s:RefreshGitBranch(localtime())
endfunc

function! AWStop()
Expand Down