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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use crate::Error;
use crate::Layout;
use crate::WindowFlag;
#[cfg(feature = "tmux_2_1")]
use std::time::Duration;

pub const WINDOW_ACTIVE: u64 = 1;
pub const WINDOW_ACTIVE_CLIENTS: u64 = 1 << 1;
pub const WINDOW_ACTIVE_CLIENTS_LIST: u64 = 1 << 2;
pub const WINDOW_ACTIVE_SESSIONS: u64 = 1 << 3;
pub const WINDOW_ACTIVE_SESSIONS_LIST: u64 = 1 << 4;
pub const WINDOW_ACTIVITY: u64 = 1 << 5;
pub const WINDOW_ACTIVITY_STRING: u64 = 1 << 6;
pub const WINDOW_ACTIVITY_FLAG: u64 = 1 << 7;
pub const WINDOW_BELL_FLAG: u64 = 1 << 8;
pub const WINDOW_CONTENT_FLAG: u64 = 1 << 9;
pub const WINDOW_BIGGER: u64 = 1 << 10;
pub const WINDOW_CELL_HEIGHT: u64 = 1 << 11;
pub const WINDOW_CELL_WIDTH: u64 = 1 << 12;
pub const WINDOW_END_FLAG: u64 = 1 << 13;
pub const WINDOW_FIND_MATCHES: u64 = 1 << 14;
pub const WINDOW_FLAGS: u64 = 1 << 15;
pub const WINDOW_FORMAT: u64 = 1 << 16;
pub const WINDOW_HEIGHT: u64 = 1 << 17;
pub const WINDOW_ID: u64 = 1 << 18;
pub const WINDOW_INDEX: u64 = 1 << 19;
pub const WINDOW_LAST_FLAG: u64 = 1 << 20;
pub const WINDOW_LAYOUT: u64 = 1 << 21;
pub const WINDOW_LINKED: u64 = 1 << 22;
pub const WINDOW_LINKED_SESSIONS: u64 = 1 << 23;
pub const WINDOW_LINKED_SESSIONS_LIST: u64 = 1 << 24;
pub const WINDOW_MARKED_FLAG: u64 = 1 << 25;
pub const WINDOW_NAME: u64 = 1 << 26;
pub const WINDOW_OFFSET_X: u64 = 1 << 27;
pub const WINDOW_OFFSET_Y: u64 = 1 << 28;
pub const WINDOW_PANES: u64 = 1 << 29;
pub const WINDOW_SILENCE_FLAG: u64 = 1 << 30;
pub const WINDOW_STACK_INDEX: u64 = 1 << 31;
pub const WINDOW_START_FLAG: u64 = 1 << 32;
pub const WINDOW_VISIBLE_LAYOUT: u64 = 1 << 33;
pub const WINDOW_WIDTH: u64 = 1 << 34;
pub const WINDOW_ZOOMED_FLAG: u64 = 1 << 35;

// XXX: number of all flags, needed for array init
// NOTE: variables were first intoduced in tmux 1.6
#[cfg(all(feature = "tmux_1_6", not(feature = "tmux_1_7")))]
pub const WINDOW_FLAGS_NUM: usize = 7;
#[cfg(all(feature = "tmux_1_7", not(feature = "tmux_1_8")))]
pub const WINDOW_FLAGS_NUM: usize = 10;
#[cfg(all(feature = "tmux_1_8", not(feature = "tmux_1_9")))]
pub const WINDOW_FLAGS_NUM: usize = 10;
#[cfg(all(feature = "tmux_1_9", not(feature = "tmux_1_9a")))]
pub const WINDOW_FLAGS_NUM: usize = 14;
#[cfg(all(feature = "tmux_1_9a", not(feature = "tmux_2_0")))]
pub const WINDOW_FLAGS_NUM: usize = 14;
#[cfg(all(feature = "tmux_2_0", not(feature = "tmux_2_1")))]
pub const WINDOW_FLAGS_NUM: usize = 15;
#[cfg(all(feature = "tmux_2_1", not(feature = "tmux_2_2")))]
pub const WINDOW_FLAGS_NUM: usize = 18;
#[cfg(all(feature = "tmux_2_2", not(feature = "tmux_2_3")))]
pub const WINDOW_FLAGS_NUM: usize = 17;
#[cfg(all(feature = "tmux_2_3", not(feature = "tmux_2_4")))]
pub const WINDOW_FLAGS_NUM: usize = 18;
#[cfg(all(feature = "tmux_2_4", not(feature = "tmux_2_5")))]
pub const WINDOW_FLAGS_NUM: usize = 18;
#[cfg(all(feature = "tmux_2_5", not(feature = "tmux_2_6")))]
pub const WINDOW_FLAGS_NUM: usize = 19;
#[cfg(all(feature = "tmux_2_6", not(feature = "tmux_2_7")))]
pub const WINDOW_FLAGS_NUM: usize = 19;
#[cfg(all(feature = "tmux_2_7", not(feature = "tmux_2_8")))]
pub const WINDOW_FLAGS_NUM: usize = 19;
#[cfg(all(feature = "tmux_2_8", not(feature = "tmux_2_9")))]
pub const WINDOW_FLAGS_NUM: usize = 19;
#[cfg(all(feature = "tmux_2_9", not(feature = "tmux_2_9a")))]
pub const WINDOW_FLAGS_NUM: usize = 24;
#[cfg(all(feature = "tmux_2_9a", not(feature = "tmux_3_0")))]
pub const WINDOW_FLAGS_NUM: usize = 24;
#[cfg(all(feature = "tmux_3_0", not(feature = "tmux_3_0a")))]
pub const WINDOW_FLAGS_NUM: usize = 24;
#[cfg(all(feature = "tmux_3_0a", not(feature = "tmux_3_1")))]
pub const WINDOW_FLAGS_NUM: usize = 24;
#[cfg(all(feature = "tmux_3_1", not(feature = "tmux_3_1a")))]
pub const WINDOW_FLAGS_NUM: usize = 33;
#[cfg(all(feature = "tmux_3_1a", not(feature = "tmux_3_1b")))]
pub const WINDOW_FLAGS_NUM: usize = 33;
#[cfg(all(feature = "tmux_3_1b", not(feature = "tmux_X_X")))]
pub const WINDOW_FLAGS_NUM: usize = 33;
#[cfg(feature = "tmux_X_X")]
pub const WINDOW_FLAGS_NUM: usize = 33;

pub const WINDOW_NONE: u64 = 0;
//pub const WINDOW_DEFAULT: usize = WINDOW_ID | WINDOW_NAME;
pub const WINDOW_ALL: u64 = WINDOW_ACTIVE
    | WINDOW_ACTIVE_CLIENTS
    | WINDOW_ACTIVE_CLIENTS_LIST
    | WINDOW_ACTIVE_SESSIONS
    | WINDOW_ACTIVE_SESSIONS_LIST
    | WINDOW_ACTIVITY
    | WINDOW_ACTIVITY_STRING
    | WINDOW_ACTIVITY_FLAG
    | WINDOW_BELL_FLAG
    | WINDOW_CONTENT_FLAG
    | WINDOW_BIGGER
    | WINDOW_CELL_HEIGHT
    | WINDOW_CELL_WIDTH
    | WINDOW_END_FLAG
    | WINDOW_FIND_MATCHES
    | WINDOW_FLAGS
    | WINDOW_FORMAT
    | WINDOW_HEIGHT
    | WINDOW_ID
    | WINDOW_INDEX
    | WINDOW_LAST_FLAG
    | WINDOW_LAYOUT
    | WINDOW_LINKED
    | WINDOW_LINKED_SESSIONS
    | WINDOW_LINKED_SESSIONS_LIST
    | WINDOW_MARKED_FLAG
    | WINDOW_NAME
    | WINDOW_OFFSET_X
    | WINDOW_OFFSET_Y
    | WINDOW_PANES
    | WINDOW_SILENCE_FLAG
    | WINDOW_STACK_INDEX
    | WINDOW_START_FLAG
    | WINDOW_VISIBLE_LAYOUT
    | WINDOW_WIDTH
    | WINDOW_ZOOMED_FLAG;

