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
//! Input fields for allowing the user to enter various kinds of data.
//!
//! The following input fields are defined in this module:
//! - [`Checkbox`] for entering booleans.
//! - [`Radio`] for selecting one item among a set.
//! - [`Slider`] for entering a number in a range.
//! - [`Textbox`] for entering single-line strings.
//! - [`Toggle`] for toggling a set of items on/off.
//!
//! Fields are mainly designed to be used in [forms](crate::dialog::form!), but can be used on their own by
//! feeding key-presses with [`Field::input`] and drawing them using the [`Text`] returned from
//! [`Field::format`].
//!
//!
//! # Custom Fields
//!
//! Custom fields may be created by implementing the [`Field`] trait. See its documentation for more
//! information.
use Text;
use crateKeyEvent;
pub use ;
/// Field builder specification.
///
/// Builders are responsible for providing the methods available when instantiating fields inside a
/// [form](crate::dialog::form!). More specifically, the DSL
/// `Textbox{ name: "Password", value: "admin", hidden }` gets (loosely) translated as:
/// ```no_run
/// # use tundra::field::{Field, Build, textbox::{Textbox, Builder}};
/// # let _ =
/// Textbox::builder()
/// .name("Password")
/// .value("admin")
/// .hidden()
/// .build()
/// # ;
/// ```
///
/// Three restrictions are placed on field builder types:
/// 1. They must implement [`Default`].
/// 2. They must implement [`Build`].
/// 3. All methods can take at most one argument.
///
/// For maximal flexibility, the second restriction is not added as a bound to [`Field::Builder`]. This
/// allows the [`Build`] trait implementation to be predicated on type-state, such as requiring that a
/// specific builder method was called.
///
/// All library-provided fields require that at least the [`Field::name`] is defined.
///
///
/// # Example
///
/// A simple builder with no type state:
/// ```no_run
/// # use tundra::{KeyEvent, field::InputResult};
/// # use ratatui::text::Text;
/// use tundra::field::{Field, Build};
///
/// #[derive(Default)]
/// struct MyField {
/// name: String,
/// // ...
/// }
///
/// impl Field for MyField {
/// type Builder = Builder;
///
/// // ...
/// # type Value = ();
/// # fn name(&self) -> &str { todo!() }
/// # fn input(&mut self, _: KeyEvent) -> InputResult { todo!() }
/// # fn format(&self, _: bool) -> Text { todo!() }
/// # fn value(&self) -> &() { todo!() }
/// # fn into_value(self) -> Self::Value { todo!() }
/// }
///
/// #[derive(Default)]
/// struct Builder(MyField);
///
/// impl Builder {
/// pub fn name(self, name: String) -> Self {
/// Builder(MyField{ name, ..self.0 })
/// }
/// }
///
/// impl Build for Builder {
/// type Field = MyField;
///
/// fn build(self) -> MyField {
/// self.0
/// }
/// }
/// ```
///
/// A builder requiring that a name was supplied:
/// ```no_run
/// # use tundra::{KeyEvent, field::InputResult};
/// # use ratatui::text::Text;
/// use tundra::field::{Field, Build};
///
/// #[derive(Default)]
/// struct MyField {
/// name: String,
/// // ...
/// }
///
/// impl Field for MyField {
/// type Builder = Builder<false>;
///
/// // ...
/// # type Value = ();
/// # fn name(&self) -> &str { todo!() }
/// # fn input(&mut self, _: KeyEvent) -> InputResult { todo!() }
/// # fn format(&self, _: bool) -> Text { todo!() }
/// # fn value(&self) -> &() { todo!() }
/// # fn into_value(self) -> Self::Value { todo!() }
/// }
///
/// // note the type state parameter `NAME`, indicating whether the name has yet been supplied
/// #[derive(Default)]
/// struct Builder<const NAME: bool>(MyField);
///
/// impl Builder<false> {
/// // only callable if name has not yet been given
/// pub fn name(self, name: String) -> Builder<true> {
/// Builder(MyField{ name, ..self.0 })
/// }
/// }
///
/// impl Build for Builder<true> {
/// type Field = MyField;
///
/// // only callable if name has been given
/// fn build(self) -> MyField {
/// self.0
/// }
/// }
/// Interface for user input fields.
///
/// For most applications, the [library provided fields](self) should suffice, but custom fields may be
/// created by implementing this trait.
///
/// Fields are mainly designed to be used in [forms](crate::dialog::form!), but can be used on their own by
/// feeding key-presses with [`Field::input`] and drawing them using the [`Text`] returned from
/// [`Field::format`].
/// Indicates the result of a call to [`Field::input`].
///
///
/// # Custom fields
///
/// Note that care should be taken when and when not to return [`Consumed`](InputResult::Consumed), since it
/// blocks [forms](crate::dialog::form!) from responding to [`KeyCode::Up`](crate::prelude::KeyCode::Up) and
/// [`KeyCode::Down`](crate::prelude::KeyCode::Down) inputs.