check for api key in config file to fix #23
This commit is contained in:
parent
3218aabd97
commit
ec353d8fe6
1 changed files with 54 additions and 44 deletions
|
@ -27,17 +27,24 @@ let s:VERSION = '3.0.9'
|
||||||
let s:old_cpo = &cpo
|
let s:old_cpo = &cpo
|
||||||
set cpo&vim
|
set cpo&vim
|
||||||
|
|
||||||
" To be backwards compatible, rename config file
|
" Globals
|
||||||
|
let s:plugin_directory = expand("<sfile>:p:h") . '/'
|
||||||
|
let s:config_file = expand("$HOME/.wakatime.cfg")
|
||||||
|
let s:config_file_already_setup = 0
|
||||||
|
|
||||||
|
" For backwards compatibility, rename wakatime.conf to wakatime.cfg
|
||||||
|
if !filereadable(s:config_file)
|
||||||
if filereadable(expand("$HOME/.wakatime"))
|
if filereadable(expand("$HOME/.wakatime"))
|
||||||
exec "silent !mv" expand("$HOME/.wakatime") expand("$HOME/.wakatime.conf")
|
exec "silent !mv" expand("$HOME/.wakatime") expand("$HOME/.wakatime.conf")
|
||||||
endif
|
endif
|
||||||
if filereadable(expand("$HOME/.wakatime.conf"))
|
if filereadable(expand("$HOME/.wakatime.conf"))
|
||||||
if !filereadable(expand("$HOME/.wakatime.cfg"))
|
if !filereadable(s:config_file)
|
||||||
let contents = ['[settings]'] + readfile(expand("$HOME/.wakatime.conf"), '')
|
let contents = ['[settings]'] + readfile(expand("$HOME/.wakatime.conf"), '')
|
||||||
call writefile(contents, expand("$HOME/.wakatime.cfg"))
|
call writefile(contents, s:config_file)
|
||||||
call delete(expand("$HOME/.wakatime.conf"))
|
call delete(expand("$HOME/.wakatime.conf"))
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
" Set default python binary location
|
" Set default python binary location
|
||||||
if !exists("g:wakatime_PythonBinary")
|
if !exists("g:wakatime_PythonBinary")
|
||||||
|
@ -49,27 +56,45 @@ let s:VERSION = '3.0.9'
|
||||||
let g:wakatime_HeartbeatFrequency = 2
|
let g:wakatime_HeartbeatFrequency = 2
|
||||||
endif
|
endif
|
||||||
|
|
||||||
" Globals
|
|
||||||
let s:plugin_directory = expand("<sfile>:p:h") . '/'
|
|
||||||
let s:config_file_exists = 0
|
|
||||||
|
|
||||||
" }}}
|
" }}}
|
||||||
|
|
||||||
|
|
||||||
" Function Definitions {{{
|
" Function Definitions {{{
|
||||||
|
|
||||||
|
function! s:StripWhitespace(str)
|
||||||
|
return substitute(a:str, '^\s*\(.\{-}\)\s*$', '\1', '')
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! s:SetupConfigFile()
|
function! s:SetupConfigFile()
|
||||||
if !s:config_file_exists
|
if !s:config_file_already_setup
|
||||||
|
|
||||||
" Create config file if does not exist
|
" Create config file if does not exist
|
||||||
if !filereadable(expand("$HOME/.wakatime.cfg"))
|
if !filereadable(s:config_file)
|
||||||
let key = input("[WakaTime] Enter your wakatime.com api key: ")
|
let key = input("[WakaTime] Enter your wakatime.com api key: ")
|
||||||
if key != ''
|
if key != ''
|
||||||
call writefile(['[settings]', 'debug = false', printf("api_key = %s", key), 'hidefilenames = false', 'ignore =', ' ^COMMIT_EDITMSG$', ' ^TAG_EDITMSG$'], expand("$HOME/.wakatime.cfg"))
|
call writefile(['[settings]', 'debug = false', printf("api_key = %s", key), 'hidefilenames = false', 'ignore =', ' COMMIT_EDITMSG$', ' TAG_EDITMSG$'], s:config_file)
|
||||||
|
endif
|
||||||
|
|
||||||
|
" Make sure config file has api_key
|
||||||
|
else
|
||||||
|
let found_api_key = 0
|
||||||
|
let lines = readfile(s:config_file)
|
||||||
|
for line in lines
|
||||||
|
let group = split(line, '=')
|
||||||
|
if len(group) == 2 && s:StripWhitespace(group[0]) == 'api_key' && s:StripWhitespace(group[1]) != ''
|
||||||
|
let found_api_key = 1
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
if !found_api_key
|
||||||
|
let key = input("[WakaTime] Enter your wakatime.com api key: ")
|
||||||
|
let lines = lines + [join(['api_key', key], '=')]
|
||||||
|
call writefile(lines, s:config_file)
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
let s:config_file_already_setup = 1
|
||||||
echo "[WakaTime] Setup complete! Visit http://wakatime.com to view your logged time."
|
echo "[WakaTime] Setup complete! Visit http://wakatime.com to view your logged time."
|
||||||
endif
|
endif
|
||||||
endif
|
|
||||||
let s:config_file_exists = 1
|
|
||||||
endif
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:GetCurrentFile()
|
function! s:GetCurrentFile()
|
||||||
|
@ -100,11 +125,11 @@ let s:VERSION = '3.0.9'
|
||||||
else
|
else
|
||||||
exec 'silent !' . join(cmd, ' ') . ' &'
|
exec 'silent !' . join(cmd, ' ') . ' &'
|
||||||
endif
|
endif
|
||||||
call s:SetLastAction(a:time, a:time, targetFile)
|
call s:SetLastHeartbeat(a:time, a:time, targetFile)
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:GetLastAction()
|
function! s:GetLastHeartbeat()
|
||||||
if !filereadable(expand("$HOME/.wakatime.data"))
|
if !filereadable(expand("$HOME/.wakatime.data"))
|
||||||
return [0, 0, '']
|
return [0, 0, '']
|
||||||
endif
|
endif
|
||||||
|
@ -115,18 +140,10 @@ let s:VERSION = '3.0.9'
|
||||||
return last
|
return last
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:SetLastAction(time, last_update, targetFile)
|
function! s:SetLastHeartbeat(time, last_update, targetFile)
|
||||||
call writefile([substitute(printf('%d', a:time), ',', '.', ''), substitute(printf('%d', a:last_update), ',', '.', ''), a:targetFile], expand("$HOME/.wakatime.data"))
|
call writefile([substitute(printf('%d', a:time), ',', '.', ''), substitute(printf('%d', a:last_update), ',', '.', ''), a:targetFile], expand("$HOME/.wakatime.data"))
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:GetChar()
|
|
||||||
let c = getchar()
|
|
||||||
if c =~ '^\d\+$'
|
|
||||||
let c = nr2char(c)
|
|
||||||
endif
|
|
||||||
return c
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! s:EnoughTimePassed(now, last)
|
function! s:EnoughTimePassed(now, last)
|
||||||
let prev = a:last[0]
|
let prev = a:last[0]
|
||||||
if a:now - prev > g:wakatime_HeartbeatFrequency * 60
|
if a:now - prev > g:wakatime_HeartbeatFrequency * 60
|
||||||
|
@ -140,23 +157,16 @@ let s:VERSION = '3.0.9'
|
||||||
|
|
||||||
" Event Handlers {{{
|
" Event Handlers {{{
|
||||||
|
|
||||||
function! s:normalAction()
|
function! s:handleActivity(is_write)
|
||||||
call s:SetupConfigFile()
|
call s:SetupConfigFile()
|
||||||
let targetFile = s:GetCurrentFile()
|
let targetFile = s:GetCurrentFile()
|
||||||
let now = localtime()
|
let now = localtime()
|
||||||
let last = s:GetLastAction()
|
let last = s:GetLastHeartbeat()
|
||||||
if s:EnoughTimePassed(now, last) || targetFile != last[2]
|
if a:is_write || s:EnoughTimePassed(now, last) || targetFile != last[2]
|
||||||
call s:Api(targetFile, now, 0, last)
|
call s:Api(targetFile, now, a:is_write, last)
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:writeAction()
|
|
||||||
let targetFile = s:GetCurrentFile()
|
|
||||||
let now = localtime()
|
|
||||||
let last = s:GetLastAction()
|
|
||||||
call s:Api(targetFile, now, 1, last)
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
" }}}
|
" }}}
|
||||||
|
|
||||||
|
|
||||||
|
@ -164,10 +174,10 @@ let s:VERSION = '3.0.9'
|
||||||
|
|
||||||
augroup Wakatime
|
augroup Wakatime
|
||||||
autocmd!
|
autocmd!
|
||||||
autocmd BufEnter * call s:normalAction()
|
autocmd BufEnter * call s:handleActivity(0)
|
||||||
autocmd VimEnter * call s:normalAction()
|
autocmd VimEnter * call s:handleActivity(0)
|
||||||
autocmd BufWritePost * call s:writeAction()
|
autocmd BufWritePost * call s:handleActivity(1)
|
||||||
autocmd CursorMoved,CursorMovedI * call s:normalAction()
|
autocmd CursorMoved,CursorMovedI * call s:handleActivity(0)
|
||||||
augroup END
|
augroup END
|
||||||
|
|
||||||
" }}}
|
" }}}
|
||||||
|
|
Loading…
Reference in a new issue