tui-realm-treeview 4.0.0

Treeview component for tui-realm
Documentation
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! # Tree State
//!
//! This module implements the tree state.

use super::Node;

/// Tree state tracks the current state for the component tree.
#[derive(Default, Clone)]
pub struct TreeState {
    /// Tracks open nodes
    open: Vec<String>,
    /// Current selected item
    selected: Option<String>,
}

impl TreeState {
    /// Returns whether `node` is open
    pub fn is_open<V>(&self, node: &Node<V>) -> bool {
        self.open.contains(node.id())
    }

    /// Returns whether `node` is closed
    pub fn is_closed<V>(&self, node: &Node<V>) -> bool {
        !self.is_open(node)
    }

    /// Get current selected item
    pub fn selected(&self) -> Option<&str> {
        self.selected.as_deref()
    }

    /// Returns whether provided node is currently selected
    pub fn is_selected<V>(&self, node: &Node<V>) -> bool {
        self.selected
            .as_ref()
            .map(|x| x == node.id())
            .unwrap_or(false)
    }

    /// Get first sibling in children of current selected node's parent
    pub fn first_sibling<'a, V>(&self, tree: &'a Node<V>) -> Option<&'a Node<V>> {
        let selected = self.selected.as_ref()?;
        let parent = tree.parent(selected)?;
        parent.iter().next()
    }

    /// Get last sibling in children of current selected node's parent
    pub fn last_sibling<'a, V>(&self, tree: &'a Node<V>) -> Option<&'a Node<V>> {
        let selected = self.selected.as_ref()?;
        let parent = tree.parent(selected)?;
        parent.iter().last()
    }

    /// The tree has changed, so this method must check whether to keep states or not
    pub fn tree_changed<V>(&mut self, root: &Node<V>, preserve: bool) {
        if preserve {
            // Check whether selected is still valid; if doesn't exist, use root
            self.selected = self
                .selected
                .take()
                .map(|selected| root.query(&selected).unwrap_or(root).id().to_string());
            // Check whether open nodes still exist
            self.open.retain(|x| root.query(x).is_some());
        } else {
            // Reset state
            self.open = Vec::new();
            self.selected = Some(root.id().to_string());
        }
    }

    /// Open currently selected `node`. Node can be open only if it is closed and it is NOT a leaf
    pub fn open<V>(&mut self, root: &Node<V>) {
        if let Some(selected) = self.selected.as_ref()
            && let Some(node) = root.query(selected)
        {
            self.open_node(root, node);
        }
    }

    /// Close currently selected `node`.
    /// If node has children, then all children are closed recursively
    pub fn close<V>(&mut self, root: &Node<V>) {
        if let Some(selected) = self.selected.as_ref()
            && let Some(node) = root.query(selected)
            && self.is_open(node)
        {
            self.close_node(node);
        }
    }

    /// Move cursor down in current tree from current position. Rewind if required
    pub fn move_down<V>(&mut self, root: &Node<V>) {
        if let Some(selected) = self.selected.take() {
            // Get current node
            if let Some(node) = root.query(&selected) {
                // If node is open, then move to its first child
                if !node.is_leaf() && self.is_open(node) {
                    // NOTE: unwrap is safe; checked by `is_leaf()`
                    self.selected = Some(node.iter().next().unwrap().id().to_string());
                } else {
                    // If has a "next sibling", let's get it
                    if let Some(sibling) = self.next_sibling(root, node) {
                        self.selected = Some(sibling.id().to_string());
                    } else {
                        // Then the next element becomes the next sibling of the parent
                        // this thing has to be performed recursively for all parents, until one is found (or root is reached)
                        let mut current = &selected;
                        loop {
                            if let Some(parent) = root.parent(current) {
                                current = parent.id();
                                if let Some(sibling) = self.next_sibling(root, parent) {
                                    self.selected = Some(sibling.id().to_string());
                                    break;
                                }
                            } else {
                                // has no parent, keep selectd
                                self.selected = Some(selected);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    /// Move cursor up in current tree from current position. Rewind if required
    pub fn move_up<V>(&mut self, root: &Node<V>) {
        if let Some(selected) = self.selected.take() {
            // Get parent
            if let Some(parent) = root.parent(&selected) {
                // Selected becomes previous sibling's last child; or if None, the parent
                self.selected = Some(
                    self.previous_sibling(root, root.query(&selected).unwrap())
                        .map(|x| self.get_last_open_heir(x))
                        .unwrap_or(parent)
                        .id()
                        .to_string(),
                );
            } else {
                // Is root; then keep selected
                self.selected = Some(selected);
            }
        }
    }

    /// Set current selected node.
    /// When selecting a node, all its ancestors will be opened
    pub fn select<V>(&mut self, root: &Node<V>, node: &Node<V>) {
        self.open_ancestors(root, node);
        self.selected = Some(node.id().to_string());
    }

    /// Close `node`
    fn close_node<V>(&mut self, node: &Node<V>) {
        // Remove from open nodes
        self.open.retain(|x| x != node.id());
        // Close children for node
        self.close_children(node);
    }

    /// Open provided node
    /// Node is opened only if is NOT a leaf and it is closed.
    /// It will also open all the ancestors for `node`
    fn open_node<V>(&mut self, root: &Node<V>, node: &Node<V>) {
        if !node.is_leaf() && self.is_closed(node) {
            self.open.push(node.id().to_string());
        }
        self.open_ancestors(root, node);
    }

    /// Close all node children recursively
    fn close_children<V>(&mut self, node: &Node<V>) {
        node.iter().for_each(|x| self.close_node(x));
    }

    /// Open all ancestors for `node` in the current `tree`
    fn open_ancestors<V>(&mut self, root: &Node<V>, node: &Node<V>) {
        if let Some(parent) = root.parent(node.id()) {
            self.open_node(root, parent);
        }
    }

    /// Returns the previous sibling of `node` in `root`
    fn previous_sibling<'a, V>(
        &mut self,
        root: &'a Node<V>,
        node: &'a Node<V>,
    ) -> Option<&'a Node<V>> {
        let parent = root.parent(node.id())?;
        let mut prev_node = None;
        for child in parent.iter() {
            if child.id() == node.id() {
                break;
            }
            prev_node = Some(child);
        }
        prev_node
    }

    /// Returs next sibling of `node` in `tree`
    fn next_sibling<'a, V>(&mut self, root: &'a Node<V>, node: &'a Node<V>) -> Option<&'a Node<V>> {
        let parent = root.parent(node.id())?;
        let mut keep_next = false;
        for child in parent.iter() {
            if keep_next {
                // Return child
                return Some(child);
            } else if child.id() == node.id() {
                // keep next element
                keep_next = true;
            }
        }
        // No next sibling
        None
    }

    /// Get last open heir for node
    fn get_last_open_heir<'a, V>(&self, node: &'a Node<V>) -> &'a Node<V> {
        if self.is_open(node) {
            // If node is open, get its last child and call this function recursively
            if let Some(child) = node.iter().last() {
                self.get_last_open_heir(child)
            } else {
                node
            }
        } else {
            // Else return `node`
            node
        }
    }

    #[cfg(test)]
    /// Force open nodes
    pub fn force_open(&mut self, open: &[&str]) {
        self.open = open.iter().map(|x| x.to_string()).collect();
    }
}

#[cfg(test)]
mod test {

    use pretty_assertions::assert_eq;

    use super::*;
    use crate::mock::mock_tree;

    #[test]
    fn should_initialize_tree_state() {
        let state = TreeState::default();
        assert!(state.open.is_empty());
        assert!(state.selected().is_none());
    }

    #[test]
    fn should_select_nodes() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        // select 'bA'
        state.select(tree.root(), tree.root().query(&String::from("bA")).unwrap());
        assert_eq!(state.selected().unwrap(), "bA");
        // All ancestors should be opened
        assert_eq!(state.open.len(), 2);
        assert!(state.is_open(tree.root().query(&String::from("b")).unwrap()));
        assert!(state.is_open(tree.root()));
    }

    #[test]
    fn should_open_and_close_nodes() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        // Select node 'bA'
        let ba = tree.root().query(&String::from("bA")).unwrap();
        state.select(tree.root(), ba);
        // Open
        state.open(tree.root());
        assert!(state.is_open(ba));
        // At this point also its ancestors should be open
        assert!(state.is_open(tree.root().query(&String::from("b")).unwrap()));
        assert!(state.is_open(tree.root()));
        assert_eq!(state.open.len(), 3);
        // let's open its child 'bA0'
        let ba0 = tree.root().query(&String::from("bA0")).unwrap();
        state.select(tree.root(), ba0);
        state.open(tree.root());
        assert!(state.is_open(ba0));
        assert_eq!(state.open.len(), 4);
        // Now let's close 'b'
        let b = tree.root().query(&String::from("b")).unwrap();
        state.select(tree.root(), b);
        state.close(tree.root());
        assert!(state.is_closed(b));
        // All its children should be close
        assert!(state.is_closed(ba));
        assert!(state.is_closed(ba0));
        // But root should still be open
        assert!(state.is_open(tree.root()));
    }

    #[test]
    fn should_not_open_twice() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        // Open root
        state.select(tree.root(), tree.root());
        state.open(tree.root());
        assert_eq!(state.open.len(), 1);
        assert!(state.is_open(tree.root()));
        // Open twice
        state.open(tree.root());
        assert_eq!(state.open.len(), 1);
        assert!(state.is_open(tree.root()));
    }

    #[test]
    fn should_find_previous_sibling() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        let bb4 = tree.root().query(&String::from("bB4")).unwrap();
        // Prev siblign should be bb3
        let bb3 = tree.root().query(&String::from("bB3")).unwrap();
        assert_eq!(state.previous_sibling(tree.root(), bb4).unwrap(), bb3);
        // bb0 shouldn't have a previous sibling
        let bb0 = tree.root().query(&String::from("bB0")).unwrap();
        assert!(state.previous_sibling(tree.root(), bb0).is_none());
    }

    #[test]
    fn should_find_next_sibling() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        let bb4 = tree.root().query(&String::from("bB4")).unwrap();
        // Next siblign should be bb3
        let bb5 = tree.root().query(&String::from("bB5")).unwrap();
        assert_eq!(state.next_sibling(tree.root(), bb4).unwrap(), bb5);
        // bb5 shouldn't have a previous sibling
        assert!(state.next_sibling(tree.root(), bb5).is_none());
    }

    #[test]
    fn should_find_first_sibling() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        let bb4 = tree.root().query(&String::from("bB4")).unwrap();
        state.select(tree.root(), bb4);
        // First siblign should be bb1
        let bb0 = tree.root().query(&String::from("bB0")).unwrap();
        assert_eq!(state.first_sibling(tree.root()).unwrap(), bb0);
        // / shouldn't have a first sibling
        state.select(tree.root(), tree.root());
        assert!(state.first_sibling(tree.root()).is_none());
    }

    #[test]
    fn should_find_last_sibling() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        let bb2 = tree.root().query(&String::from("bB2")).unwrap();
        state.select(tree.root(), bb2);
        // First siblign should be bb1
        let bb5 = tree.root().query(&String::from("bB5")).unwrap();
        assert_eq!(state.last_sibling(tree.root()).unwrap(), bb5);
        // / shouldn't have a last sibling
        state.select(tree.root(), tree.root());
        assert!(state.last_sibling(tree.root()).is_none());
    }

    #[test]
    fn should_preserve_tree_state() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        // Open cA branch
        let ca = tree.root().query(&String::from("cA")).unwrap();
        state.select(tree.root(), ca);
        state.open(tree.root());
        let bb5 = tree.root().query(&String::from("bB5")).unwrap();
        state.select(tree.root(), bb5);
        // Reinitialize tree
        state.tree_changed(tree.root(), true);
        // States should have been preserved
        assert_eq!(state.open.len(), 5);
        assert!(state.is_open(ca));
        assert_eq!(state.selected().unwrap(), "bB5");
    }

    #[test]
    fn should_not_preserve_tree_state() {
        let mut state = TreeState::default();
        let mut tree = mock_tree();
        // Open cA branch
        let ca = tree.root().query(&String::from("cA")).unwrap();
        state.select(tree.root(), ca);
        state.open(tree.root());
        let bb5 = tree.root().query(&String::from("bB5")).unwrap();
        state.select(tree.root(), bb5);
        // New tree without 'c' and without 'bB'
        tree.root_mut()
            .query_mut(&String::from("b"))
            .unwrap()
            .remove_child(&String::from("bB"));
        tree.root_mut().remove_child(&String::from("c"));
        // Re initialize tree
        state.tree_changed(tree.root(), true);
        // Select should be root
        assert_eq!(state.selected().unwrap(), "/");
        // No node should be open, except for root and 'b'
        assert_eq!(state.open.len(), 2);
        assert_eq!(state.open, vec![String::from("/"), String::from("b")]);
        assert!(state.is_open(tree.root()));
    }

    #[test]
    fn should_reinitialize_tree_state() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        // Open branches
        let ca = tree.root().query(&String::from("cA")).unwrap();
        state.select(tree.root(), ca);
        state.open(tree.root());
        let bb5 = tree.root().query(&String::from("bB5")).unwrap();
        state.select(tree.root(), bb5);
        // Re-initialize tree
        state.tree_changed(tree.root(), false);
        // No node should be open; selected should be root
        assert!(state.open.is_empty());
        assert_eq!(state.selected().unwrap(), "/");
    }

    #[test]
    fn should_move_cursor_down_on_sibling() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        // Let's start from bB5
        let bb3 = tree.root().query(&String::from("bB3")).unwrap();
        state.select(tree.root(), bb3);
        // Move down (should become bb4)
        state.move_down(tree.root());
        assert_eq!(state.selected().unwrap(), "bB4");
        // Move down (should become bb5)
        state.move_down(tree.root());
        assert_eq!(state.selected().unwrap(), "bB5");
    }

    #[test]
    fn should_move_cursor_down_to_next_lower_node_if_last_child() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        // Let's start from bB5
        let bb5 = tree.root().query(&String::from("bB5")).unwrap();
        state.select(tree.root(), bb5);
        // Move down (should become 'c')
        state.move_down(tree.root());
        assert_eq!(state.selected().unwrap(), "c");
    }

    #[test]
    fn should_move_cursor_down_to_child_if_parent_is_open() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        // Let's start from b
        let b = tree.root().query(&String::from("b")).unwrap();
        state.select(tree.root(), b);
        // Open b
        state.open(tree.root());
        // Move down (should become 'bA')
        state.move_down(tree.root());
        assert_eq!(state.selected().unwrap(), "bA");
        // Open bA
        state.open(tree.root());
        // Move down (should become 'bA0')
        state.move_down(tree.root());
        assert_eq!(state.selected().unwrap(), "bA0");
    }

    #[test]
    fn should_move_cursor_down_to_next_sibling_if_node_is_closed() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        // Let's start from b
        let aa = tree.root().query(&String::from("aA")).unwrap();
        state.select(tree.root(), aa);
        // Move down (should become 'aB')
        state.move_down(tree.root());
        assert_eq!(state.selected().unwrap(), "aB");
        // Move down (should become 'aC')
        state.move_down(tree.root());
        assert_eq!(state.selected().unwrap(), "aC");
    }

    #[test]
    fn should_not_move_cursor_down_if_root_is_closed() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        state.select(tree.root(), tree.root());
        // Move cursor down
        state.move_down(tree.root());
        assert_eq!(state.selected().unwrap(), "/");
    }

    #[test]
    fn should_not_move_cursor_down_if_last_element_is_selected() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        let ca2 = tree.root().query(&String::from("cA2")).unwrap();
        state.select(tree.root(), ca2);
        // Move cursor down (keep last element)
        state.move_down(tree.root());
        assert_eq!(state.selected().unwrap(), "cA2");
    }

    #[test]
    fn should_move_cursor_up_on_sibling() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        // Let's start from bB5
        let bb4 = tree.root().query(&String::from("bB4")).unwrap();
        state.select(tree.root(), bb4);
        // Move up (should become 'bB3')
        state.move_up(tree.root());
        assert_eq!(state.selected().unwrap(), "bB3");
        // Move up (should become 'bB2')
        state.move_up(tree.root());
        assert_eq!(state.selected().unwrap(), "bB2");
    }

    #[test]
    fn should_move_cursor_up_on_deepest_child_of_previous_sibling() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        // Open 'bB'
        state.select(tree.root(), tree.root().query(&String::from("bB")).unwrap());
        state.open(tree.root());
        // Select 'c'
        state.select(tree.root(), tree.root().query(&String::from("c")).unwrap());
        // Move up (should become 'bB5')
        state.move_up(tree.root());
        assert_eq!(state.selected().unwrap(), "bB5");
    }

    #[test]
    fn should_move_cursor_up_to_parent_if_first_child() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        // Open 'bB'
        state.select(
            tree.root(),
            tree.root().query(&String::from("bB0")).unwrap(),
        );
        // Move up (should become 'bB')
        state.move_up(tree.root());
        assert_eq!(state.selected().unwrap(), "bB");
        // Move up (should become 'bA')
        state.move_up(tree.root());
        assert_eq!(state.selected().unwrap(), "bA");
        // Move up (should become 'b')
        state.move_up(tree.root());
        assert_eq!(state.selected().unwrap(), "b");
    }

    #[test]
    fn should_not_move_cursor_up_if_root_is_selected() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        state.select(tree.root(), tree.root());
        // Move cursor down
        state.move_up(tree.root());
        assert_eq!(state.selected().unwrap(), "/");
    }

    #[test]
    fn should_get_last_open_heir() {
        let mut state = TreeState::default();
        let tree = mock_tree();
        // Open aA, aB, aC
        state.select(tree.root(), tree.root().query(&String::from("aA")).unwrap());
        state.open(tree.root());
        state.select(tree.root(), tree.root().query(&String::from("aB")).unwrap());
        state.open(tree.root());
        state.select(tree.root(), tree.root().query(&String::from("aC")).unwrap());
        state.open(tree.root());
        // Open bB
        state.select(tree.root(), tree.root().query(&String::from("bB")).unwrap());
        state.open(tree.root());
        // Get last open heir from root
        assert_eq!(
            state
                .get_last_open_heir(tree.root().query(&String::from("bB")).unwrap())
                .id()
                .as_str(),
            "bB5"
        );
        // Get last open heir from a
        assert_eq!(
            state
                .get_last_open_heir(tree.root().query(&String::from("a")).unwrap())
                .id()
                .as_str(),
            "aC0"
        );
    }

    #[test]
    fn get_last_open_heir_0_children() {
        let mut state = TreeState::default();
        let mut tree = mock_tree();

        state.select(tree.root(), tree.root());
        state.open(tree.root());

        let node = &tree.root().children()[0];
        let node_id = node.id().to_string();
        state.select(tree.root(), node);
        state.open(tree.root());

        tree.root_mut().query_mut(&node_id).unwrap().clear();

        state.get_last_open_heir(&tree.root().children()[0]);
    }
}