weechat/buffer/
nick.rs

1use std::{borrow::Cow, ffi::CStr, marker::PhantomData};
2
3use crate::{buffer::Buffer, LossyCString, Weechat};
4use weechat_sys::{t_gui_buffer, t_gui_nick, t_weechat_plugin};
5
6/// Settings to create a new nick.
7pub struct NickSettings<'a> {
8    /// Name of the new nick.
9    pub(crate) name: &'a str,
10    /// Color for the nick.
11    pub(crate) color: &'a str,
12    /// Prefix that will be shown before the name.
13    pub(crate) prefix: &'a str,
14    /// Color of the prefix.
15    pub(crate) prefix_color: &'a str,
16    /// Should the nick be visible in the nicklist.
17    pub(crate) visible: bool,
18}
19
20impl<'a> NickSettings<'a> {
21    /// Create new empyt nick creation settings.
22    ///
23    /// # Arguments
24    ///
25    /// * `name` - The name of the new nick.
26    pub fn new(name: &str) -> NickSettings {
27        NickSettings {
28            name,
29            color: "",
30            prefix: "",
31            prefix_color: "",
32            visible: true,
33        }
34    }
35
36    /// Set the color of the nick.
37    ///
38    /// # Arguments
39    ///
40    /// * `color` - The color that the nick should have.
41    pub fn set_color(mut self, color: &'a str) -> NickSettings<'a> {
42        self.color = color;
43        self
44    }
45
46    /// Set the prefix of the nick.
47    ///
48    /// # Arguments
49    ///
50    /// * `prefix` - The prefix displayed before the nick in the nicklist.
51    pub fn set_prefix(mut self, prefix: &'a str) -> NickSettings<'a> {
52        self.prefix = prefix;
53        self
54    }
55
56    /// Set the color of the nick prefix.
57    ///
58    /// # Arguments
59    ///
60    /// * `prefix_color` - The color that the prefix should have.
61    pub fn set_prefix_color(mut self, prefix_color: &'a str) -> NickSettings<'a> {
62        self.prefix_color = prefix_color;
63        self
64    }
65
66    /// Set the visibility of the nick.
67    ///
68    /// # Arguments
69    ///
70    /// * `visible` - Should the nick be visible in the nicklist, `true` if it
71    ///     should be visible, false otherwise. Defaults to `true`.
72    pub fn set_visible(mut self, visible: bool) -> NickSettings<'a> {
73        self.visible = visible;
74        self
75    }
76}
77
78/// Weechat Nick type
79pub struct Nick<'a> {
80    pub(crate) ptr: *mut t_gui_nick,
81    pub(crate) buf_ptr: *mut t_gui_buffer,
82    pub(crate) weechat_ptr: *mut t_weechat_plugin,
83    pub(crate) buffer: PhantomData<&'a Buffer<'a>>,
84}
85
86impl<'a> Nick<'a> {
87    /// Get a Weechat object out of the nick.
88    fn get_weechat(&self) -> Weechat {
89        Weechat::from_ptr(self.weechat_ptr)
90    }
91
92    /// Get a string property of the nick.
93    /// * `property` - The name of the property to get the value for, this can
94    ///     be one of name, color, prefix or prefix_color. If a unknown
95    ///     property is requested an empty string is returned.
96    fn get_string(&self, property: &str) -> Option<Cow<str>> {
97        let weechat = self.get_weechat();
98        let get_string = weechat.get().nicklist_nick_get_string.unwrap();
99        let c_property = LossyCString::new(property);
100        unsafe {
101            let ret = get_string(self.buf_ptr, self.ptr, c_property.as_ptr());
102
103            if ret.is_null() {
104                None
105            } else {
106                Some(CStr::from_ptr(ret).to_string_lossy())
107            }
108        }
109    }
110
111    /// Get the name property of the nick.
112    pub fn name(&self) -> Cow<str> {
113        self.get_string("name").unwrap()
114    }
115
116    /// Get the color of the nick.
117    pub fn color(&self) -> Cow<str> {
118        self.get_string("color").unwrap()
119    }
120
121    /// Get the prefix of the nick.
122    pub fn prefix(&self) -> Cow<str> {
123        self.get_string("prefix").unwrap()
124    }
125
126    /// Get the color of the nick prefix.
127    pub fn prefix_color(&self) -> Cow<str> {
128        self.get_string("prefix_color").unwrap()
129    }
130}