Added per file tracking

This commit is contained in:
əlemi 2019-11-02 02:33:20 +01:00
parent b6a5636325
commit 1107b8460e
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 46 additions and 29 deletions

1
.combo/none.cmb Normal file
View file

@ -0,0 +1 @@
0

View file

@ -13,7 +13,9 @@ Each keystroke in insert mode increases your combo,
but not typing for 1 second resets it. but not typing for 1 second resets it.
The script is triggered whenever a text edit is detected in insert mode. The script is triggered whenever a text edit is detected in insert mode.
Best score is displayed next to the current combo in square brackets. Progress is tracked per filetype (and stored in ~/.vim/.combo/).
Best score is displayed next to the current combo, and in square brackets
the best score across all filetypes is shown.
When a new high score is achieved, it is saved inside .vim as .combo. When a new high score is achieved, it is saved inside .vim as .combo.
If you get errors at launch, check that .combo exists and has just a If you get errors at launch, check that .combo exists and has just a
number inside (should be 0, or last best combo). number inside (should be 0, or last best combo).
@ -22,8 +24,6 @@ By default, combo is not tracked in .txt, .tex, .vim files and in files with
no extension. This can be removed by deleting (or commenting) the first lines no extension. This can be removed by deleting (or commenting) the first lines
in the script. in the script.
The plugin is not in its final version. Right now, you will need to edit
the path to the .combo file and choose where to display the combo information.
By default, the section B of airline-vim is overwritten to display current By default, the section B of airline-vim is overwritten to display current
combo. A random Runic character has been chosen to indicate the combo field. combo. A random Runic character has been chosen to indicate the combo field.

View file

@ -1,32 +1,48 @@
" COMBO Counter " COMBO Counter
let g:disable_combo = 0 " Choose combo file depending on extension
" Combo counting is disabled in plain text, LaTeX, vim config and files if strlen(expand("%:e")) > 0
" without extension (usually configuration files) let g:combo_file = $HOME . '/.vim/.combo/' . expand("%:e") . ".cmb"
if expand("%:e") == "txt" || expand("%:e") == "tex" || expand("%:e") == "vim" || expand("%:e") == "" else
let g:disable_combo = 1 let g:combo_file = $HOME . '/.vim/.combo/none.cmb'
endif endif
if g:disable_combo == 1 " Find best score across all filetypes
let g:best_combo = readfile('$HOME/.vim/.combo') let scores = []
for f in split(globpath($HOME . '/.vim/.combo/', '*'), '\n')
let buf = readfile(f)
call insert(scores, buf[0])
endfor
let g:best_combo_all = max(scores)
" Find best score for current filetype
if filereadable(g:combo_file)
let g:best_combo = readfile(g:combo_file)
let g:best_combo = g:best_combo[0] let g:best_combo = g:best_combo[0]
let g:airline_section_b = 'ᛥ [%{g:best_combo}]'
else else
let g:combo_counter = 0 " The actual combo variable silent !echo 0 > $HOME/.vim/.combo/%:e.cmb
let g:best_combo = readfile('$HOME/.vim/.combo') " ~ is not expanded ??? let g:best_combo = 0
let g:best_combo = g:best_combo[0] " Reading from file returns a list, but an int is needed
let g:last_combo = reltime() " Set current time as last combo time
let g:airline_section_b = 'ᛥ %{g:combo_counter} [%{g:best_combo}]' " I use airline vim and inserted the combo meter in it
function! UpdateCombo()
if reltimefloat(reltime(g:last_combo)) > 1 " Timeout is 1 second
if g:combo_counter > g:best_combo " Before resetting combo counter, check if new high score
call writefile([g:combo_counter], "$HOME/.vim/.combo")
let g:best_combo = g:combo_counter
endif
let g:combo_counter = 1
else
let g:combo_counter +=1
endif
let g:last_combo = reltime()
endfunction
autocmd TextChangedI * call UpdateCombo() " Every time text in the file changes, call combo function
endif endif
" Configure variables
let g:combo_counter = 0 " The actual combo variable
let g:timeout = 1
let g:last_combo = reltime() " Set current time as last combo time
let g:airline_section_b = 'ᛥ %{g:combo_counter}|%{g:best_combo} [%{g:best_combo_all}]' " I use airline vim and inserted the combo meter in it
function! UpdateCombo()
if reltimefloat(reltime(g:last_combo)) > g:timeout " Timeout is 1 second
call SaveCombo()
let g:combo_counter = 1
else
let g:combo_counter +=1
endif
let g:last_combo = reltime()
endfunction
function! SaveCombo()
if g:combo_counter > g:best_combo
call writefile([g:combo_counter], g:combo_file)
let g:best_combo = g:combo_counter
endif
endfunction
autocmd TextChangedI * call UpdateCombo() " Every time the cursor moves, call combo function
autocmd InsertLeave * call SaveCombo()