Fix close_all and change_title plugins

This commit is contained in:
mathieui 2018-07-22 16:17:06 +02:00
parent e2414121af
commit c6cf2d08b7
No known key found for this signature in database
GPG key ID: C59F84CEEFD616E3
6 changed files with 46 additions and 31 deletions

View file

@ -12,8 +12,8 @@ The following events are poezio-only events, for Slixmpp events, check out
- **tab:** :py:class:`~tabs.MucTab` source of the event - **tab:** :py:class:`~tabs.MucTab` source of the event
tab_change tab_change
- **old_tab_nb:** :py:class:`int` Old current tab number. - **old_tab:** :py:class:`int` Old current tab.
- **new_tab_nb:** :py:class:`int` New current tab number. - **new_tab:** :py:class:`int` New current tab.
Triggered whenever the user switches between tabs. Triggered whenever the user switches between tabs.

View file

@ -8,7 +8,7 @@ import sys
class Plugin(BasePlugin): class Plugin(BasePlugin):
def init(self): def init(self):
self.on_tab_change(0, self.core.current_tab_nb) self.on_tab_change(None, new_tab=self.core.tabs.current_tab)
self.api.add_event_handler('tab_change', self.on_tab_change) self.api.add_event_handler('tab_change', self.on_tab_change)
def cleanup(self): def cleanup(self):
@ -16,8 +16,6 @@ class Plugin(BasePlugin):
sys.stdout.write("\x1b]0;poezio\x07") sys.stdout.write("\x1b]0;poezio\x07")
sys.stdout.flush() sys.stdout.flush()
def on_tab_change(self, old, new): def on_tab_change(self, old_tab, new_tab):
new_tab = self.core.get_tab_by_number(new)
sys.stdout.write("\x1b]0;{}\x07".format(new_tab.name)) sys.stdout.write("\x1b]0;{}\x07".format(new_tab.name))
sys.stdout.flush() sys.stdout.flush()

View file

@ -26,19 +26,18 @@ class Plugin(BasePlugin):
""" """
/closeall /closeall
""" """
current = self.core.current_tab() current = self.api.current_tab()
if not isinstance(current, (tabs.RosterInfoTab, tabs.MucTab)): if not isinstance(current, (tabs.RosterInfoTab, tabs.MucTab)):
self.core.go_to_roster() self.core.go_to_roster()
current = self.core.current_tab() current = self.api.current_tab()
def filter_func(x): def filter_func(x):
return not isinstance(x, (tabs.RosterInfoTab, tabs.MucTab)) return not isinstance(x, (tabs.RosterInfoTab, tabs.MucTab))
matching_tabs = list(filter(filter_func, self.core.tabs)) matching_tabs = list(filter(filter_func, self.core.tabs.get_tabs()))
length = len(matching_tabs) length = len(matching_tabs)
for tab in matching_tabs: for tab in matching_tabs:
self.core.close_tab(tab) self.core.close_tab(tab)
self.core.current_tab_nb = current.nb
self.api.information('%s tabs closed.' % length, 'Info') self.api.information('%s tabs closed.' % length, 'Info')
self.core.refresh_window() self.core.refresh_window()

View file

@ -95,7 +95,11 @@ class Core:
self.xml_tab = None self.xml_tab = None
self.xml_buffer = TextBuffer() self.xml_buffer = TextBuffer()
self.tabs = Tabs() self.plugins_autoloaded = False
self.plugin_manager = PluginManager(self)
self.events = events.EventHandler()
self.tabs = Tabs(self.events)
self.previous_tab_nb = 0 self.previous_tab_nb = 0
own_nick = config.get('default_nick') own_nick = config.get('default_nick')
@ -104,10 +108,6 @@ class Core:
own_nick = own_nick or 'poezio' own_nick = own_nick or 'poezio'
self.own_nick = own_nick self.own_nick = own_nick
self.plugins_autoloaded = False
self.plugin_manager = PluginManager(self)
self.events = events.EventHandler()
self.size = SizeManager(self) self.size = SizeManager(self)
# Set to True whenever we consider that we have been disconnected # Set to True whenever we consider that we have been disconnected

View file

