From 04a76896ffde1464cea23b14989b80aa9802a213 Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Tue, 16 Oct 2018 22:58:46 -0300 Subject: Update examples to show hook handle hoisting. --- Cargo.toml | 2 +- src/lib.rs | 43 +++++++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dba35de..a87a433 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hexchat-plugin" -version = "0.2.8" +version = "0.2.9" authors = ["SoniEx2 "] description = "Lets you write HexChat plugins in Rust" license = "AGPL-3.0+" diff --git a/src/lib.rs b/src/lib.rs index 77edf8d..0f74cf8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,18 +35,21 @@ //! #[macro_use] //! extern crate hexchat_plugin; //! -//! use hexchat_plugin::{Plugin, PluginHandle}; +//! use std::sync::Mutex; +//! use hexchat_plugin::{Plugin, PluginHandle, CommandHookHandle}; //! //! #[derive(Default)] -//! struct MyPlugin {} +//! struct MyPlugin { +//! cmd: Mutex> +//! } //! //! impl Plugin for MyPlugin { //! fn init(&self, ph: &mut PluginHandle, arg: Option<&str>) -> bool { //! ph.register("myplugin", "0.1.0", "my simple plugin"); -//! ph.hook_command("hello-world", |ph, arg, arg_eol| { +//! *self.cmd.lock().unwrap() = Some(ph.hook_command("hello-world", |ph, arg, arg_eol| { //! ph.print("Hello, World!"); //! hexchat_plugin::EAT_ALL -//! }, hexchat_plugin::PRI_NORM, Some("prints 'Hello, World!'")); +//! }, hexchat_plugin::PRI_NORM, Some("prints 'Hello, World!'"))); //! true //! } //! } @@ -686,13 +689,15 @@ impl PluginHandle { /// # Examples /// /// ``` - /// use hexchat_plugin::PluginHandle; + /// use hexchat_plugin::{PluginHandle, CommandHookHandle}; /// - /// fn register_commands(ph: &mut PluginHandle) { + /// fn register_commands(ph: &mut PluginHandle) -> Vec { + /// vec![ /// ph.hook_command("hello-world", |ph, arg, arg_eol| { /// ph.print("Hello, World!"); /// hexchat_plugin::EAT_ALL - /// }, hexchat_plugin::PRI_NORM, Some("prints 'Hello, World!'")); + /// }, hexchat_plugin::PRI_NORM, Some("prints 'Hello, World!'")), + /// ] /// } /// ``` pub fn hook_command(&mut self, cmd: &str, cb: F, pri: i32, help: Option<&str>) -> CommandHookHandle where F: Fn(&mut PluginHandle, Word, WordEol) -> Eat + 'static + ::std::panic::RefUnwindSafe { @@ -733,15 +738,17 @@ impl PluginHandle { /// # Examples /// /// ``` - /// use hexchat_plugin::PluginHandle; + /// use hexchat_plugin::{PluginHandle, ServerHookHandle}; /// - /// fn register_server_hooks(ph: &mut PluginHandle) { + /// fn register_server_hooks(ph: &mut PluginHandle) -> Vec { + /// vec![ /// ph.hook_server("PRIVMSG", |ph, word, word_eol| { /// if word.len() > 0 && word[0].starts_with('@') { /// ph.print("We have message tags!?"); /// } /// hexchat_plugin::EAT_NONE - /// }, hexchat_plugin::PRI_NORM); + /// }, hexchat_plugin::PRI_NORM), + /// ] /// } /// ``` pub fn hook_server(&mut self, cmd: &str, cb: F, pri: i32) -> ServerHookHandle where F: Fn(&mut PluginHandle, Word, WordEol) -> Eat + 'static + ::std::panic::RefUnwindSafe { @@ -785,9 +792,10 @@ impl PluginHandle { /// # Examples /// /// ``` - /// use hexchat_plugin::PluginHandle; + /// use hexchat_plugin::{PluginHandle, PrintHookHandle}; /// - /// fn register_print_hooks(ph: &mut PluginHandle) { + /// fn register_print_hooks(ph: &mut PluginHandle) -> Vec { + /// vec![ /// ph.hook_print("Channel Message", |ph, arg| { /// if let Some(nick) = arg.get(0) { /// if *nick == "KnOwN_SpAmMeR" { @@ -795,7 +803,8 @@ impl PluginHandle { /// } /// } /// hexchat_plugin::EAT_NONE - /// }, hexchat_plugin::PRI_NORM); + /// }, hexchat_plugin::PRI_NORM), + /// ] /// } /// ``` pub fn hook_print(&mut self, name: &str, cb: F, pri: i32) -> PrintHookHandle where F: Fn(&mut PluginHandle, Word) -> Eat + 'static + ::std::panic::RefUnwindSafe { @@ -838,13 +847,15 @@ impl PluginHandle { /// # Examples /// /// ``` - /// use hexchat_plugin::PluginHandle; + /// use hexchat_plugin::{PluginHandle, TimerHookHandle}; /// - /// fn register_timers(ph: &mut PluginHandle) { + /// fn register_timers(ph: &mut PluginHandle) -> Vec { + /// vec![ /// ph.hook_timer(2000, |ph| { /// ph.print("timer up!"); /// false - /// }); + /// }), + /// ] /// } /// ``` pub fn hook_timer(&mut self, timeout: i32, cb: F) -> TimerHookHandle where F: Fn(&mut PluginHandle) -> bool + 'static + ::std::panic::RefUnwindSafe { -- cgit 1.4.1