Fix /reorder

This commit is contained in:
mathieui 2018-07-21 21:36:46 +02:00
parent 784f2d2876
commit 534ba14d5c
No known key found for this signature in database
GPG key ID: C59F84CEEFD616E3
2 changed files with 15 additions and 26 deletions

View file

@ -80,18 +80,18 @@ TAB_TO_TEXT = {
tabs.GapTab: 'empty' tabs.GapTab: 'empty'
} }
def parse_config(config): def parse_config(tab_config):
result = {} result = {}
for option in config.options('reorder'): for option in tab_config.options('reorder'):
if not option.isdecimal(): if not option.isdecimal():
continue continue
pos = int(option) pos = int(option)
if pos in result or pos <= 0: if pos in result or pos <= 0:
return return None
typ, name = config.get(option, default=':').split(':', maxsplit=1) typ, name = tab_config.get(option, default=':').split(':', maxsplit=1)
if typ not in TEXT_TO_TAB: if typ not in TEXT_TO_TAB:
return return None
result[pos] = (TEXT_TO_TAB[typ], name) result[pos] = (TEXT_TO_TAB[typ], name)
return result return result
@ -132,25 +132,22 @@ class Plugin(BasePlugin):
""" """
/reorder /reorder
""" """
self.core.go_to_roster()
self.core.current_tab_nb = 0
tabs_spec = parse_config(self.config) tabs_spec = parse_config(self.config)
if not tabs_spec: if not tabs_spec:
return self.api.information('Invalid reorder config', 'Error') return self.api.information('Invalid reorder config', 'Error')
old_tabs = self.core.tabs[1:] old_tabs = self.core.tabs.get_tabs()
roster = self.core.tabs[0] roster = old_tabs.pop(0)
create_gaps = config.get('create_gaps') create_gaps = config.get('create_gaps')
new_tabs = [] new_tabs = [roster]
last = 0 last = 0
for pos in sorted(tabs_spec): for pos in sorted(tabs_spec):
if create_gaps and pos > last + 1: if create_gaps and pos > last + 1:
new_tabs += [tabs.GapTab(self.core) for i in range(pos - last)] new_tabs += [tabs.GapTab(self.core) for i in range(pos - last - 1)]
cls, name = tabs_spec[pos] cls, name = tabs_spec[pos]
tab = self.core.get_tab_by_name(name, typ=cls) tab = self.core.tabs.by_name_and_class(name, cls=cls)
if tab and tab in old_tabs: if tab and tab in old_tabs:
new_tabs.append(tab) new_tabs.append(tab)
old_tabs.remove(tab) old_tabs.remove(tab)
@ -164,9 +161,5 @@ class Plugin(BasePlugin):
if tab: if tab:
new_tabs.append(tab) new_tabs.append(tab)
self.core.tabs.clear() self.core.tabs.replace_tabs(new_tabs)
self.core.tabs.append(roster)
self.core.tabs += new_tabs
self.core.refresh_window() self.core.refresh_window()

View file

@ -141,20 +141,16 @@ class Tabs:
self._tab_names[tab.name] = tab self._tab_names[tab.name] = tab
self._update_numbers() self._update_numbers()
def replace_tabs(self, new_tabs: List[tabs.Tab]): def replace_tabs(self, new_tabs: List[tabs.Tab]) -> bool:
""" """
Replace the current tab list with another, and Replace the current tab list with another, and
rebuild the mappings. rebuild the mappings.
""" """
if self._current_tab not in new_tabs:
return False
self._tabs = new_tabs self._tabs = new_tabs
self._rebuild() self._rebuild()
current_tab = self.current_tab return True
try:
idx = self._tabs.index(current_tab)
self._current_index = idx
except ValueError:
self._current_index = 0
self._current_tab = self._tabs[0]
def _inc_cursor(self): def _inc_cursor(self):
self._current_index = (self._current_index + 1) % len(self._tabs) self._current_index = (self._current_index + 1) % len(self._tabs)