Skip to content

Instantly share code, notes, and snippets.

@saevarb
Created February 14, 2014 16:47
Show Gist options
  • Save saevarb/9004594 to your computer and use it in GitHub Desktop.
Save saevarb/9004594 to your computer and use it in GitHub Desktop.
if !has('python')
echo "Error: Chronos needs vim compiled with python support."
finish
endif
python << EOF
import vim
import time
timerDict = {}
statsDict = {}
def getExtension(fileName):
splits = fileName.rsplit(".", 1)
if len(splits) < 2:
return None
else:
return splits[1]
class Chronos:
def startTimer(self):
curBuf = vim.current.buffer.name
if not curBuf:
return
curTime = time.clock()
timerDict[curBuf] = curTime
def stopTimer(self):
curBuf = vim.current.buffer.name
if not curBuf:
return
curExt = getExtension(curBuf)
if not curExt:
return
curTime = time.clock()
if curBuf not in timerDict:
return
startTime = timerDict[curBuf]
elapsed = curTime - startTime
if curExt in statsDict:
statsDict[curExt] += elapsed
else:
statsDict[curExt] = elapsed
chronos = Chronos()
print timerDict.items()
EOF
function! StartTimer()
py print "Started timer"
py chronos.startTimer()
endfunction
function! StopTimer()
py print "Stopped timer"
py chronos.stopTimer()
endfunction
augroup Chronos
autocmd!
autocmd Chronos FocusGained * call StartTimer()
autocmd Chronos FocusLost * call StopTimer()
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment