forked from poljar/rust-weechat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlines.rs
More file actions
265 lines (220 loc) · 7.78 KB
/
lines.rs
File metadata and controls
265 lines (220 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use std::{borrow::Cow, collections::HashMap, ffi::c_void, marker::PhantomData};
use weechat_sys::{t_hdata, t_weechat_plugin};
use crate::{buffer::Buffer, Weechat};
/// An iterator that steps over the lines of the buffer.
pub struct BufferLines<'a> {
pub(crate) weechat_ptr: *mut t_weechat_plugin,
pub(crate) first_line: *mut c_void,
pub(crate) last_line: *mut c_void,
pub(crate) buffer: PhantomData<&'a Buffer<'a>>,
pub(crate) done: bool,
}
impl<'a> Iterator for BufferLines<'a> {
type Item = BufferLine<'a>;
fn next(&mut self) -> Option<Self::Item> {
if self.done {
None
} else {
let weechat = Weechat::from_ptr(self.weechat_ptr);
let line_hdata = unsafe { weechat.hdata_get("line") };
let line_data_pointer =
unsafe { weechat.hdata_pointer(line_hdata, self.first_line, "data") };
if line_data_pointer.is_null() {
return None;
}
if self.first_line == self.last_line {
self.done = true;
}
self.first_line = unsafe { weechat.hdata_move(line_hdata, self.first_line, 1) };
Some(BufferLine { weechat, line_data_pointer, buffer: PhantomData })
}
}
}
impl DoubleEndedIterator for BufferLines<'_> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.done {
None
} else {
let weechat = Weechat::from_ptr(self.weechat_ptr);
let line_hdata = unsafe { weechat.hdata_get("line") };
let line_data_pointer =
unsafe { weechat.hdata_pointer(line_hdata, self.last_line, "data") };
if line_data_pointer.is_null() {
return None;
}
if self.last_line == self.first_line {
self.done = true;
}
self.last_line = unsafe { weechat.hdata_move(line_hdata, self.last_line, -1) };
Some(BufferLine { weechat, line_data_pointer, buffer: PhantomData })
}
}
}
/// Struct that can be used to update multiple line fields at once.
#[allow(missing_docs)]
#[derive(Debug, Clone, Default)]
pub struct LineData<'a> {
pub prefix: Option<&'a str>,
pub message: Option<&'a str>,
pub date: Option<i64>,
pub date_printed: Option<i64>,
pub tags: Option<&'a [&'a str]>,
}
/// The buffer line, makes it possible to modify the printed message and other
/// line data.
pub struct BufferLine<'a> {
weechat: Weechat,
line_data_pointer: *mut c_void,
buffer: PhantomData<&'a Buffer<'a>>,
}
impl<'a> BufferLine<'a> {
fn hdata(&self) -> *mut t_hdata {
unsafe { self.weechat.hdata_get("line_data") }
}
fn update_line(&self, hashmap: HashMap<&str, &str>) {
unsafe {
self.weechat.hdata_update(self.hdata(), self.line_data_pointer, hashmap);
}
}
/// Get the prefix of the line, everything left of the message separator
/// (usually `|`) is considered the prefix.
pub fn prefix(&self) -> Cow<str> {
unsafe { self.weechat.hdata_string(self.hdata(), self.line_data_pointer, "prefix") }
}
/// Set the prefix to the given new value.
///
/// # Arguments
///
/// * `new_prefix` - The new prefix that should be set on the line.
pub fn set_prefix(&self, new_prefix: &str) {
let mut hashmap = HashMap::new();
hashmap.insert("prefix", new_prefix);
self.update_line(hashmap);
}
/// Get the message of the line.
pub fn message(&self) -> Cow<str> {
unsafe { self.weechat.hdata_string(self.hdata(), self.line_data_pointer, "message") }
}
/// Set the message to the given new value.
///
/// # Arguments
///
/// * `new_value` - The new message that should be set on the line.
pub fn set_message(&self, new_value: &str) {
let mut hashmap = HashMap::new();
hashmap.insert("message", new_value);
self.update_line(hashmap);
}
/// Get the date of the line.
pub fn date(&self) -> isize {
unsafe { self.weechat.hdata_time(self.hdata(), self.line_data_pointer, "date") }
}
/// Set the date to the given new value.
///
/// # Arguments
///
/// * `new_value` - The new date that should be set on the line.
pub fn set_date(&self, new_value: i64) {
let mut hashmap = HashMap::new();
let date = new_value.to_string();
hashmap.insert("date", date.as_ref());
self.update_line(hashmap);
}
/// Get the date the line was printed.
pub fn date_printed(&self) -> isize {
unsafe { self.weechat.hdata_time(self.hdata(), self.line_data_pointer, "date_printed") }
}
/// Set the date the line was printed to the given new value.
///
/// # Arguments
///
/// * `new_value` - The new date that should be set on the line.
pub fn set_date_printed(&self, new_value: &str) {
let mut hashmap = HashMap::new();
let date = new_value.to_string();
hashmap.insert("date_printed", date.as_ref());
self.update_line(hashmap);
}
/// Is the line highlighted.
pub fn highlighted(&self) -> bool {
unsafe { self.weechat.hdata_char(self.hdata(), self.line_data_pointer, "highlight") != 0 }
}
/// Get the list of tags of the line.
pub fn tags(&self) -> Vec<Cow<str>> {
unsafe {
let count = self.weechat.hdata_var_array_size(
self.hdata(),
self.line_data_pointer,
"tags_array",
);
let mut tags = Vec::with_capacity(count as usize);
for i in 0..count {
let tag = self.weechat.hdata_string(
self.hdata(),
self.line_data_pointer,
&format!("{i}|tags_array"),
);
tags.push(tag);
}
tags
}
}
/// Set the tags of the line to the new value.
///
/// # Arguments
///
/// * `new_value` - The new tags that should be set on the line.
pub fn set_tags(&self, new_value: &[&str]) {
let mut hashmap = HashMap::new();
let tags = new_value.join(",");
hashmap.insert("tags_array", tags.as_ref());
self.update_line(hashmap);
}
/// Update multiple fields of the line at once.
///
/// # Arguments
/// * `data` - `LineData` that contains new values that should be set on the
/// line.
///
/// # Example
/// ```no_run
/// # use weechat::Weechat;
/// # use weechat::buffer::{BufferBuilder, LineData};
/// # let buffer_handle = BufferBuilder::new("test")
/// # .build()
/// # .unwrap();
/// # let buffer = buffer_handle.upgrade().unwrap();
/// # let mut lines = buffer.lines();
/// # let line = lines.next().unwrap();
///
/// let new_line = LineData {
/// message: Some("Hello world"),
/// tags: Some(&["First", "Second tag"]),
/// .. Default::default()
/// };
///
/// line.update(new_line)
/// ```
pub fn update(&self, data: LineData<'a>) {
let mut hashmap = HashMap::new();
let tags = data.tags.map(|t| t.join(","));
let date = data.date.map(|d| d.to_string());
let date_printed = data.date_printed.map(|d| d.to_string());
if let Some(message) = data.message {
hashmap.insert("message", message);
}
if let Some(prefix) = data.prefix {
hashmap.insert("prefix", prefix);
}
if let Some(t) = tags.as_ref() {
hashmap.insert("tags_array", t);
}
if let Some(d) = date.as_ref() {
hashmap.insert("date", d);
}
if let Some(d) = date_printed.as_ref() {
hashmap.insert("date_printed", d);
}
self.update_line(hashmap);
}
}