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
use crate::{Id, Mux, Orientation};
pub struct Path<'a> {
mux: &'a Mux,
cur_id: Option<Id>,
}
#[derive(Debug, PartialEq)]
pub(crate) enum SearchPath {
Left,
Right,
Up,
Down,
}
impl<'a> Path<'a> {
fn new(mux: &'a Mux, id: Id) -> Self {
Path {
mux,
cur_id: Some(id),
}
}
pub fn build(self) -> Option<Id> {
if let Some(node) = self.cur_id {
if self.mux.tree.get(node).unwrap().get().has_view() {
self.cur_id
} else {
None
}
} else {
self.cur_id
}
}
pub fn up(self) -> Self {
self.next_node(SearchPath::Up, Orientation::Vertical)
}
pub fn down(self) -> Self {
self.next_node(SearchPath::Down, Orientation::Vertical)
}
pub fn left(self) -> Self {
self.next_node(SearchPath::Left, Orientation::Horizontal)
}
pub fn right(self) -> Self {
self.next_node(SearchPath::Right, Orientation::Horizontal)
}
fn next_node(mut self, direction: SearchPath, orit: Orientation) -> Self {
if let Some(node) = self.cur_id {
if node.children(&self.mux.tree).count() > 0 {
if let Some(node_content) = self.mux.tree.get(node) {
match node_content.get().orientation {
_ if node_content.get().orientation == orit => {
if let Some(new) = node.children(&self.mux.tree).nth(match direction {
SearchPath::Up | SearchPath::Left => 0,
SearchPath::Right | SearchPath::Down => 1,
}) {
self.cur_id = Some(new);
} else {
self.cur_id = None;
}
}
_ => {
println!("ello");
self.cur_id = None;
}
}
} else {
self.cur_id = None;
}
}
}
self
}
}
impl Mux {
pub fn root(&self) -> Path {
Path::new(self, self.root)
}
}
#[cfg(test)]
mod test {
use super::Mux;
use cursive::views::DummyView;
#[test]
fn path_up() {
let (mut mux, node1) = Mux::new(DummyView);
mux.add_below(DummyView, node1).unwrap();
let upper_pane = mux.root().up().build();
assert!(upper_pane.is_some());
assert_eq!(node1, upper_pane.unwrap());
}
#[test]
fn path_down() {
let (mut mux, node1) = Mux::new(DummyView);
let node2 = mux.add_below(DummyView, node1).unwrap();
let lower_pane = mux.root().down().build();
assert!(lower_pane.is_some());
assert_eq!(node2, lower_pane.unwrap());
}
#[test]
fn path_left() {
let (mut mux, node1) = Mux::new(DummyView);
mux.add_right_of(DummyView, node1).unwrap();
let left_pane = mux.root().left().build();
assert!(left_pane.is_some());
assert_eq!(node1, left_pane.unwrap());
}
#[test]
fn path_right() {
let (mut mux, node1) = Mux::new(DummyView);
let node2 = mux.add_right_of(DummyView, node1).unwrap();
let right_pane = mux.root().right().build();
assert!(right_pane.is_some());
assert_eq!(node2, right_pane.unwrap());
}
#[test]
fn path_invalid() {
let (mut mux, node1) = Mux::new(DummyView);
let _ = mux.add_right_of(DummyView, node1).unwrap();
let root_pane = mux.root().build();
assert!(root_pane.is_none());
}
}