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
//! The rows and the columns, the size, and the selected row.
use super::{Column, Model, Row};
use crate::style::height;
impl Model {
/// The selected row.
///
/// ```
/// use petitcarnets::bubbles::table::{self, Column};
/// # fn cities() -> table::Model {
/// # let mut t = table::Model::new()
/// # .with_columns(vec![Column::new("City", 5), Column::new("Country", 7)])
/// # .with_height(4)
/// # .with_width(20);
/// # t.from_values("Tokyo,Japan\nDelhi,India\nCairo,Egypt", ",");
/// # t
/// # }
///
/// let mut t = cities();
/// assert_eq!(
/// t.selected_row(),
/// Some(&vec!["Tokyo".to_string(), "Japan".to_string()])
/// );
/// t.set_rows(Vec::new());
/// assert_eq!(t.selected_row(), None);
/// ```
#[must_use]
pub fn selected_row(&self) -> Option<&Row> {
self.rows.get(self.cursor)
}
/// The rows.
///
/// ```
/// use petitcarnets::bubbles::table::{self, Column};
/// # fn cities() -> table::Model {
/// # let mut t = table::Model::new()
/// # .with_columns(vec![Column::new("City", 5), Column::new("Country", 7)])
/// # .with_height(4)
/// # .with_width(20);
/// # t.from_values("Tokyo,Japan\nDelhi,India\nCairo,Egypt", ",");
/// # t
/// # }
///
/// let t = cities();
/// assert_eq!(t.rows().len(), 3);
/// assert_eq!(t.rows()[2][0], "Cairo");
/// ```
#[must_use]
pub fn rows(&self) -> &[Row] {
&self.rows
}
/// The columns.
///
/// ```
/// use petitcarnets::bubbles::table::{self, Column};
/// # fn cities() -> table::Model {
/// # let mut t = table::Model::new()
/// # .with_columns(vec![Column::new("City", 5), Column::new("Country", 7)])
/// # .with_height(4)
/// # .with_width(20);
/// # t.from_values("Tokyo,Japan\nDelhi,India\nCairo,Egypt", ",");
/// # t
/// # }
///
/// let t = cities();
/// let titles: Vec<&str> = t.columns().iter().map(|c| c.title.as_str()).collect();
/// assert_eq!(titles, ["City", "Country"]);
/// ```
#[must_use]
pub fn columns(&self) -> &[Column] {
&self.cols
}
/// Replaces the rows, keeping the cursor on one.
///
/// ```
/// use petitcarnets::bubbles::table::{self, Column};
/// # fn cities() -> table::Model {
/// # let mut t = table::Model::new()
/// # .with_columns(vec![Column::new("City", 5), Column::new("Country", 7)])
/// # .with_height(4)
/// # .with_width(20);
/// # t.from_values("Tokyo,Japan\nDelhi,India\nCairo,Egypt", ",");
/// # t
/// # }
///
/// let mut t = cities();
/// t.set_cursor(2);
/// t.set_rows(vec![vec!["Lagos".into(), "Nigeria".into()]]);
/// assert_eq!(t.cursor(), 0, "kept on a row");
/// assert_eq!(t.selected_row().map(|r| r[0].as_str()), Some("Lagos"));
/// ```
pub fn set_rows(&mut self, rows: Vec<Row>) {
self.rows = rows;
self.cursor = self.cursor.min(self.rows.len().saturating_sub(1));
self.update_viewport();
}
/// Replaces the columns.
///
/// ```
/// use petitcarnets as tea;
/// use tea::bubbles::table::{self, Column};
/// use tea::style::strip_ansi;
/// # fn cities() -> table::Model {
/// # let mut t = table::Model::new()
/// # .with_columns(vec![Column::new("City", 5), Column::new("Country", 7)])
/// # .with_height(4)
/// # .with_width(20);
/// # t.from_values("Tokyo,Japan\nDelhi,India\nCairo,Egypt", ",");
/// # t
/// # }
///
/// let mut t = cities();
/// t.set_columns(vec![Column::new("City", 5)]);
/// assert_eq!(strip_ansi(&t.view()).lines().next(), Some(" City "));
/// ```
pub fn set_columns(&mut self, cols: Vec<Column>) {
self.cols = cols;
self.update_viewport();
}
/// Sets the width.
///
/// ```
/// use petitcarnets::bubbles::table::{self, Column};
/// # fn cities() -> table::Model {
/// # let mut t = table::Model::new()
/// # .with_columns(vec![Column::new("City", 5), Column::new("Country", 7)])
/// # .with_height(4)
/// # .with_width(20);
/// # t.from_values("Tokyo,Japan\nDelhi,India\nCairo,Egypt", ",");
/// # t
/// # }
///
/// let mut t = cities();
/// t.set_width(12);
/// assert_eq!(t.width(), 12);
/// ```
pub fn set_width(&mut self, columns: usize) {
self.viewport.set_width(columns);
self.update_viewport();
}
/// Sets the height, headers included.
///
/// ```
/// use petitcarnets::bubbles::table::{self, Column};
/// # fn cities() -> table::Model {
/// # let mut t = table::Model::new()
/// # .with_columns(vec![Column::new("City", 5), Column::new("Country", 7)])
/// # .with_height(4)
/// # .with_width(20);
/// # t.from_values("Tokyo,Japan\nDelhi,India\nCairo,Egypt", ",");
/// # t
/// # }
///
/// let mut t = cities();
/// t.set_height(3);
/// assert_eq!(t.height(), 2, "a line for the headers");
/// ```
pub fn set_height(&mut self, rows: usize) {
self.viewport
.set_height(rows.saturating_sub(height(&self.headers_view())));
self.update_viewport();
}
/// The rows' height.
///
/// ```
/// use petitcarnets::bubbles::table::{self, Column};
/// # fn cities() -> table::Model {
/// # let mut t = table::Model::new()
/// # .with_columns(vec![Column::new("City", 5), Column::new("Country", 7)])
/// # .with_height(4)
/// # .with_width(20);
/// # t.from_values("Tokyo,Japan\nDelhi,India\nCairo,Egypt", ",");
/// # t
/// # }
///
/// let t = cities();
/// assert_eq!(t.height(), 3);
/// ```
#[must_use]
pub const fn height(&self) -> usize {
self.viewport.height()
}
/// The width.
///
/// ```
/// use petitcarnets::bubbles::table::{self, Column};
/// # fn cities() -> table::Model {
/// # let mut t = table::Model::new()
/// # .with_columns(vec![Column::new("City", 5), Column::new("Country", 7)])
/// # .with_height(4)
/// # .with_width(20);
/// # t.from_values("Tokyo,Japan\nDelhi,India\nCairo,Egypt", ",");
/// # t
/// # }
///
/// let t = cities();
/// assert_eq!(t.width(), 20);
/// ```
#[must_use]
pub const fn width(&self) -> usize {
self.viewport.width()
}
/// The selected row's index.
///
/// ```
/// use petitcarnets as tea;
/// use tea::bubbles::table::{self, Column};
/// # fn cities() -> table::Model {
/// # let mut t = table::Model::new()
/// # .with_columns(vec![Column::new("City", 5), Column::new("Country", 7)])
/// # .with_height(4)
/// # .with_width(20);
/// # t.from_values("Tokyo,Japan\nDelhi,India\nCairo,Egypt", ",");
/// # t
/// # }
///
/// let down = tea::Msg::KeyPress(tea::Key::new(tea::KeyCode::Down));
/// let mut t = cities().with_focused(true);
/// assert_eq!(t.cursor(), 0);
/// let _ = t.update::<()>(&down);
/// assert_eq!(t.cursor(), 1);
/// ```
#[must_use]
pub const fn cursor(&self) -> usize {
self.cursor
}
/// Rows from `value`: a line each, cells split at `separator`.
///
/// ```
/// use petitcarnets::bubbles::table;
///
/// let mut t = table::Model::new();
/// t.from_values("1;Tokyo\n2;Delhi", ";");
/// assert_eq!(t.rows()[1], ["2", "Delhi"]);
/// ```
pub fn from_values(&mut self, value: &str, separator: &str) {
let rows = value
.split('\n')
.map(|line| line.split(separator).map(String::from).collect())
.collect();
self.set_rows(rows);
}
}