New fixed segment showing position of current view

Solves #539
Fixed version of #540 and #579
Works now for split windows and wrapped lines.
This commit is contained in:
EinfachToll 2014-01-15 10:32:04 +01:00
parent 31f85800b6
commit 7aa978fdba
6 changed files with 65 additions and 0 deletions

View File

@ -25,6 +25,8 @@
"file_vcs_status_A": { "fg": "brightgreen", "bg": "gray4" },
"line_percent": { "fg": "gray9", "bg": "gray4" },
"line_percent_gradient": { "fg": "green_yellow_red", "bg": "gray4" },
"position": { "fg": "gray9", "bg": "gray4" },
"position_gradient": { "fg": "green_yellow_red", "bg": "gray4" },
"line_current": { "fg": "gray1", "bg": "gray10", "attr": ["bold"] },
"line_current_symbol": { "fg": "gray1", "bg": "gray10" },
"virtcol_current_gradient": { "fg": "dark_GREEN_Orange_red", "bg": "gray10" },

View File

@ -25,6 +25,8 @@
"file_vcs_status_A": { "fg": "green", "bg": "darkgreencopper" },
"line_percent": { "fg": "oldlace", "bg": "lightskyblue4" },
"line_percent_gradient": { "fg": "green_yellow_orange_red", "bg": "lightskyblue4" },
"position": { "fg": "oldlace", "bg": "lightskyblue4" },
"position_gradient": { "fg": "green_yellow_orange_red", "bg": "lightskyblue4" },
"line_current": { "fg": "gray13", "bg": "lightyellow", "attr": ["bold"] },
"line_current_symbol": { "fg": "gray13", "bg": "lightyellow" },
"virtcol_current_gradient": { "fg": "GREEN_Orange_red", "bg": "gray10" },
@ -68,6 +70,8 @@
"file_vcs_status_A": { "fg": "green", "bg": "lightyellow" },
"line_percent": { "fg": "oldlace", "bg": "gray61" },
"line_percent_gradient": { "fg": "oldlace", "bg": "gray61" },
"position": { "fg": "oldlace", "bg": "gray61" },
"position_gradient": { "fg": "oldlace", "bg": "gray61" },
"line_current": { "fg": "gray13", "bg": "oldlace", "attr": ["bold"] },
"line_current_symbol": { "fg": "gray13", "bg": "oldlace" },
"col_current": { "fg": "azure4", "bg": "oldlace" }

View File

@ -25,6 +25,8 @@
"file_vcs_status_A": { "fg": "green", "bg": "lightyellow" },
"line_percent": { "fg": "gray13", "bg": "lightyellow" },
"line_percent_gradient": { "fg": "green_yellow_orange_red", "bg": "lightyellow" },
"position": { "fg": "gray13", "bg": "lightyellow" },
"position_gradient": { "fg": "green_yellow_orange_red", "bg": "lightyellow" },
"line_current": { "fg": "oldlace", "bg": "royalblue5", "attr": ["bold"] },
"line_current_symbol": { "fg": "oldlace", "bg": "royalblue5" },
"virtcol_current_gradient": { "fg": "yellow_orange_red", "bg": "royalblue5" },
@ -68,6 +70,8 @@
"file_vcs_status_A": { "fg": "green", "bg": "royalblue5" },
"line_percent": { "fg": "gray13", "bg": "gray61" },
"line_percent_gradient": { "fg": "gray13", "bg": "gray61" },
"position": { "fg": "gray13", "bg": "gray61" },
"position_gradient": { "fg": "gray13", "bg": "gray61" },
"line_current": { "fg": "oldlace", "bg": "gray13", "attr": ["bold"] },
"line_current_symbol": { "fg": "oldlace", "bg": "gray13" },
"virtcol_current_gradient": { "fg": "yellow_orange_red", "bg": "gray13" },

View File

@ -265,6 +265,44 @@ def line_percent(pl, segment_info, gradient=False):
}]
@window_cached
def position(pl, position_strings=('Top', 'Bot', '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 bool gradient:
highlight the percentage with a color gradient (by default a green to red gradient)
Highlight groups used: ``position_gradient`` (gradient), ``position``.
'''
line_last = len(vim.current.buffer)
winline_first = int(vim.eval('line("w0")'))
winline_last = int(vim.eval('line("w$")'))
if winline_first == 1 and winline_last == line_last:
percentage = 0.0
content = position_strings[2]
elif winline_first == 1:
percentage = 0.0
content = position_strings[0]
elif winline_last == line_last:
percentage = 100.0
content = position_strings[1]
else:
percentage = winline_first * 100.0 / (line_last - winline_last + winline_first)
content = str(int(round(percentage))) + '%'
if not gradient:
return content
return [{
'contents': content,
'highlight_group': ['position_gradient', 'position'],
'gradient_level': percentage,
}]
@requires_segment_info
def line_current(pl, segment_info):
'''Return the current cursor line.'''

View File

@ -501,6 +501,19 @@ class TestVim(TestCase):
finally:
vim_module._bw(segment_info['bufnr'])
def test_position(self):
pl = Pl()
segment_info = vim_module._get_segment_info()
try:
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, gradient=True),
[{'contents': 'All', 'highlight_group': ['position_gradient', 'position'], 'gradient_level': 0.0}])
finally:
vim_module._bw(segment_info['bufnr'])
def test_cursor_current(self):
pl = Pl()
segment_info = vim_module._get_segment_info()

View File

@ -169,6 +169,10 @@ 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'