Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added sort and filter

  • Loading branch information...
commit 5ac3d0d219031077176669fdefc83a837b0a4469 1 parent 5ac238c
@tacahiroy authored
Showing with 92 additions and 31 deletions.
  1. +88 −24 autoload/edtime.vim
  2. +4 −7 plugin/edtime.vim
View
112 autoload/edtime.vim
@@ -13,6 +13,15 @@ function! s:curfile()
return expand('%:p')
endfunction
+function! s:shorten_path(s)
+ return substitute(a:s, '^'.$HOME, '~', '')
+endfunction
+
+function! s:expand_path(s)
+ " error E33 would occur if you forget to escape tilde
+ return substitute(a:s, '\~', $HOME, 'g')
+endfunction
+
function! s:divide(t)
let DAY = 60 * 24
let HOUR = 60
@@ -43,18 +52,9 @@ function! s:format_time(t)
endfunction
" }}}
-" Object " {{{
-let s:EdTime = {}
-let s:EdTime.files = {}
-let s:EdTime.db = ''
-let s:EdTime.summary = {}
-
-" NOTE: files that `accept_pattern` - `ignore_pattern` are managed
-" if both pattern are specified
-let s:EdTime.accept_pattern = get(g:, 'edtime_accept_pattern', '')
-let s:EdTime.ignore_pattern = get(g:, 'edtime_ignore_pattern', '')
-lockvar s:EdTime.accept_pattern
-lockvar s:EdTime.ignore_pattern
+" public " {{{
+function! edtime#complete(A, L, P)
+endfunction
function! edtime#new(f)
let obj = deepcopy(s:EdTime)
@@ -62,6 +62,10 @@ function! edtime#new(f)
call obj.load()
return obj
endfunction
+" }}}
+
+" Object " {{{
+let s:EdTime = {}
function! s:EdTime.start(f) dict
if self.is_ignored(a:f)
@@ -101,6 +105,10 @@ function! s:EdTime.reset(f) dict
let self.files[a:f].end = []
endfunction
+function! s:EdTime.get_total(k) dict
+ return self.has_file(a:k) ? self.files[a:k].total : 0
+endfunction
+
function! s:EdTime.remove(f) dict
call remove(self.files, a:f)
endfunction
@@ -144,40 +152,82 @@ function! s:EdTime.load() dict
endfunction
" TODO: sorting
-" TODO: display to buffer
+" TODO: display into a buffer
" TODO: omit file name if it's better
function! s:EdTime.show(...) dict
try
call self.stop(s:curfile())
- if !a:0 && self.is_ignored(s:curfile())
+ if a:0 == 0 && self.is_ignored(s:curfile())
return
endif
- let opts = ['all', '']
+ let opts = ['-a', '']
let opt = get(a:, '1', '')
- if index(opts, opt) == -1
- return
- endif
-
let files = {}
- if opt == 'all'
- let files = self.summary.files
+ if opt == '-a'
+ for [k, v] in items(self.summary.files)
+ let files[k] = {'total': self.summary.get_total(k)}
+ endfor
else
" show only current file
let files = {s:curfile(): self.files[s:curfile()]}
endif
- for [k, v] in items(files)
- let sum = self.summary.files[k].total
- echo printf('%s: %s (%s)', k, s:format_time(v.total), s:format_time(sum))
+ let sortedlist = self.sort(files)
+ if !self.is_display_zero
+ let sortedlist = self.filter(sortedlist)
+ endif
+
+ let i = 1
+ for [k, v] in sortedlist
+ let sum = s:format_time(self.summary.files[k].total)
+ let today = s:format_time(self.get_total(k))
+ echo printf('%3d: %s: %s (%s)', i, s:shorten_path(k), today, sum)
+ let i += 1
endfor
finally
call self.start(s:curfile())
endtry
endfunction
+function! s:EdTime.sort(files) dict
+ let list = []
+ for [k, v] in items(a:files)
+ call add(list, [k, v])
+ endfor
+
+ if empty(self.sort_func)
+ let self.sort_func = self.sort_by_edtime
+ endif
+
+ return sort(sort(list), self.sort_func, self)
+endfunction
+
+function! s:EdTime.filter(list)
+ return filter(a:list, '0 < v:val[1].total')
+endfunction
+
+function! s:EdTime.sort_by_edtime(a, b) dict
+ let a = a:a[1].total
+ let b = a:b[1].total
+ let r = 0
+
+ if a < b
+ let r = -1
+ elseif b < a
+ let r = 1
+ else
+ let r = 0
+ endif
+
+ return r * (self.sort_order_is_desc ? -1 : 1)
+endfunction
+
+function s:EdTime.sort_by_time_today(a, b)
+endfunction
+
" returns whether {f} is ignored or not
function! s:EdTime.is_ignored(f) dict
if empty(a:f)
@@ -210,6 +260,20 @@ function! s:EdTime.is_ignored(f) dict
return 0
endfunction
+
+let s:EdTime.files = {}
+let s:EdTime.db = ''
+let s:EdTime.summary = {}
+let s:EdTime.sort_func = ''
+let s:EdTime.sort_order_is_desc = 0
+
+" NOTE: files that `accept_pattern` - `ignore_pattern` are managed
+" if both pattern are specified
+let s:EdTime.accept_pattern = s:expand_path(get(g:, 'edtime_accept_pattern', ''))
+let s:EdTime.ignore_pattern = s:expand_path(get(g:, 'edtime_ignore_pattern', ''))
+let s:EdTime.is_display_zero = get(g:, 'edtime_is_display_zero', 0)
+let s:EdTime.sort_order_is_desc = get(g:, 'edtime_sort_order_is_desc', 1)
+let s:EdTime.sort_func = s:EdTime.sort_by_edtime
" }}}
View
11 plugin/edtime.vim
@@ -19,11 +19,11 @@ let s:saved_cpo = &cpo
set cpo&vim
-" " Utilities " {{{
+" Utilities " {{{
function! s:to_path(...)
return join(a:000, '/')
endfunction
-" " }}}
+" }}}
let s:data_dir = expand(get(g:, 'edtime_data_dir', '~/.edtime'))
@@ -38,11 +38,8 @@ let s:edt = edtime#new(s:to_path(s:data_dir, strftime('%Y%m%d.db')))
let s:edt.summary = edtime#new(s:to_path(s:data_dir, 'full.db'))
" Command
-function! s:complete_edtime(A, L, P)
- return ['all']
-endfunction
-command! -nargs=? -complete=customlist,s:complete_edtime
- \ EdTime call s:edt.show(<f-args>)
+command! -nargs=0 EdTime call s:edt.show()
+command! -nargs=0 EdTimeAll call s:edt.show('-a')
augroup EdTime
Please sign in to comment.
Something went wrong with that request. Please try again.