diff --git a/powerline/segments/vim.py b/powerline/segments/vim.py index b3cd41ed..07da7e37 100644 --- a/powerline/segments/vim.py +++ b/powerline/segments/vim.py @@ -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))) + '%' diff --git a/tests/test_segments.py b/tests/test_segments.py index c3014ac7..7759c9f2 100644 --- a/tests/test_segments.py +++ b/tests/test_segments.py @@ -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: diff --git a/tests/vim.py b/tests/vim.py index da12c812..a7ee45ca 100644 --- a/tests/vim.py +++ b/tests/vim.py @@ -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