pub const WINDOW_VARS_SEPARATOR: &str = "'";
// FIXME: regex name can be anything, and other keys should be checked better
pub const WINDOW_VARS: [(&str, u64, fn(w: &mut Window, p: &str)); WINDOW_FLAGS_NUM] = [
    #[cfg(feature = "tmux_1_6")]
    ("window_active", WINDOW_ACTIVE, |w, p| {
        w.active = p.parse::<usize>().map(|i| i == 1).ok()
    }),
    #[cfg(feature = "tmux_3_1")]
    ("window_active_clients", WINDOW_ACTIVE_CLIENTS, |w, p| {
        w.active_clients = p.parse().ok()
    }),
    #[cfg(feature = "tmux_3_1")]
    (
        "window_active_clients_list",
        WINDOW_ACTIVE_CLIENTS_LIST,
        |w, p| w.active_clients_list = p.parse().ok(),
    ),
    #[cfg(feature = "tmux_3_1")]
    ("window_active_sessions", WINDOW_ACTIVE_SESSIONS, |w, p| {
        w.active_sessions = p.parse().ok()
    }),
    #[cfg(feature = "tmux_3_1")]
    (
        "window_active_sessions_list",
        WINDOW_ACTIVE_SESSIONS_LIST,
        |w, p| w.active_sessions_list = p.parse().ok(),
    ),
    #[cfg(feature = "tmux_2_1")]
    ("window_activity", WINDOW_ACTIVITY, |w, p| {
        w.activity = p.parse().ok().map(Duration::from_secs)
    }),
    #[cfg(all(feature = "tmux_2_1", not(feature = "tmux_2_2")))]
    ("window_activity_string", WINDOW_ACTIVITY_STRING, |w, p| {
        w.activity_string = p.parse().ok()
    }),
    #[cfg(any(
        all(feature = "tmux_1_9", not(feature = "tmux_2_2")),
        feature = "tmux_2_3"
    ))]
    ("window_activity_flag", WINDOW_ACTIVITY_FLAG, |w, p| {
        w.activity_flag = p.parse::<usize>().map(|i| i == 1).ok()
    }),
    #[cfg(feature = "tmux_1_9")]
    ("window_bell_flag", WINDOW_BELL_FLAG, |w, p| {
        w.bell_flag = p.parse::<usize>().map(|i| i == 1).ok()
    }),
    #[cfg(all(feature = "tmux_1_9", not(feature = "tmux_2_0")))]
    ("window_content_flag", WINDOW_CONTENT_FLAG, |w, p| {
        w.content_flag = p.parse().ok()
    }),
    #[cfg(feature = "tmux_2_9")]
    ("window_bigger", WINDOW_BIGGER, |w, p| {
        w.bigger = p.parse::<usize>().map(|i| i == 1).ok()
    }),
    #[cfg(feature = "tmux_3_1")]
    ("window_cell_height", WINDOW_CELL_HEIGHT, |w, p| {
        w.cell_height = p.parse().ok()
    }),
    #[cfg(feature = "tmux_3_1")]
    ("window_cell_width", WINDOW_CELL_WIDTH, |w, p| {
        w.cell_width = p.parse().ok()
    }),
    #[cfg(feature = "tmux_2_9")]
    ("window_end_flag", WINDOW_END_FLAG, |w, p| {
        w.end_flag = p.parse::<usize>().map(|i| i == 1).ok()
    }),
    #[cfg(all(feature = "tmux_1_7", not(feature = "tmux_2_6")))]
    ("window_find_matches", WINDOW_FIND_MATCHES, |w, p| {
        w.find_matches = p.parse().ok()
    }),
    #[cfg(feature = "tmux_1_6")]
    ("window_flags", WINDOW_FLAGS, |w, p| {
        w.flags = p.parse().ok()
    }),
    #[cfg(feature = "tmux_2_6")]
    ("window_format", WINDOW_FORMAT, |w, p| {
        w.format = p.parse::<usize>().map(|i| i == 1).ok()
    }),
    #[cfg(feature = "tmux_1_6")]
    ("window_height", WINDOW_HEIGHT, |w, p| {
        w.height = p.parse().ok()
    }),
    #[cfg(feature = "tmux_1_7")]
    ("window_id", WINDOW_ID, |w, p| w.id = p[1..].parse().ok()), // skip `@` char
    #[cfg(feature = "tmux_1_6")]
    ("window_index", WINDOW_INDEX, |w, p| {
        w.index = p.parse().ok()
    }),
    #[cfg(feature = "tmux_2_0")]
    ("window_last_flag", WINDOW_LAST_FLAG, |w, p| {
        w.last_flag = p.parse::<usize>().map(|i| i == 1).ok()
    }),
    #[cfg(feature = "tmux_1_6")]
    ("window_layout", WINDOW_LAYOUT, |w, p| {
        w.layout = p.parse().ok()
    }),
    #[cfg(feature = "tmux_2_1")]
    ("window_linked", WINDOW_LINKED, |w, p| {
        w.linked = p.parse::<usize>().map(|i| i == 1).ok()
    }),
    #[cfg(feature = "tmux_3_1")]
    ("window_linked_sessions", WINDOW_LINKED_SESSIONS, |w, p| {
        w.linked_sessions = p.parse().ok()
    }),
    #[cfg(feature = "tmux_3_1")]
    (
        "window_linked_sessions_list",
        WINDOW_LINKED_SESSIONS_LIST,
        |w, p| w.linked_sessions_list = p.parse().ok(),
    ),
    #[cfg(feature = "tmux_3_1")]
    ("window_marked_flag", WINDOW_MARKED_FLAG, |w, p| {
        w.marked_flag = p.parse().ok()
    }),
    #[cfg(feature = "tmux_1_6")]
    ("window_name", WINDOW_NAME, |w, p| w.name = p.parse().ok()),
    #[cfg(feature = "tmux_2_9")]
    ("window_offset_x", WINDOW_OFFSET_X, |w, p| {
        w.offset_x = p.parse().ok()
    }),
    #[cfg(feature = "tmux_2_9")]
    ("window_offset_y", WINDOW_OFFSET_Y, |w, p| {
        w.offset_y = p.parse().ok()
    }),
    #[cfg(feature = "tmux_1_7")]
    ("window_panes", WINDOW_PANES, |w, p| {
        w.panes = p.parse().ok()
    }),
    #[cfg(feature = "tmux_1_9")]
    ("window_silence_flag", WINDOW_SILENCE_FLAG, |w, p| {
        w.silence_flag = p.parse::<usize>().map(|i| i == 1).ok()
    }),
    #[cfg(feature = "tmux_2_5")]
    ("window_stack_index", WINDOW_STACK_INDEX, |w, p| {
        w.stack_index = p.parse().ok()
    }),
    #[cfg(feature = "tmux_2_9")]
    ("window_start_flag", WINDOW_START_FLAG, |w, p| {
        w.start_flag = p.parse::<usize>().map(|i| i == 1).ok()
    }),
    #[cfg(feature = "tmux_2_2")]
    ("window_visible_layout", WINDOW_VISIBLE_LAYOUT, |w, p| {
        w.visible_layout = p.parse().ok()
    }),
    #[cfg(feature = "tmux_1_6")]
    ("window_width", WINDOW_WIDTH, |w, p| {
        w.width = p.parse().ok()
    }),
    #[cfg(feature = "tmux_2_0")]
    ("window_zoomed_flag", WINDOW_ZOOMED_FLAG, |w, p| {
        w.zoomed_flag = p.parse::<usize>().map(|i| i == 1).ok()
    }),
];

