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
6pub struct NickSettings<'a> {
8 pub(crate) name: &'a str,
10 pub(crate) color: &'a str,
12 pub(crate) prefix: &'a str,
14 pub(crate) prefix_color: &'a str,
16 pub(crate) visible: bool,
18}
19
20impl<'a> NickSettings<'a> {
21 pub fn new(name: &str) -> NickSettings {
27 NickSettings {
28 name,
29 color: "",
30 prefix: "",
31 prefix_color: "",
32 visible: true,
33 }
34 }
35
36 pub fn set_color(mut self, color: &'a str) -> NickSettings<'a> {
42 self.color = color;
43 self
44 }
45
46 pub fn set_prefix(mut self, prefix: &'a str) -> NickSettings<'a> {
52 self.prefix = prefix;
53 self
54 }
55
56 pub fn set_prefix_color(mut self, prefix_color: &'a str) -> NickSettings<'a> {
62 self.prefix_color = prefix_color;
63 self
64 }
65
66 pub fn set_visible(mut self, visible: bool) -> NickSettings<'a> {
73 self.visible = visible;
74 self
75 }
76}
77
78pub 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 fn get_weechat(&self) -> Weechat {
89 Weechat::from_ptr(self.weechat_ptr)
90 }
91
92 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 pub fn name(&self) -> Cow<str> {
113 self.get_string("name").unwrap()
114 }
115
116 pub fn color(&self) -> Cow<str> {
118 self.get_string("color").unwrap()
119 }
120
121 pub fn prefix(&self) -> Cow<str> {
123 self.get_string("prefix").unwrap()
124 }
125
126 pub fn prefix_color(&self) -> Cow<str> {
128 self.get_string("prefix_color").unwrap()
129 }
130}