Separate logic for resolving buffers from index

This commit is contained in:
Michael Hoang 2017-10-04 04:33:23 +11:00
parent 211de6d664
commit b7061dc7db

View File

@ -1009,40 +1009,27 @@ class CommandDispatcher:
raise cmdexc.CommandError(e) raise cmdexc.CommandError(e)
self._open(url, tab, bg, window) self._open(url, tab, bg, window)
@cmdutils.register(instance='command-dispatcher', scope='window') def _resolve_buffer_index(self, index):
@cmdutils.argument('index', completion=miscmodels.buffer) """Resolves a buffer index to the tabbedbrowser and tab.
@cmdutils.argument('count', count=True)
def buffer(self, index=None, count=None):
"""Select tab by index or url/title best match.
Focuses window if necessary when index is given. If both index and
count are given, use count.
Args: Args:
index: The [win_id/]index of the tab to focus. Or a substring index: The [win_id/]index of the tab to be selected. Or a substring
in which case the closest match will be focused. in which case the closest match will be focused.
count: The tab index to focus, starting with 1.
""" """
if count is not None: index_parts = index.split('/', 1)
index_parts = [count]
elif index is None:
raise cmdexc.CommandError("buffer: Either a count or the argument "
"index must be specified.")
else:
index_parts = index.split('/', 1)
try: try:
for part in index_parts: for part in index_parts:
int(part) int(part)
except ValueError: except ValueError:
model = miscmodels.buffer() model = miscmodels.buffer()
model.set_pattern(index) model.set_pattern(index)
if model.count() > 0: if model.count() > 0:
index = model.data(model.first_item()) index = model.data(model.first_item())
index_parts = index.split('/', 1) index_parts = index.split('/', 1)
else: else:
raise cmdexc.CommandError( raise cmdexc.CommandError(
"No matching tab for: {}".format(index)) "No matching tab for: {}".format(index))
if len(index_parts) == 2: if len(index_parts) == 2:
win_id = int(index_parts[0]) win_id = int(index_parts[0])
@ -1066,10 +1053,35 @@ class CommandDispatcher:
raise cmdexc.CommandError( raise cmdexc.CommandError(
"There's no tab with index {}!".format(idx)) "There's no tab with index {}!".format(idx))
window = objreg.window_registry[win_id] return (tabbed_browser, tabbed_browser.widget(idx-1))
@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('index', completion=miscmodels.buffer)
@cmdutils.argument('count', count=True)
def buffer(self, index=None, count=None):
"""Select tab by index or url/title best match.
Focuses window if necessary when index is given. If both index and
count are given, use count.
Args:
index: The [win_id/]index of the tab to focus. Or a substring
in which case the closest match will be focused.
count: The tab index to focus, starting with 1.
"""
if count is None and index is None:
raise cmdexc.CommandError("buffer: Either a count or the argument "
"index must be specified.")
if count is not None:
index = str(count)
tabbed_browser, tab = self._resolve_buffer_index(index)
window = tabbed_browser.window()
window.activateWindow() window.activateWindow()
window.raise_() window.raise_()
tabbed_browser.setCurrentIndex(idx-1) tabbed_browser.setCurrentWidget(tab)
@cmdutils.register(instance='command-dispatcher', scope='window') @cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('index', choices=['last']) @cmdutils.argument('index', choices=['last'])