aboutsummaryrefslogtreecommitdiff
path: root/vim/bundle/tlib_vim/autoload/tlib/progressbar.vim
blob: e739eb3c74cbf8ccc0cc674e4663f5254e0d7580 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
" @Author:      Tom Link (mailto:micathom AT gmail com?subject=[vim])
" @Website:     http://www.vim.org/account/profile.php?user_id=4037
" @License:     GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Revision:    72

let s:statusline = []
let s:laststatus = []
let s:max = []
let s:format = []
let s:width = []
let s:value = []
let s:timestamp = -1

" EXAMPLE: >
"     call tlib#progressbar#Init(20)
"     try
"         for i in range(20)
"             call tlib#progressbar#Display(i)
"             call DoSomethingThatTakesSomeTime(i)
"         endfor
"     finally
"         call tlib#progressbar#Restore()
"     endtry
function! tlib#progressbar#Init(max, ...) "{{{3
    TVarArg ['format', '%s'], ['width', 10]
    call insert(s:statusline, &statusline)
    call insert(s:laststatus, &laststatus)
    call insert(s:max, a:max)
    call insert(s:format, format)
    call insert(s:width, width)
    call insert(s:value, -1)
    let &laststatus = 2
    let s:timestamp = localtime()
endf


function! tlib#progressbar#Display(value, ...) "{{{3
    TVarArg 'extra', ['always', 0]
    let ts = localtime()
    if !always && ts == s:timestamp
        return
    else
        let s:timestamp = ts
    endif
    let val = a:value * s:width[0] / s:max[0]
    if always || val != s:value[0]
        let s:value[0] = val
        let pbl = repeat('#', val)
        let pbr = repeat('.', s:width[0] - val)
        let txt = printf(s:format[0], '['.pbl.pbr.']') . extra
        let &l:statusline = txt
        " TLogDBG txt
        redrawstatus
        " redraw
        " call tlib#notify#Echo(txt)
    endif
endf


function! tlib#progressbar#Restore() "{{{3
    let &l:statusline = remove(s:statusline, 0)
    let &laststatus = remove(s:laststatus, 0)
    redrawstatus
    " redraw
    " echo
    call remove(s:max, 0)
    call remove(s:format, 0)
    call remove(s:width, 0)
    call remove(s:value, 0)
endf