// accordingly to tmux.h: Formats
// XXX: check all types, optionality
#[derive(Default, Clone, PartialEq, Debug)]
pub struct Window {
    /// window_active - 1 if window active
    #[cfg(feature = "tmux_1_6")]
    pub active: Option<bool>,
    /// window_active_clients - Number of clients viewing this window
    #[cfg(feature = "tmux_3_1")]
    pub active_clients: Option<usize>,
    /// window_active_clients_list - List of clients viewing this window
    #[cfg(feature = "tmux_3_1")]
    pub active_clients_list: Option<String>,
    /// window_active_sessions - Number of sessions on which this window is active
    #[cfg(feature = "tmux_3_1")]
    pub active_sessions: Option<usize>,
    /// window_active_sessions_list - List of sessions on which this window is active
    #[cfg(feature = "tmux_3_1")]
    pub active_sessions_list: Option<String>,
    /// window_activity - Time of window last activity
    #[cfg(feature = "tmux_2_1")]
    pub activity: Option<Duration>,
    /// session_activity_string - String time of session last activity
    #[cfg(all(feature = "tmux_2_1", not(feature = "tmux_2_2")))]
    pub activity_string: Option<String>,
    /// window_activity_flag - 1 if window has activity
    #[cfg(any(
        all(feature = "tmux_1_9", not(feature = "tmux_2_2")),
        feature = "tmux_2_3"
    ))]
    pub activity_flag: Option<bool>,
    /// window_bell_flag - 1 if window has bell
    #[cfg(feature = "tmux_1_9")]
    pub bell_flag: Option<bool>,
    /// window_content_flag - 1 if window has content alert
    #[cfg(all(feature = "tmux_1_9", not(feature = "tmux_2_0")))]
    pub content_flag: Option<bool>,
    /// 1 if window is larger than client
    #[cfg(feature = "tmux_2_9")]
    pub bigger: Option<bool>,
    /// window_cell_height - Height of each cell in pixels
    #[cfg(feature = "tmux_3_1")]
    pub cell_height: Option<usize>,
    /// window_cell_width - Width of each cell in pixels
    #[cfg(feature = "tmux_3_1")]
    pub cell_width: Option<usize>,
    /// 1 if window has the highest index
    #[cfg(feature = "tmux_2_9")]
    pub end_flag: Option<bool>,
    /// window_find_matches - Matched data from the find-window command if available
    #[cfg(all(feature = "tmux_1_7", not(feature = "tmux_2_6")))]
    pub find_matches: Option<String>,
    /// window_flags - #F Window flags
    #[cfg(feature = "tmux_1_6")]
    pub flags: Option<WindowFlag>,
    /// 1 if format is for a window
    #[cfg(feature = "tmux_2_6")]
    pub format: Option<bool>,
    /// window_height - Height of window
    #[cfg(feature = "tmux_1_6")]
    pub height: Option<usize>,
    /// window_id - Unique window ID
    #[cfg(feature = "tmux_1_7")]
    pub id: Option<usize>,
    /// window_index - #I Index of window
    #[cfg(feature = "tmux_1_6")]
    pub index: Option<usize>,
    /// window_last_flag - 1 if window is the last used
    #[cfg(feature = "tmux_2_0")]
    pub last_flag: Option<bool>,
    /// window_layout - Window layout description, ignoring zoomed window panes
    #[cfg(feature = "tmux_1_6")]
    pub layout: Option<Layout>,
    /// window_linked - 1 if window is linked across sessions
    #[cfg(feature = "tmux_2_1")]
    pub linked: Option<bool>,
    /// window_linked_sessions - Number of sessions this window is linked to
    #[cfg(feature = "tmux_3_1")]
    pub linked_sessions: Option<usize>,
    /// window_linked_sessions_list - List of sessions this window is linked to
    #[cfg(feature = "tmux_3_1")]
    pub linked_sessions_list: Option<String>,
    /// window_marked_flag - 1 if window contains the marked pane
    #[cfg(feature = "tmux_3_1")]
    pub marked_flag: Option<bool>,
    /// window_name - #W Name of window
    #[cfg(feature = "tmux_1_6")]
    pub name: Option<String>,
    /// X offset into window if larger than client
    #[cfg(feature = "tmux_2_9")]
    pub offset_x: Option<usize>,
    /// Y offset into window if larger than client
    #[cfg(feature = "tmux_2_9")]
    pub offset_y: Option<usize>,
    /// window_panes - Number of panes in window
    #[cfg(feature = "tmux_1_7")]
    pub panes: Option<usize>,
    /// window_silence_flag - 1 if window has silence alert
    #[cfg(feature = "tmux_1_9")]
    pub silence_flag: Option<bool>,
    /// Index in session most recent stack
    #[cfg(feature = "tmux_2_5")]
    pub stack_index: Option<usize>,
    /// 1 if window has the lowest index
    #[cfg(feature = "tmux_2_9")]
    pub start_flag: Option<bool>,
    /// Window layout description, respecting zoomed window panes
    #[cfg(feature = "tmux_2_2")]
    pub visible_layout: Option<Layout>,
    /// window_width - Width of window
    #[cfg(feature = "tmux_1_6")]
    pub width: Option<usize>,
    /// window_zoomed_flag - 1 if window is zoomed
    #[cfg(feature = "tmux_2_0")]
    pub zoomed_flag: Option<bool>,
}

impl Window {
    pub fn new() -> Self {
        Default::default()
    }

    // XXX: mb deserialize like serde something?
    pub fn from_str(s: &str, bitflags: u64) -> Result<Self, Error> {
        let wv: Vec<&str> = s.split(WINDOW_VARS_SEPARATOR).collect();
        let mut wv = wv.iter();
        // XXX: optimize?
        let mut w = Window::new();
        // for all bitflags
        for var in WINDOW_VARS.iter() {
            // is current bitflag given?
            if bitflags & var.1 == var.1 {
                // does vector element exist?
                if let Some(part) = wv.next() {
                    // is vector element not empty
                    if !part.is_empty() {
                        // call corresponding func from array
                        var.2(&mut w, part);
                    }
                }
            }
        }
        Ok(w)
    }
}