hexchat_unsafe_plugin/
strip.rs

1// This file is part of Hexchat Plugin API Bindings for Rust
2// Copyright (C) 2022 Soni L.
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as
6// published by the Free Software Foundation, either version 3 of the
7// License, or (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17/// Stripping mode for [`PluginHandle::strip`](crate::PluginHandle::strip).
18#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Default)]
19pub struct Strip {
20    // 4
21    hidden: bool,
22    // 2
23    formatting: bool,
24    // 1
25    colors: bool,
26}
27
28impl Strip {
29    /// Creates a new `Strip` that, by default, strips no attributes.
30    ///
31    /// # Examples
32    ///
33    /// ```no_run
34    /// use hexchat_unsafe_plugin::{PluginHandle, Strip};
35    ///
36    /// fn strip_nothing(s: &str, ph: &PluginHandle<'_>) -> String {
37    ///     ph.strip(s, Strip::new())
38    /// }
39    /// ```
40    #[inline]
41    pub const fn new() -> Strip {
42        Strip {
43            hidden: false,
44            formatting: false,
45            colors: false,
46        }
47    }
48
49    /// Sets whether to remove mIRC color attributes.
50    ///
51    /// # Examples
52    ///
53    /// ```no_run
54    /// use hexchat_unsafe_plugin::{PluginHandle, Strip};
55    ///
56    /// fn strip_colors(s: &str, ph: &PluginHandle<'_>) -> String {
57    ///     ph.strip(s, Strip::new().colors(true))
58    /// }
59    /// ```
60    #[inline]
61    pub const fn colors(mut self, strip: bool) -> Self {
62        self.colors = strip;
63        self
64    }
65
66    /// Sets whether to remove formatting attributes.
67    ///
68    /// # Examples
69    ///
70    /// ```no_run
71    /// use hexchat_unsafe_plugin::{PluginHandle, Strip};
72    ///
73    /// fn strip_formatting(s: &str, ph: &PluginHandle<'_>) -> String {
74    ///     ph.strip(s, Strip::new().formatting(true))
75    /// }
76    /// ```
77    #[inline]
78    pub const fn formatting(mut self, strip: bool) -> Self {
79        self.formatting = strip;
80        self
81    }
82
83    /// Sets whether to remove internal "hidden text" formatting attributes.
84    ///
85    /// This is split from [`Self::formatting`] because these attributes are
86    /// only processed when writing directly to a buffer - they're for
87    /// internal/plugin use. This tends to be useful when processing user or
88    /// remote input and writing it directly to a buffer.
89    ///
90    /// # Examples
91    ///
92    /// ```no_run
93    /// use hexchat_unsafe_plugin::{PluginHandle, Strip};
94    ///
95    /// fn strip_hidden(s: &str, ph: &PluginHandle<'_>) -> String {
96    ///     ph.strip(s, Strip::new().hidden(true))
97    /// }
98    /// ```
99    #[inline]
100    pub const fn hidden(mut self, strip: bool) -> Self {
101        self.hidden = strip;
102        self
103    }
104
105    /// Creates a new `Strip` that strips all strippable attributes.
106    ///
107    /// # Examples
108    ///
109    /// ```no_run
110    /// use hexchat_unsafe_plugin::{PluginHandle, Strip};
111    ///
112    /// fn strip_all(s: &str, ph: &PluginHandle<'_>) -> String {
113    ///     ph.strip(s, Strip::all())
114    /// }
115    /// ```
116    #[inline]
117    pub const fn all() -> Strip {
118        Strip {
119            hidden: true,
120            formatting: true,
121            colors: true,
122        }
123    }
124
125    /// Builds the flags for FFI.
126    pub(crate) fn flags(self) -> ::libc::c_int {
127        let mut value = 0;
128        if self.hidden {
129            value |= 4;
130        }
131        if self.formatting {
132            value |= 2;
133        }
134        if self.colors {
135            value |= 1;
136        }
137        value
138    }
139}