1
2 Maintainer:peter@peterodding.com
3 Last Change:
4 URL:http://peterodding.com/code/vim/profile/autoload/xolox/timer.vim
5
6 if !exists('g:timer_enabled')
7 let g:timer_enabled = 0
8 endif
9
10 if !exists('g:timer_verbosity')
11 let g:timer_verbosity = 1
12 endif
13
14 let s:has_reltime = has('reltime')
15
16 function! xolox#timer#start()
17 if g:timer_enabled || &verbose >= g:timer_verbosity
18 return s:has_reltime ? reltime() : [localtime()]
19 endif
20 return []
21 endfunction
22
23 function! xolox#timer#stop(...)
24 if (g:timer_enabled || &verbose >= g:timer_verbosity)
25 call call('xolox#message', map(copy(a:000), 's:convert_value(v:val)'))
26 endif
27 endfunction
28
29 function! s:convert_value(value)
30 if type(a:value) != type([])
31 return a:value
32 elseif !empty(a:value)
33 if s:has_reltime
34 let ts = xolox#trim(reltimestr(reltime(a:value)))
35 else
36 let ts = localtime() - a:value[0]
37 endif
38 return xolox#timer#format_timespan(ts)
39 else
40 return '?'
41 endif
42 endfunction
43
44 function! xolox#timer#format_timespan(ts)
45
46
47 let seconds = a:ts + 0
48
49 reltime()
50 if seconds < 5
51 let extract = matchstr(a:ts, '^\d\+\(\.0*[123456789][123456789]\?\)\?')
52 if extract =~ '[123456789]'
53 return extract . ' second' . (extract != '1' ? 's' : '')
54 endif
55 endif
56
57
58 let result = []
59 for [name, size] in s:units
60 if seconds >= size
61 let counter = seconds / size
62 let seconds = seconds % size
63 let suffix = counter != 1 ? 's' : ''
64 call add(result, printf('%i %s%s', counter, name, suffix))
65 endif
66 endfor
67
68
69 if len(result) == 1
70 return result[0]
71 else
72 return join(result[0:-2], ', ') . ' and ' . result[-1]
73 endif
74
75 endfunction
76
77 let s:units = [['day', 60 * 60 * 24], ['hour', 60 * 60], ['minute', 60], ['second', 1]]
78
79