Add single_tab segment

This commit is contained in:
ZyX 2014-08-03 12:43:41 +04:00
parent 2acefc9ac9
commit 6cf0c485fa
5 changed files with 75 additions and 5 deletions

View File

@ -12,6 +12,8 @@
"line_percent": "information:additional",
"line_count": "line_current",
"position": "information:additional",
"single_tab": "line_current",
"many_tabs": "line_current",
"tabnr": "file_directory"
}
}

View File

@ -34,6 +34,11 @@
"draw_hard_divider": false,
"width": "auto"
}
],
"right": [
{
"name": "single_tab"
}
]
}
}

View File

@ -504,6 +504,32 @@ def tabnr(pl, segment_info, show_current=False):
return str(tabnr)
def single_tab(pl, single_text='Bufs', multiple_text='Tabs'):
'''Show one text if there is only one tab and another if there are many
Mostly useful for tabline to indicate what kind of data is shown there.
:param str single_text:
Text displayed when there is only one tabpage. May be None if you do not
want to display anything.
:param str multiple_text:
Text displayed when there is more then one tabpage. May be None if you
do not want to display anything.
Highlight groups used: ``single_tab``, ``many_tabs``.
'''
if len(list_tabpages()) == 1:
return single_text and [{
'contents': single_text,
'highlight_group': ['single_tab'],
}]
else:
return multiple_text and [{
'contents': multiple_text,
'highlight_group': ['many_tabs'],
}]
def tabpage_updated_segment_info(segment_info, tabpage):
segment_info = segment_info.copy()
window = tabpage.window

View File

@ -793,6 +793,19 @@ class TestVim(TestCase):
self.assertEqual(vim.tabnr(pl=pl, segment_info=segment_info, show_current=True), '1')
self.assertEqual(vim.tabnr(pl=pl, segment_info=segment_info, show_current=False), None)
def test_single_tab(self):
pl = Pl()
single_tab = partial(vim.single_tab, pl=pl)
with vim_module._with('tabpage'):
self.assertEqual(single_tab(), [{'highlight_group': ['many_tabs'], 'contents': 'Tabs'}])
self.assertEqual(single_tab(single_text='s', multiple_text='m'), [{'highlight_group': ['many_tabs'], 'contents': 'm'}])
self.assertEqual(single_tab(multiple_text='m'), [{'highlight_group': ['many_tabs'], 'contents': 'm'}])
self.assertEqual(single_tab(single_text='s'), [{'highlight_group': ['many_tabs'], 'contents': 'Tabs'}])
self.assertEqual(single_tab(), [{'highlight_group': ['single_tab'], 'contents': 'Bufs'}])
self.assertEqual(single_tab(single_text='s', multiple_text='m'), [{'highlight_group': ['single_tab'], 'contents': 's'}])
self.assertEqual(single_tab(multiple_text='m'), [{'highlight_group': ['single_tab'], 'contents': 'Bufs'}])
self.assertEqual(single_tab(single_text='s'), [{'highlight_group': ['single_tab'], 'contents': 's'}])
old_cwd = None

View File

@ -125,8 +125,11 @@ class _ObjList(object):
return iter(self.l)
@_vim
def _pop(self, *args, **kwargs):
return self.l.pop(*args, **kwargs)
def _pop(self, idx):
obj = self.l.pop(idx - 1)
for moved_obj in self.l[idx - 1:]:
moved_obj.number -= 1
return obj
@_vim
def _append(self, *args, **kwargs):
@ -461,15 +464,21 @@ class _Tabpage(object):
self.window = self.windows._new(**kwargs)
return self.window
def _close_window(self, winnr):
def _close_window(self, winnr, open_window=True):
curwinnr = self.window.number
win = self.windows._pop(winnr - 1)
win = self.windows._pop(winnr)
if self.windows and winnr == curwinnr:
self.window = self.windows[-1]
else:
elif open_window:
current.tabpage._new_window()
return win
def _close(self):
while self.windows:
self._close_window(1, False)
tabpages._pop(self.number)
_tabpage = len(tabpages)
tabpages = _ObjList(_Tabpage)
@ -665,6 +674,7 @@ def _tabnew(name=None):
windows = tabpage.windows
_tabpage = len(tabpages)
_new(name)
return tabpage
@_vim
@ -823,6 +833,18 @@ class _WithBufName(object):
self.buffer.name = self.old
class _WithNewTabPage(object):
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
def __enter__(self):
self.tab = _tabnew(*self.args, **self.kwargs)
def __exit__(self, *args):
self.tab._close()
@_vim
def _with(key, *args, **kwargs):
if key == 'buffer':
@ -843,6 +865,8 @@ def _with(key, *args, **kwargs):
return _WithDict(_environ, **kwargs)
elif key == 'split':
return _WithSplit()
elif key == 'tabpage':
return _WithNewTabPage(*args, **kwargs)
class error(Exception):