Small fixes for new position segment

- use vim_funcs
- use a dictionary instead of a tuple for translation strings
This commit is contained in:
EinfachToll 2014-01-21 15:48:05 +01:00
parent 45e4650d5c
commit 90132fe386
3 changed files with 19 additions and 13 deletions

View File

@ -23,6 +23,7 @@ vim_funcs = {
'expand': vim_get_func('expand', rettype=str),
'bufnr': vim_get_func('bufnr', rettype=int),
'line2byte': vim_get_func('line2byte', rettype=int),
'line': vim_get_func('line', rettype=int),
}
vim_modes = {
@ -266,11 +267,11 @@ def line_percent(pl, segment_info, gradient=False):
@window_cached
def position(pl, position_strings=('Top', 'Bot', 'All'), gradient=False):
def position(pl, position_strings={'top':'Top', 'bottom':'Bot', 'all':'All'}, gradient=False):
'''Return the position of the current view in the file as a percentage.
:param tuple position_strings:
use these three strings to indicate whether we are at top or bottom or see the complete file
:param dict position_strings:
dict for translation of the position strings, e.g. ``{"top":"Oben", "bottom":"Unten", "all":"Alles"}``
:param bool gradient:
highlight the percentage with a color gradient (by default a green to red gradient)
@ -279,17 +280,17 @@ def position(pl, position_strings=('Top', 'Bot', 'All'), gradient=False):
'''
line_last = len(vim.current.buffer)
winline_first = int(vim.eval('line("w0")'))
winline_last = int(vim.eval('line("w$")'))
winline_first = vim_funcs['line']('w0')
winline_last = vim_funcs['line']('w$')
if winline_first == 1 and winline_last == line_last:
percentage = 0.0
content = position_strings[2]
content = position_strings['all']
elif winline_first == 1:
percentage = 0.0
content = position_strings[0]
content = position_strings['top']
elif winline_last == line_last:
percentage = 100.0
content = position_strings[1]
content = position_strings['bottom']
else:
percentage = winline_first * 100.0 / (line_last - winline_last + winline_first)
content = str(int(round(percentage))) + '%'

View File

@ -508,7 +508,7 @@ class TestVim(TestCase):
segment_info['buffer'][:] = ['1']
vim_module._set_cursor(1, 0)
self.assertEqual(vim.position(pl=pl, segment_info=segment_info), 'All')
self.assertEqual(vim.position(pl=pl, segment_info=segment_info, position_strings=('Comienzo', 'Final', 'Todo')), 'Todo')
self.assertEqual(vim.position(pl=pl, segment_info=segment_info, position_strings={'top':'Comienzo', 'bottom':'Final', 'all':'Todo'}), 'Todo')
self.assertEqual(vim.position(pl=pl, segment_info=segment_info, gradient=True),
[{'contents': 'All', 'highlight_group': ['position_gradient', 'position'], 'gradient_level': 0.0}])
finally:

View File

@ -169,10 +169,6 @@ def eval(expr):
return vars[expr[2:]]
elif expr.startswith('&'):
return options[expr[1:]]
elif expr == 'line("w0")':
return 1
elif expr == 'line("w$")':
return 1
elif expr.startswith('PowerlineRegisterCachePurgerEvent'):
_buf_purge_events.add(expr[expr.find('"') + 1:expr.rfind('"') - 1])
return '0'
@ -306,6 +302,15 @@ def _emul_line2byte(line):
raise NotImplementedError
@_vim
def _emul_line(expr):
if expr == 'w0':
return 1
if expr == 'w$':
return 1
raise NotImplementedError
_window_ids = [None]
_window_id = 0