Merge pull request #750 from EinfachToll/feature/539-position-segment

New (fixed) segment showing position of current view
This commit is contained in:
ZyX-I 2014-01-23 03:51:42 -08:00
commit 04d2e4394d
6 changed files with 81 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

@ -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 = {
@ -265,6 +266,44 @@ def line_percent(pl, segment_info, gradient=False):
}]
@window_cached
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 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)
Highlight groups used: ``position_gradient`` (gradient), ``position``.
'''
line_last = len(vim.current.buffer)
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['all']
elif winline_first == 1:
percentage = 0.0
content = position_strings['top']
elif winline_last == line_last:
percentage = 100.0
content = position_strings['bottom']
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,27 @@ 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'][0:-1] = [str(i) for i in range(99)]
vim_module._set_cursor(49, 0)
self.assertEqual(vim.position(pl=pl, segment_info=segment_info), '50%')
self.assertEqual(vim.position(pl=pl, segment_info=segment_info, gradient=True),
[{'contents': '50%', 'highlight_group': ['position_gradient', 'position'], 'gradient_level': 50.0}])
vim_module._set_cursor(0, 0)
self.assertEqual(vim.position(pl=pl, segment_info=segment_info), 'Top')
vim_module._set_cursor(97, 0)
self.assertEqual(vim.position(pl=pl, segment_info=segment_info, position_strings={'top':'Comienzo', 'bottom':'Final', 'all':'Todo'}), 'Final')
segment_info['buffer'][0:-1] = [str(i) for i in range(2)]
vim_module._set_cursor(0, 0)
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:
vim_module._bw(segment_info['bufnr'])
def test_cursor_current(self):
pl = Pl()
segment_info = vim_module._get_segment_info()

View File

@ -302,6 +302,17 @@ def _emul_line2byte(line):
raise NotImplementedError
@_vim
def _emul_line(expr):
cursorline = windows[_window - 1].cursor[0] + 1
numlines = len(_buf_lines[_buffer()])
if expr == 'w0':
return max(cursorline-5, 1)
if expr == 'w$':
return min(cursorline+5, numlines)
raise NotImplementedError
_window_ids = [None]
_window_id = 0