marquee.py 2.47 KB
Newer Older
mathieui's avatar
mathieui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
"""
Marquee plugin: replicate the html <marquee/> tag with message corrections.

Usage of this plugin is not recommended.

Commands
--------

.. glossary::

    /marquee <text>
        Send the following text with <marquee/> behavior

Configuration
-------------

.. glossary::
    :sorted:

    refresh
        **Default:** ``1``

        Interval between each correction (the closest to 0 is the fastest)

    total_duration
        **Default:** ``30``

        Total duration of the animation.

    padding
        **Default:** ``20``

        Padding to use to move the text.


"""
37 38 39 40
from poezio.plugin import BasePlugin
from poezio import tabs
from poezio import xhtml
from poezio.decorators import command_args_parser
mathieui's avatar
mathieui committed
41

mathieui's avatar
mathieui committed
42

mathieui's avatar
mathieui committed
43
def move(text, step, spacing):
44
    new_text = text + ("\u00A0" * spacing)
mathieui's avatar
mathieui committed
45 46 47
    return new_text[-(step % len(new_text)):] + new_text[:-(
        step % len(new_text))]

mathieui's avatar
mathieui committed
48 49

class Plugin(BasePlugin):
mathieui's avatar
mathieui committed
50 51 52 53 54 55 56
    default_config = {
        "marquee": {
            "refresh": 1.0,
            "total_duration": 30,
            "padding": 20
        }
    }
mathieui's avatar
mathieui committed
57 58

    def init(self):
59
        for tab_t in [tabs.MucTab, tabs.DynamicConversationTab, tabs.StaticConversationTab, tabs.PrivateTab]:
mathieui's avatar
mathieui committed
60 61 62
            self.add_tab_command(
                tab_t, 'marquee', self.command_marquee,
                'Replicate the <marquee/> behavior in a message')
mathieui's avatar
mathieui committed
63 64 65 66

    @command_args_parser.raw
    def command_marquee(self, args):
        tab = self.api.current_tab()
mathieui's avatar
mathieui committed
67
        args = xhtml.clean_text(xhtml.convert_simple_to_full_colors(args))
mathieui's avatar
mathieui committed
68
        tab.command_say(args)
69
        is_muctab = isinstance(tab, tabs.MucTab)
mathieui's avatar
mathieui committed
70
        msg_id = tab.last_sent_message["id"]
71
        jid = tab.jid
mathieui's avatar
mathieui committed
72

mathieui's avatar
mathieui committed
73 74 75
        event = self.api.create_delayed_event(
            self.config.get("refresh"), self.delayed_event, jid, args, msg_id,
            1, 0, is_muctab)
76
        self.api.add_timed_event(event)
mathieui's avatar
mathieui committed
77

78
    def delayed_event(self, jid, body, msg_id, step, duration, is_muctab):
mathieui's avatar
mathieui committed
79 80 81
        if duration >= self.config.get("total_duration"):
            return
        message = self.core.xmpp.make_message(jid)
82
        message["type"] = "groupchat" if is_muctab else "chat"
83
        message["body"] = move(body, step, self.config.get("padding"))
mathieui's avatar
mathieui committed
84 85
        message["replace"]["id"] = msg_id
        message.send()
mathieui's avatar
mathieui committed
86 87
        event = self.api.create_delayed_event(
            self.config.get("refresh"), self.delayed_event, jid, body,
88
            msg_id, step + 1, duration + self.config.get("refresh"),
mathieui's avatar
mathieui committed
89
            is_muctab)
90
        self.api.add_timed_event(event)