Skip to content

Commit

Permalink
Rework buffers provider
Browse files Browse the repository at this point in the history
Close #8
Close #59
  • Loading branch information
liuchengxu committed Oct 19, 2019
1 parent 6790852 commit 0398f67
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
18 changes: 18 additions & 0 deletions autoload/clap/icon.vim
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ function! clap#icon#get(pattern) abort
endif
endfunction

function! s:get_or_default(k) abort
return get(g:clap#icon#extensions, a:k, g:clap#icon#default)
endfunction

function! clap#icon#for(bufname) abort
let ft = getbufvar(a:bufname, '&ft')
if empty(ft)
let ext = fnamemodify(expand(a:bufname), ':e')
if empty(ext)
return g:clap#icon#default
else
return s:get_or_default(ext)
endif
else
return s:get_or_default(ft)
return
endfunction

function! clap#icon#get_all() abort
let extensions = values(g:clap#icon#extensions)
let exact_matches = values(g:clap#icon#exact_matches)
Expand Down
39 changes: 34 additions & 5 deletions autoload/clap/provider/buffers.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,47 @@
let s:save_cpo = &cpo
set cpo&vim

" TODO more fancy buffers, e.g., append icon.
function! s:padding(origin, target_width) abort
let width = strdisplaywidth(a:origin)
if width < a:target_width
return a:origin.repeat(' ', a:target_width - width)
else
return a:origin
endif
endfunction

function! s:format_buffer(b)
let name = bufname(a:b)
let name = empty(name) ? '[No Name]' : fnamemodify(name, ":p:~:.")
let flag = a:b == bufnr('') ? '%' : (a:b == bufnr('#') ? '#' : ' ')
let modified = getbufvar(a:b, '&modified') ? ' [+]' : ''
let readonly = getbufvar(a:b, '&modifiable') ? '' : ' [RO]'

let bp = s:padding('['.a:b.']', 5)
let fsize = s:padding(clap#util#getfsize(name), 6)
let icon = s:padding(clap#icon#for(name), 3)
let extra = join(filter([modified, readonly], '!empty(v:val)'), '')
let line = get(s:line_info, a:b, '')

return trim(printf("%s %s %s %s %s %s %s", bp, fsize, icon, flag, name, extra, line))
endfunction

function! s:buffers() abort
redir => l:buffers
silent buffers
redir END
let s:buffers_cache = split(l:buffers, "\n")
return s:buffers_cache
let s:line_info = {}
for line in split(l:buffers, "\n")
let bufnr = str2nr(trim(matchstr(line, '^\s*\d\+')))
let lnum = matchstr(line, 'line.*$')
let s:line_info[bufnr] = lnum
endfor
return map(clap#util#buflisted_sorted(), "s:format_buffer(str2nr(v:val))")
endfunction

function! s:buffers_sink(selected) abort
call win_gotoid(bufwinid(g:clap.start.bufnr))
let b = split(a:selected)[0]
call g:clap.start.goto_win()
let b = matchstr(a:selected, '^\[\zs\d\+\ze\]')
execute 'buffer' b
endfunction

Expand Down
17 changes: 17 additions & 0 deletions autoload/clap/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,22 @@ function! clap#util#expand(args) abort
return a:args
endfunction

function! clap#util#getfsize(fname) abort
let l:size = getfsize(a:fname)
if l:size == 0 || l:size == -1 || l:size == -2
return ''
endif
if l:size < 1024
let size = l:size.'B'
elseif l:size < 1024*1024
let size = printf('%.1f', l:size/1024.0) . 'K'
elseif l:size < 1024*1024*1024
let size = printf('%.1f', l:size/1024.0/1024.0) . 'M'
else
let size = printf('%.1f', l:size/1024.0/1024.0/1024.0) . 'G'
endif
return size
endfunction

let &cpo = s:save_cpo
unlet s:save_cpo

0 comments on commit 0398f67

Please sign in to comment.