[][src]Trait hexavalent::Plugin

pub trait Plugin: Default + 'static {
    pub fn init(&self, ph: PluginHandle<'_, Self>);

    pub fn deinit(&self, ph: PluginHandle<'_, Self>) { ... }
}

Must be implemented by all HexChat plugins.

Examples

use std::cell::Cell;
use std::time::SystemTime;
use hexavalent::{Plugin, PluginHandle};
use hexavalent::event::{Event, EventAttrs};
use hexavalent::event::print::ChannelMessage;
use hexavalent::hook::{Eat, Priority};

struct StatsPlugin {
    start: Cell<SystemTime>,
    messages: Cell<usize>,
    characters: Cell<usize>,
}

impl Default for StatsPlugin {
    fn default() -> Self {
        Self {
            start: Cell::new(SystemTime::now()),
            messages: Cell::new(0),
            characters: Cell::new(0),
        }
    }
}

impl StatsPlugin {
    fn message_cb(
        &self,
        ph: PluginHandle<'_, Self>,
        [_, text, _, _]: <ChannelMessage as Event<'_>>::Args,
    ) -> Eat {
        self.messages.set(self.messages.get() + 1);
        self.characters.set(self.characters.get() + text.chars().count());
        Eat::None
    }

    fn print_stats(&self, ph: PluginHandle<'_, Self>) {
        let elapsed = self.start.get().elapsed().unwrap();

        let messages = self.messages.get();
        let avg_msgs = messages as f64 / (elapsed.as_secs_f64() / 60.);
        ph.print(&format!("Messages: {} ({:.1}/min).\0", messages, avg_msgs));

        let characters = self.characters.get();
        let avg_chars = characters as f64 / messages as f64;
        ph.print(&format!("Characters: {} ({:.1}/msg).\0", characters, avg_chars));
    }
}

impl Plugin for StatsPlugin {
    fn init(&self, ph: PluginHandle<'_, Self>) {
        ph.hook_command(
            "stats\0",
            "Usage: STATS, print message statistics\0",
            Priority::Normal,
            |plugin, ph, words| {
                plugin.print_stats(ph);
                Eat::All
            },
        );
        ph.hook_print(ChannelMessage, Priority::Normal, Self::message_cb);
    }

    fn deinit(&self, ph: PluginHandle<'_, Self>) {
        ph.print("Overall stats:\0");
        self.print_stats(ph);
    }
}

Required methods

pub fn init(&self, ph: PluginHandle<'_, Self>)[src]

Initialize your plugin.

Use this function to perform any work that should be done when your plugin is loaded, such as registering hooks or printing startup messages.

Analogous to hexchat_plugin_init.

Examples

use hexavalent::{Plugin, PluginHandle};

#[derive(Default)]
struct MyPlugin;

impl Plugin for MyPlugin {
    fn init(&self, ph: PluginHandle<'_, Self>) {
        ph.print("Plugin loaded successfully!\0");
    }
}
Loading content...

Provided methods

pub fn deinit(&self, ph: PluginHandle<'_, Self>)[src]

Deinitialize your plugin.

Use this function to perform any work that should be done when your plugin is unloaded, such as printing shutdown messages or statistics.

You do not need to call PluginHandle::unhook in this function, as remaining hooks are automatically removed by HexChat when your plugin finishes unloading.

Analogous to hexchat_plugin_deinit.

Examples

use hexavalent::{Plugin, PluginHandle};

#[derive(Default)]
struct MyPlugin;

impl Plugin for MyPlugin {
    fn init(&self, _: PluginHandle<'_, Self>) {}

    fn deinit(&self, ph: PluginHandle<'_, Self>) {
        ph.print("Plugin unloading...\0");
    }
}
Loading content...

Implementors

Loading content...