Trait hexavalent::Plugin
source · pub trait Plugin: Default + 'static {
// Required method
fn init(&self, ph: PluginHandle<'_, Self>);
// Provided method
fn deinit(&self, ph: PluginHandle<'_, Self>) { ... }
}
Expand description
Must be implemented by all HexChat plugins.
§Examples
use std::cell::Cell;
use std::time::SystemTime;
use hexavalent::{Plugin, PluginHandle};
use hexavalent::event::print::ChannelMessage;
use hexavalent::hook::{Eat, Priority};
use hexavalent::str::HexStr;
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, _, _]: [&HexStr; 4],
) -> 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).", messages, avg_msgs));
let characters = self.characters.get();
let avg_chars = characters as f64 / messages as f64;
ph.print(format!("Characters: {} ({:.1}/msg).", characters, avg_chars));
}
}
impl Plugin for StatsPlugin {
fn init(&self, ph: PluginHandle<'_, Self>) {
ph.hook_command(
c"stats",
c"Usage: STATS, print message statistics",
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(c"Overall stats:");
self.print_stats(ph);
}
}
Required Methods§
sourcefn init(&self, ph: PluginHandle<'_, Self>)
fn init(&self, ph: PluginHandle<'_, Self>)
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(c"Plugin loaded successfully!");
}
}
Provided Methods§
sourcefn deinit(&self, ph: PluginHandle<'_, Self>)
fn deinit(&self, ph: PluginHandle<'_, Self>)
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(c"Plugin unloading...");
}
}
Object Safety§
This trait is not object safe.