Skip to content

Commit d600cf3

Browse files
committed
Added argument parsing.
1 parent df87157 commit d600cf3

File tree

2 files changed

+72
-31
lines changed

2 files changed

+72
-31
lines changed

autoload/startuptime.vim

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
let s:sourced_script_type = 0
22
let s:other_lines_type = 1
33

4+
" *************************************************
5+
" * Globals
6+
" *************************************************
7+
48
" 's:tfields' contains the time fields.
59
let s:tfields = ['elapsed', 'self+sourced', 'self']
610

@@ -23,6 +27,10 @@ function! s:ColBounds()
2327
endfunction
2428
let s:col_bounds = s:ColBounds()
2529

30+
" *************************************************
31+
" * Utils
32+
" *************************************************
33+
2634
function! s:Contains(list, element)
2735
return index(a:list, a:element) !=# -1
2836
endfunction
@@ -91,6 +99,10 @@ function! s:Echo(echo_list)
9199
echohl None
92100
endfunction
93101

102+
" *************************************************
103+
" * Core
104+
" *************************************************
105+
94106
function! s:SetFile()
95107
try
96108
silent file [startuptime]
@@ -161,8 +173,8 @@ function! s:Profile(callback, tries, file)
161173
\ 'exit_cb': function(l:tmp.exit, l:env),
162174
\ 'hidden': 1
163175
\ }
164-
" XXX: A new buffer is created each time this is run. Running many times will
165-
" result in large buffer numbers.
176+
" XXX: A new buffer is created each time this is run. Running many times
177+
" will result in large buffer numbers.
166178
let l:env.bufnr = term_start(l:command, l:options)
167179
call term_sendkeys(l:env.bufnr, l:exit_keys)
168180
endif
@@ -171,7 +183,7 @@ endfunction
171183
" Returns a nested list. The top-level list entries correspond to different
172184
" profiling sessions. The next level lists contain the parsed lines for each
173185
" profiling session. Each line is represented with a dict.
174-
function! s:Extract(file)
186+
function! s:Extract(file, options)
175187
let l:result = []
176188
let l:lines = readfile(a:file)
177189
for l:line in l:lines
@@ -195,7 +207,16 @@ function! s:Extract(file)
195207
else
196208
let l:item.elapsed = str2float(l:times[1])
197209
endif
198-
call add(l:result[-1], l:item)
210+
let l:types = []
211+
if a:options.sourced_events
212+
call add(l:types, s:sourced_script_type)
213+
endif
214+
if a:options.other_events
215+
call add(l:types, s:other_lines_type)
216+
endif
217+
if s:Contains(l:types, l:item.type)
218+
call add(l:result[-1], l:item)
219+
endif
199220
endfor
200221
return l:result
201222
endfunction
@@ -447,12 +468,9 @@ function! startuptime#Main(file, winid, bufnr, options)
447468
if winbufnr(a:winid) !=# a:bufnr | return | endif
448469
call win_gotoid(a:winid)
449470
normal! ggdG
450-
let l:items = s:Extract(a:file)
471+
let l:items = s:Extract(a:file, a:options)
451472
let l:items = s:Consolidate(l:items)
452473
let l:items = s:Augment(l:items, a:options)
453-
if !a:options.all
454-
call filter(l:items, 'v:val.type !=# s:other_lines_type')
455-
endif
456474
let l:Compare = {i1, i2 -> i1.time ==# i2.time ? 0 : (i1.time <# i2.time ? 1 : -1)}
457475
if a:options.sort
458476
call sort(l:items, l:Compare)
@@ -498,29 +516,45 @@ function! s:New(mods)
498516
return 1
499517
endfunction
500518

501-
" Usage:
502-
" :StartupTime [--nosort] [--all] [--self] [--tries INT]
503-
function! startuptime#StartupTime(mods, ...)
504-
" TODO: implement this with a loop
505-
" TODO: throw error for unknown options
519+
function! s:Options(args)
506520
let l:options = {
507-
\ 'sort': 1,
508-
\ 'all': 0,
509-
\ 'self': 0,
510-
\ 'tries': 1
521+
\ 'sort': g:startuptime_sort,
522+
\ 'tries': g:startuptime_tries,
523+
\ 'sourced_events': g:startuptime_sourced_events,
524+
\ 'other_events': g:startuptime_other_events,
525+
\ 'self': g:startuptime_self
511526
\ }
512-
let l:mods = split(a:mods)
513-
let l:args = a:000
514-
let l:options.sort = !s:Contains(l:args, '--nosort')
515-
let l:options.all = s:Contains(l:args, '--all')
516-
let l:options.self = s:Contains(l:args, '--self')
517-
try
518-
let l:_tries = str2nr(l:args[index(l:args, '--tries') + 1])
519-
if l:_tries ># l:options.tries
520-
let l:options.tries = l:_tries
527+
let l:idx = 0
528+
while l:idx <# len(a:args)
529+
let l:arg = a:args[l:idx]
530+
if l:arg ==# '--sort' || l:arg ==# '--no-sort'
531+
let l:options.sort = l:arg ==# '--sort'
532+
elseif l:arg ==# '--tries'
533+
let l:idx += 1
534+
let l:arg = a:args[l:idx]
535+
let l:options.tries = str2nr(l:arg)
536+
elseif l:arg ==# '--sourced-events' || l:arg ==# '--no-sourced-events'
537+
let l:options.sourced_events = l:arg ==# '--sourced-events'
538+
elseif l:arg ==# '--other-events' || l:arg ==# '--no-other-events'
539+
let l:options.other_events = l:arg ==# '--other-events'
540+
else
541+
throw 'vim-startuptime: unknown argument (' . l:arg . ')'
521542
endif
522-
catch
523-
endtry
543+
let l:idx += 1
544+
endwhile
545+
return l:options
546+
endfunction
547+
548+
" Usage:
549+
" :StartupTime
550+
" \ [--sort] [--nosort]
551+
" \ [--sourced-events] [--no-sourced-events]
552+
" \ [--other-events] [--no-other-events]
553+
" \ [--self] [--no-self]
554+
" \ [--tries INT]
555+
function! startuptime#StartupTime(mods, ...)
556+
let l:mods = split(a:mods)
557+
let l:options = s:Options(a:000)
524558
if !s:New(l:mods)
525559
throw 'vim-startuptime: couldn''t create new buffer'
526560
endif

plugin/startuptime.vim

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ if !exists(':StartupTime')
1111
\ :call startuptime#StartupTime(<q-mods>, <f-args>)
1212
endif
1313

14-
" ************************************************************
14+
" *************************************************
1515
" * User Configuration
16-
" ************************************************************
16+
" *************************************************
1717

1818
let g:startuptime_more_info_key_seq =
1919
\ get(g:, 'startuptime_more_info_key_seq', '<space>')
@@ -23,6 +23,12 @@ let g:startuptime_exe_path =
2323
let g:startuptime_exe_args =
2424
\ get(g:, 'startuptime_exe_args', [])
2525

26+
let g:startuptime_sort = get(g:, 'startuptime_sort', 1)
27+
let g:startuptime_tries = get(g:, 'startuptime_tries', 1)
28+
let g:startuptime_sourced_events = get(g:, 'startuptime_sourced_events', 1)
29+
let g:startuptime_other_events = get(g:, 'startuptime_other_events', 1)
30+
let g:startuptime_self = get(g:, 'startuptime_self', 0)
31+
2632
let g:startuptime_event_width =
2733
\ get(g:, 'startuptime_event_width', 20)
2834
let g:startuptime_time_width =
@@ -41,7 +47,8 @@ let g:startuptime_colorize =
4147
" E.g., the following will use the Title highlight for sourcing event text.
4248
" :highlight link StartupTimeSourcingEvent Title
4349
" E.g., the following will use custom highlight colors for event times.
44-
" :highlight StartupTimeTime term=bold ctermfg=12 ctermbg=159 guifg=Blue guibg=LightCyan
50+
" :highlight StartupTimeTime
51+
" \ term=bold ctermfg=12 ctermbg=159 guifg=Blue guibg=LightCyan
4552
highlight default link StartupTimeHeader ModeMsg
4653
highlight default link StartupTimeSourcingEvent Type
4754
highlight default link StartupTimeOtherEvent Identifier

0 commit comments

Comments
 (0)