@ -27,6 +27,7 @@ disabled.
from typing import List, Dict, Type, Optional, Union from typing import List, Dict, Type, Optional, Union
from collections import defaultdict from collections import defaultdict
from poezio import tabs from poezio import tabs
from poezio.events import EventHandler
class Tabs: class Tabs:
@ -34,11 +35,16 @@ class Tabs:
Tab list class Tab list class
""" """
__slots__ = [ __slots__ = [
'_current_index', '_current_tab', '_tabs', '_tab_types', '_tab_names', '_current_index',
'_previous_tab' '_current_tab',
'_tabs',
'_tab_types',
'_tab_names',
'_previous_tab',
'_events',
] ]
def __init__(self): def __init__(self, events: EventHandler):
""" """
Initialize the Tab List. Even though the list is initially Initialize the Tab List. Even though the list is initially
empty, all methods are only valid once append() has been called empty, all methods are only valid once append() has been called
@ -53,6 +59,7 @@ class Tabs:
self._tab_types = defaultdict( self._tab_types = defaultdict(
list) # type: Dict[Type[tabs.Tab], List[tabs.Tab]] list) # type: Dict[Type[tabs.Tab], List[tabs.Tab]]
self._tab_names = dict() # type: Dict[str, tabs.Tab] self._tab_names = dict() # type: Dict[str, tabs.Tab]
self._events = events # type: EventHandler
def __len__(self): def __len__(self):
return len(self._tabs) return len(self._tabs)
@ -78,11 +85,7 @@ class Tabs:
"""Set the current tab index""" """Set the current tab index"""
if 0 <= value < len(self._tabs): if 0 <= value < len(self._tabs):
tab = self._tabs[value] tab = self._tabs[value]
if not isinstance(tab, tabs.GapTab): return self.set_current_tab(tab)
self._store_previous()
self._current_index = tab.nb
self._current_tab = tab
return True
return False return False
@property @property
@ -97,6 +100,10 @@ class Tabs:
self._store_previous() self._store_previous()
self._current_index = tab.nb self._current_index = tab.nb
self._current_tab = tab self._current_tab = tab
self._events.trigger(
'tab_change',
old_tab=self._previous_tab,
new_tab=self._current_tab)
return True return True
return False return False
@ -170,6 +177,10 @@ class Tabs:
self._inc_cursor() self._inc_cursor()
while isinstance(self.current_tab, tabs.GapTab): while isinstance(self.current_tab, tabs.GapTab):
self._inc_cursor() self._inc_cursor()
self._events.trigger(
'tab_change',
old_tab=self._previous_tab,
new_tab=self._current_tab)
def prev(self): def prev(self):
"""Go to the left of the tab list (circular)""" """Go to the left of the tab list (circular)"""
@ -177,6 +188,10 @@ class Tabs:
self._dec_cursor() self._dec_cursor()
while isinstance(self.current_tab, tabs.GapTab): while isinstance(self.current_tab, tabs.GapTab):
self._dec_cursor() self._dec_cursor()
self._events.trigger(
'tab_change',
old_tab=self._previous_tab,
new_tab=self._current_tab)
def append(self, tab: tabs.Tab): def append(self, tab: tabs.Tab):
""" """

View file

@ -4,6 +4,9 @@ Tests for the Tabs list module
from poezio.core.tabs import Tabs from poezio.core.tabs import Tabs
from poezio.tabs import GapTab from poezio.tabs import GapTab
from poezio.events import EventHandler
h = EventHandler()
class DummyTab: class DummyTab:
count = 0 count = 0
@ -18,7 +21,7 @@ class DummyTab:
def test_append(): def test_append():
DummyTab.reset() DummyTab.reset()
tabs = Tabs() tabs = Tabs(h)
dummy = DummyTab() dummy = DummyTab()
tabs.append(dummy) tabs.append(dummy)
assert tabs[0] is dummy assert tabs[0] is dummy
@ -35,7 +38,7 @@ def test_append():
def test_delete(): def test_delete():
DummyTab.reset() DummyTab.reset()
tabs = Tabs() tabs = Tabs(h)
dummy = DummyTab() dummy = DummyTab()
dummy2 = DummyTab() dummy2 = DummyTab()
tabs.append(dummy) tabs.append(dummy)
@ -50,7 +53,7 @@ def test_delete():
def test_delete_restore_previous(): def test_delete_restore_previous():
DummyTab.reset() DummyTab.reset()
tabs = Tabs() tabs = Tabs(h)
dummy = DummyTab() dummy = DummyTab()
dummy2 = DummyTab() dummy2 = DummyTab()
dummy3 = DummyTab() dummy3 = DummyTab()
@ -71,7 +74,7 @@ def test_delete_restore_previous():
def test_delete_other_tab(): def test_delete_other_tab():
DummyTab.reset() DummyTab.reset()
tabs = Tabs() tabs = Tabs(h)
dummy = DummyTab() dummy = DummyTab()
dummy2 = DummyTab() dummy2 = DummyTab()
dummy3 = DummyTab() dummy3 = DummyTab()
@ -91,7 +94,7 @@ def test_delete_other_tab():
def test_insert_and_gaps(): def test_insert_and_gaps():
DummyTab.reset() DummyTab.reset()
tabs = Tabs() tabs = Tabs(h)
dummy = DummyTab() dummy = DummyTab()
dummy2 = DummyTab() dummy2 = DummyTab()
dummy3 = DummyTab() dummy3 = DummyTab()
@ -114,7 +117,7 @@ def test_insert_and_gaps():
def test_replace_tabs(): def test_replace_tabs():
DummyTab.reset() DummyTab.reset()
tabs = Tabs() tabs = Tabs(h)
dummy = DummyTab() dummy = DummyTab()
dummy2 = DummyTab() dummy2 = DummyTab()
dummy3 = DummyTab() dummy3 = DummyTab()
@ -129,7 +132,7 @@ def test_replace_tabs():
def test_prev_next(): def test_prev_next():
DummyTab.reset() DummyTab.reset()
tabs = Tabs() tabs = Tabs(h)
dummy = DummyTab() dummy = DummyTab()
dummy2 = DummyTab() dummy2 = DummyTab()
dummy3 = DummyTab() dummy3 = DummyTab()
@ -155,7 +158,7 @@ def test_prev_next():
def test_set_current(): def test_set_current():
DummyTab.reset() DummyTab.reset()
tabs = Tabs() tabs = Tabs(h)
dummy = DummyTab() dummy = DummyTab()
dummy2 = DummyTab() dummy2 = DummyTab()
dummy3 = DummyTab() dummy3 = DummyTab()