use std::{borrow::Cow, ffi::CStr, marker::PhantomData};
use crate::{buffer::Buffer, LossyCString, Weechat};
use weechat_sys::{t_gui_buffer, t_gui_nick, t_weechat_plugin};
pub struct NickSettings<'a> {
pub(crate) name: &'a str,
pub(crate) color: &'a str,
pub(crate) prefix: &'a str,
pub(crate) prefix_color: &'a str,
pub(crate) visible: bool,
}
impl<'a> NickSettings<'a> {
pub fn new(name: &str) -> NickSettings {
NickSettings {
name,
color: "",
prefix: "",
prefix_color: "",
visible: true,
}
}
pub fn set_color(mut self, color: &'a str) -> NickSettings<'a> {
self.color = color;
self
}
pub fn set_prefix(mut self, prefix: &'a str) -> NickSettings<'a> {
self.prefix = prefix;
self
}
pub fn set_prefix_color(mut self, prefix_color: &'a str) -> NickSettings<'a> {
self.prefix_color = prefix_color;
self
}
pub fn set_visible(mut self, visible: bool) -> NickSettings<'a> {
self.visible = visible;
self
}
}
pub struct Nick<'a> {
pub(crate) ptr: *mut t_gui_nick,
pub(crate) buf_ptr: *mut t_gui_buffer,
pub(crate) weechat_ptr: *mut t_weechat_plugin,
pub(crate) buffer: PhantomData<&'a Buffer<'a>>,
}
impl<'a> Nick<'a> {
fn get_weechat(&self) -> Weechat {
Weechat::from_ptr(self.weechat_ptr)
}
fn get_string(&self, property: &str) -> Option<Cow<str>> {
let weechat = self.get_weechat();
let get_string = weechat.get().nicklist_nick_get_string.unwrap();
let c_property = LossyCString::new(property);
unsafe {
let ret = get_string(self.buf_ptr, self.ptr, c_property.as_ptr());
if ret.is_null() {
None
} else {
Some(CStr::from_ptr(ret).to_string_lossy())
}
}
}
pub fn name(&self) -> Cow<str> {
self.get_string("name").unwrap()
}
pub fn color(&self) -> Cow<str> {
self.get_string("color").unwrap()
}
pub fn prefix(&self) -> Cow<str> {
self.get_string("prefix").unwrap()
}
pub fn prefix_color(&self) -> Cow<str> {
self.get_string("prefix_color").unwrap()
}
}