Cache statusline contents in Python

Each window is now tagged with an UUID and this UUID is used to cache
the window's statusline contents in the Python VimRenderer in order to
avoid translating the statusline contents to and from a vimdict
unnecessarily.

Refs #11.
This commit is contained in:
Kim Silkebækken 2012-12-15 18:48:12 +01:00
parent da6367a897
commit 0bf23adbcc
2 changed files with 14 additions and 11 deletions

View File

@ -7,6 +7,7 @@
python import sys, vim, os
python sys.path.append(vim.eval('expand("<sfile>:h:h:h")'))
python import uuid
python from powerline.core import Powerline
python pl = Powerline('vim')
@ -24,16 +25,16 @@ function! Powerline(winnr)
endfunction
function! s:UpdateAllWindows()
for w in range(1, winnr('$'))
" getwinvar() returns empty string for undefined variables.
" Use has_key(getwinvar(w, ''), 'powerline') if you care about variable
" being really defined (currently with w:powerline=='' it will throw
" E706: variable type mismatch).
if getwinvar(w, 'powerline') is# ''
call setwinvar(w, 'powerline', {})
for winnr in range(1, winnr('$'))
" getwinvar() returns empty string for undefined variables. Use
" has_key(getwinvar(winnr, ''), 'window_id') if you care about
" variable being really defined (currently with w:window_id=='' it
" will throw E706: variable type mismatch).
if getwinvar(winnr, 'window_id') is# ''
call setwinvar(winnr, 'window_id', s:pyeval('str(uuid.uuid4())'))
endif
call setwinvar(w, '&statusline', '%!Powerline('. w .')')
call setwinvar(winnr, '&statusline', '%!Powerline('. winnr .')')
endfor
endfunction

View File

@ -19,6 +19,7 @@ class VimRenderer(Renderer):
def __init__(self, *args, **kwargs):
super(VimRenderer, self).__init__(*args, **kwargs)
self.hl_groups = {}
self.window_cache = {}
def render(self, winnr):
'''Render all segments.
@ -28,17 +29,18 @@ class VimRenderer(Renderer):
used in non-current windows.
'''
current = vim_getwinvar(winnr, 'current')
window_id = vim_getwinvar(winnr, 'window_id')
winwidth = vim_winwidth(winnr)
if current:
mode = vim_mode()
contents_override = None
contents_cached = {segment['key']: segment['contents'] for segment in self.segments if segment['type'] == 'function'}
vim_setwinvar(winnr, 'powerline', contents_cached)
self.window_cache[window_id] = contents_cached
else:
mode = 'nc'
contents_cached = vim_getwinvar(winnr, 'powerline')
contents_override = {k: contents_cached[k].decode('utf-8') for k in contents_cached.keys()}
contents_override = self.window_cache.get(window_id)
statusline = super(VimRenderer, self).render(mode, width=winwidth, contents_override=contents_override)
statusline = statusline.replace(self.PERCENT_PLACEHOLDER, '%%')