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
use crate::{
widgets::{NcSelector, NcSelectorItem, NcSelectorOptions},
NcChannels, NcPlane, NcResult, NcString,
};
#[derive(Default, Debug)]
pub struct NcSelectorBuilder {
title: Option<NcString>,
secondary: Option<NcString>,
footer: Option<NcString>,
items: Vec<(NcString, NcString)>,
default_item: u32,
max_display: u32,
channels: [NcChannels; 5],
flags: u64,
}
impl NcSelectorBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn item(mut self, o: &str, d: &str) -> Self {
self.items.push((NcString::new(o), NcString::new(d)));
self
}
pub fn default_item(mut self, item: u32) -> Self {
self.default_item = item;
self
}
pub fn max_display(mut self, max: u32) -> Self {
self.max_display = max;
self
}
pub fn title(mut self, title: &str) -> Self {
self.title = Some(NcString::new(title));
self
}
pub fn secondary(mut self, secondary: &str) -> Self {
self.secondary = Some(NcString::new(secondary));
self
}
pub fn footer(mut self, footer: &str) -> Self {
self.footer = Some(NcString::new(footer));
self
}
pub fn flags(mut self, flags: u64) -> Self {
self.flags = flags;
self
}
pub fn all_channels(
mut self,
item_opt: impl Into<NcChannels>,
item_desc: impl Into<NcChannels>,
seltitle: impl Into<NcChannels>,
selfooter: impl Into<NcChannels>,
selbox: impl Into<NcChannels>,
) -> Self {
self.channels = [
item_opt.into(),
item_desc.into(),
seltitle.into(),
selfooter.into(),
selbox.into(),
];
self
}
pub fn item_channels(
mut self,
opt: impl Into<NcChannels>,
desc: impl Into<NcChannels>,
) -> Self {
self.channels[0] = opt.into();
self.channels[1] = desc.into();
self
}
pub fn title_channels(mut self, title: impl Into<NcChannels>) -> Self {
self.channels[2] = title.into();
self
}
pub fn secondary_channels(mut self, secondary: impl Into<NcChannels>) -> Self {
self.channels[3] = secondary.into();
self
}
pub fn box_channels(mut self, r#box: impl Into<NcChannels>) -> Self {
self.channels[4] = r#box.into();
self
}
pub fn finish(self, plane: &mut NcPlane) -> NcResult<&mut NcSelector> {
let mut selitems = vec![];
for (o, d) in self.items.iter() {
selitems.push(NcSelectorItem::new(o, d));
}
selitems.push(NcSelectorItem::new_empty());
let default_item = std::cmp::min(self.default_item, selitems.len() as u32 - 1);
let options = NcSelectorOptions::with_all_options(
self.title.as_ref(),
self.secondary.as_ref(),
self.footer.as_ref(),
&selitems,
default_item,
self.max_display,
self.channels[0],
self.channels[1],
self.channels[2],
self.channels[3],
self.channels[4],
);
NcSelector::new(plane, &options)
}
}