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
//! A one-line question a panel can ask.
//!
//! Most of what the dashboard lets you change is a toggle — units, sort order,
//! whether seconds show — and a toggle needs no interface beyond the key that
//! flips it. Three settings are not toggles: the agenda's `.ics` file, the
//! weather location, and the name of a timezone to add to the clock. Each of
//! those is free text, and each was stuck behind "edit your config file"
//! for want of somewhere to type.
//!
//! This is that somewhere, once, rather than three times. A panel opens it with
//! a label and the value as it stands, reads [`Outcome`] back out of
//! `handle_key`, and decides for itself whether the answer is any good — the
//! prompt has no idea what a timezone is and does not want one. A rejected
//! answer comes back with [`Prompt::reject`] and the dialog stays open with the
//! text still in it, because retyping a long path to fix one character is the
//! kind of thing that stops people using a feature at all.
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, BorderType, Borders, Clear, Padding, Paragraph};
use ratatui::crossterm::event::{KeyCode, KeyEvent};
use crate::textfield::TextField;
use crate::theme::Theme;
/// What `Tab` offers to finish.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Completion {
/// Nothing; `Tab` does nothing.
None,
/// Filesystem paths, for the agenda file.
Paths,
/// A list to choose from, shown under the field and narrowed as you type.
///
/// For a value the reader is not expected to know by heart. A timezone is
/// the case that prompted it: the identifier names a city, and it is very
/// often not the city the reader means.
Places(&'static [crate::zones::Place]),
}
/// What a keypress did.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome {
/// Still typing.
Editing,
/// Backed out; the panel should close the prompt and change nothing.
Cancelled,
/// Enter, with nothing selected from a list: whatever was typed. The panel
/// decides whether to accept it.
Submitted(String),
/// Enter, on a row of the list. Carries the label the reader recognised as
/// well as the value, because the two differ and the label is the better
/// name for the thing — someone who picked Seattle wants a clock that says
/// Seattle, not one that says Los Angeles.
Chose {
label: &'static str,
value: &'static str,
},
}
/// Rows of the list shown at once.
///
/// The list is drawn inside a dialog rather than a panel, so this is a fixed
/// window rather than something the layout decides — and it has to be a
/// constant both `handle_key` and `render` can see, because the first has to
/// know how far it may scroll and the second has to draw the same window.
const LIST_ROWS: usize = 10;
/// An open prompt.
#[derive(Debug)]
pub struct Prompt {
label: &'static str,
help: &'static str,
field: TextField,
error: Option<String>,
completion: Completion,
/// Which row of the filtered list is selected, if any.
selected: usize,
/// The first row on screen. Without it the cursor walked off the bottom of
/// the window and kept going, invisibly.
offset: usize,
}
impl Prompt {
/// Ask `label`, starting from `value`.
pub fn new(
label: &'static str,
help: &'static str,
value: &str,
completion: Completion,
) -> Self {
Self {
label,
help,
field: TextField::with_value(value),
error: None,
completion,
selected: 0,
offset: 0,
}
}
/// The list rows matching what has been typed so far.
///
/// Matched against the city *and* the identifier, case-insensitively and
/// anywhere in either — so `seattle` finds `America/Los_Angeles`, `asia`
/// finds every Asian zone, and `kolkata` finds the identifier directly.
/// Prefix-only matching would answer half these and is the reason a plain
/// completion was not enough.
pub fn matches(&self) -> Vec<&'static crate::zones::Place> {
let Completion::Places(places) = self.completion else {
return Vec::new();
};
let needle = self.field.trimmed().to_lowercase();
places
.iter()
.filter(|place| {
needle.is_empty()
|| place.city.to_lowercase().contains(&needle)
|| place.tz.to_lowercase().contains(&needle)
})
.collect()
}
/// Refuse the answer and say why, leaving the text in place to be fixed.
pub fn reject(&mut self, why: impl Into<String>) {
self.error = Some(why.into());
}
/// The text as it stands. Exposed for tests.
#[cfg(test)]
pub fn value(&self) -> &str {
self.field.value()
}
pub fn handle_key(&mut self, key: KeyEvent) -> Outcome {
// A keystroke means the user is dealing with the complaint, so the
// complaint goes.
self.error = None;
let listed = self.matches();
match key.code {
KeyCode::Esc => Outcome::Cancelled,
KeyCode::Down => {
self.selected = (self.selected + 1).min(listed.len().saturating_sub(1));
self.scroll_into_view();
Outcome::Editing
}
KeyCode::Up => {
self.selected = self.selected.saturating_sub(1);
self.scroll_into_view();
Outcome::Editing
}
KeyCode::PageDown => {
self.selected = (self.selected + LIST_ROWS).min(listed.len().saturating_sub(1));
self.scroll_into_view();
Outcome::Editing
}
KeyCode::PageUp => {
self.selected = self.selected.saturating_sub(LIST_ROWS);
self.scroll_into_view();
Outcome::Editing
}
KeyCode::Home if !listed.is_empty() => {
self.selected = 0;
self.scroll_into_view();
Outcome::Editing
}
KeyCode::End if !listed.is_empty() => {
self.selected = listed.len() - 1;
self.scroll_into_view();
Outcome::Editing
}
KeyCode::Enter => match listed.get(self.selected) {
Some(place) => Outcome::Chose {
label: place.city,
value: place.tz,
},
// Nothing matched, so the reader knows something the list does
// not. Hand back what they typed rather than refusing it.
None => Outcome::Submitted(self.field.trimmed().to_string()),
},
KeyCode::Tab => {
self.complete();
Outcome::Editing
}
_ => {
self.field.handle_key(key);
// Typing narrows the list under the cursor, so a selection two
// rows down would otherwise land on something unrelated — or
// past the end of what is left.
self.selected = 0;
self.offset = 0;
Outcome::Editing
}
}
}
/// Keep the selected row inside the window that is drawn.
///
/// Only moves the window when the selection would otherwise leave it, so
/// the list stays put while the cursor travels across it and scrolls by one
/// at the edges. Recomputing the window from the selection each time would
/// be stateless and simpler, and would also jump the whole list whenever
/// you crossed a page boundary going back up.
fn scroll_into_view(&mut self) {
if self.selected < self.offset {
self.offset = self.selected;
} else if self.selected >= self.offset + LIST_ROWS {
self.offset = self.selected + 1 - LIST_ROWS;
}
}
/// Extend the text as far as every candidate agrees.
///
/// Completing only to the common prefix is what a shell does, and it is the
/// behaviour that never surprises: it either finishes the job or stops at
/// the point where a choice genuinely has to be made. Completing to the
/// first match instead would silently pick one of several.
fn complete(&mut self) {
let typed = self.field.value().to_string();
let (fixed, stem) = match self.completion {
// A list is chosen from rather than completed into, so neither of
// these has anything for Tab to do.
Completion::None | Completion::Places(_) => return,
Completion::Paths => match typed.rfind('/') {
Some(cut) => (typed[..=cut].to_string(), typed[cut + 1..].to_string()),
None => (String::new(), typed.clone()),
},
};
let candidates = self.candidates(&fixed, &stem);
let Some(shared) = common_prefix(&candidates) else {
return;
};
if shared.len() <= stem.len() {
return;
}
self.field = TextField::with_value(format!("{fixed}{shared}"));
self.field.end();
}
fn candidates(&self, fixed: &str, stem: &str) -> Vec<String> {
match self.completion {
Completion::None | Completion::Places(_) => Vec::new(),
Completion::Paths => {
let directory = expand_tilde(if fixed.is_empty() { "./" } else { fixed });
let Ok(entries) = std::fs::read_dir(&directory) else {
return Vec::new();
};
entries
.flatten()
.filter_map(|entry| {
let name = entry.file_name().to_string_lossy().into_owned();
if !name.starts_with(stem) {
return None;
}
// A trailing slash on a directory means one Tab gets you
// into it rather than up to its edge.
let directory = entry.file_type().is_ok_and(|kind| kind.is_dir());
Some(if directory { format!("{name}/") } else { name })
})
.collect()
}
}
}
/// Draw the prompt over the middle of `screen`.
///
/// `screen` is the whole terminal rather than the calling panel's slice of
/// it. A dialog confined to its panel is as narrow as the panel, which for
/// the agenda meant a long path scrolled inside about forty columns.
pub fn render(&self, frame: &mut ratatui::Frame, screen: Rect, theme: &Theme) {
let listed = self.matches();
let rows = u16::try_from(listed.len().min(LIST_ROWS)).unwrap_or(0);
let width = screen.width.clamp(20, 64);
let height = 3 + rows + crate::frame::FRAME_HEIGHT;
let popup = crate::frame::centred(screen, width, height);
// Room for the text, inside the border and its padding.
let inner = usize::from(width).saturating_sub(usize::from(crate::frame::FRAME_WIDTH));
let (visible, cursor) = self.field.visible(inner);
let mut lines = vec![Line::from(Span::styled(
visible,
Style::default().fg(theme.text),
))];
// The list, if there is one, between the field and the help line.
let city_width = listed
.iter()
.map(|p| p.city.len())
.max()
.unwrap_or(0)
.min(18);
// `enumerate` before `skip`, so `index` is the row's place in the whole
// list and can be compared against `selected`. Enumerating after
// skipping restarts the count at zero and puts the highlight on the top
// visible row whatever is actually selected.
for (index, place) in listed
.iter()
.enumerate()
.skip(self.offset)
.take(usize::from(rows))
{
let here = index == self.selected;
lines.push(Line::from(vec![
Span::styled(
if here { "▸ " } else { " " },
Style::default().fg(theme.accent),
),
Span::styled(
format!("{:<city_width$} ", place.city),
if here {
Style::default().fg(theme.text).add_modifier(Modifier::BOLD)
} else {
Style::default().fg(theme.text)
},
),
// The identifier is shown as well as the city, because it is
// what ends up in your config and in zones.toml — picking
// Seattle and finding `America/Los_Angeles` written down later
// should not be a surprise.
Span::styled(place.tz, Style::default().fg(theme.muted)),
]));
}
lines.push(Line::from(""));
lines.push(match &self.error {
Some(error) => Line::from(Span::styled(
crate::grid::truncate(error, inner),
Style::default().fg(theme.error),
)),
None if listed.len() > LIST_ROWS => Line::from(Span::styled(
crate::grid::truncate(
&format!("{} of {} · {}", rows, listed.len(), self.help),
inner,
),
Style::default().fg(theme.muted),
)),
None => Line::from(Span::styled(
crate::grid::truncate(self.help, inner),
Style::default().fg(theme.muted),
)),
});
frame.render_widget(Clear, popup);
frame.render_widget(
Paragraph::new(lines).block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(theme.border_focused))
.padding(Padding::horizontal(1))
.title_top(Line::from(Span::styled(
self.label,
Style::default()
.fg(theme.title)
.add_modifier(Modifier::BOLD),
))),
),
popup,
);
// A visible cursor, because a text field without one reads as a label.
let x = popup.x + 2 + u16::try_from(cursor).unwrap_or(0);
frame.set_cursor_position((x.min(popup.x + popup.width - 2), popup.y + 1));
}
}
/// Replace a leading `~` with the user's home directory.
pub fn expand_tilde(path: &str) -> std::path::PathBuf {
let Some(rest) = path.strip_prefix('~') else {
return std::path::PathBuf::from(path);
};
let Some(home) = dirs::home_dir() else {
return std::path::PathBuf::from(path);
};
home.join(rest.trim_start_matches('/'))
}
/// The longest start every candidate shares, or `None` if there are none.
fn common_prefix(candidates: &[String]) -> Option<String> {
let first = candidates.first()?;
let mut shared = first.len();
for other in &candidates[1..] {
shared = shared.min(
first
.char_indices()
.zip(other.char_indices())
.take_while(|((_, a), (_, b))| a == b)
.map(|((index, a), _)| index + a.len_utf8())
.last()
.unwrap_or(0),
);
}
Some(first[..shared].to_string())
}
#[cfg(test)]
mod tests {
use super::*;
const PLACES: &[crate::zones::Place] = &[
crate::zones::Place {
city: "London",
tz: "Europe/London",
},
crate::zones::Place {
city: "Lisbon",
tz: "Europe/Lisbon",
},
crate::zones::Place {
city: "Seattle",
tz: "America/Los_Angeles",
},
crate::zones::Place {
city: "Tokyo",
tz: "Asia/Tokyo",
},
];
fn prompt(value: &str) -> Prompt {
Prompt::new("ZONE", "help", value, Completion::Places(PLACES))
}
fn press(prompt: &mut Prompt, code: KeyCode) -> Outcome {
prompt.handle_key(KeyEvent::from(code))
}
#[test]
fn esc_abandons_it() {
assert_eq!(press(&mut prompt("x"), KeyCode::Esc), Outcome::Cancelled);
}
/// The headline case, and the reason a plain completion was not enough:
/// Seattle keeps time in `America/Los_Angeles`, and nobody should have to
/// know that before they can add a clock. Prefix matching cannot answer it.
#[test]
fn a_city_finds_the_zone_it_is_actually_in() {
let mut p = prompt("");
for c in "seattle".chars() {
p.handle_key(KeyEvent::from(KeyCode::Char(c)));
}
let found: Vec<&str> = p.matches().iter().map(|place| place.tz).collect();
assert_eq!(found, ["America/Los_Angeles"]);
}
/// And the identifier still works, for someone who knows it.
#[test]
fn the_identifier_matches_as_well_as_the_city() {
let mut p = prompt("");
for c in "europe/".chars() {
p.handle_key(KeyEvent::from(KeyCode::Char(c)));
}
let found: Vec<&str> = p.matches().iter().map(|place| place.city).collect();
assert_eq!(found, ["London", "Lisbon"]);
}
/// Choosing carries the label as well as the value, because they differ:
/// someone who picked Seattle wants a clock that says Seattle.
#[test]
fn choosing_a_row_returns_the_city_as_the_label() {
let mut p = prompt("seattle");
assert_eq!(
press(&mut p, KeyCode::Enter),
Outcome::Chose {
label: "Seattle",
value: "America/Los_Angeles"
}
);
}
/// A reader who types a zone the list has never heard of knows something
/// the list does not. Refusing them would make a convenience into a cage.
#[test]
fn text_matching_nothing_is_handed_back_rather_than_refused() {
let mut p = prompt(" Antarctica/Troll ");
assert!(p.matches().is_empty(), "nothing in the list matches");
assert_eq!(
press(&mut p, KeyCode::Enter),
Outcome::Submitted("Antarctica/Troll".into())
);
}
/// The list draws a fixed window of rows. Moving the selection past the
/// bottom of it used to keep moving an invisible cursor: the highlight
/// vanished and the row that Enter would take was anybody's guess.
#[test]
fn the_window_follows_the_selection_off_the_bottom_and_back() {
// The real table, not the four-entry fixture: with fewer places than
// LIST_ROWS nothing ever scrolls and this test cannot fail. It did not,
// the first time it was written.
let real = crate::zones::PLACES;
assert!(real.len() > LIST_ROWS, "there is something to scroll");
let mut p = Prompt::new("ZONE", "help", "", Completion::Places(real));
for _ in 0..real.len() {
press(&mut p, KeyCode::Down);
}
assert!(
p.selected >= p.offset && p.selected < p.offset + LIST_ROWS,
"selected {} left the window at {}",
p.selected,
p.offset
);
assert!(p.offset > 0, "the window actually moved");
for _ in 0..real.len() {
press(&mut p, KeyCode::Up);
}
assert_eq!(p.selected, 0, "back to the first");
assert_eq!(p.offset, 0, "and the window came back with it");
}
/// The window only moves when it has to, so the cursor travels across a
/// stationary list rather than dragging it along one row at a time.
#[test]
fn the_window_stays_put_while_the_selection_is_inside_it() {
let mut p = prompt("");
press(&mut p, KeyCode::Down);
assert_eq!(p.offset, 0, "second row is already visible");
}
#[test]
fn the_selection_clamps_and_returns_to_the_top_when_the_list_narrows() {
let mut p = prompt("");
for _ in 0..20 {
press(&mut p, KeyCode::Down);
}
assert_eq!(p.selected, PLACES.len() - 1, "clamped at the end");
// Typing narrows the list under the cursor, so a stale index would
// leave the highlight on something unrelated — or past the end.
p.handle_key(KeyEvent::from(KeyCode::Char('l')));
assert_eq!(p.selected, 0);
}
/// A list is chosen from, not completed into — `Tab` would be a second
/// way to do what the arrows already do, and a worse one.
#[test]
fn tab_does_nothing_when_the_prompt_offers_a_list() {
let mut p = prompt("Lis");
press(&mut p, KeyCode::Tab);
assert_eq!(p.value(), "Lis");
}
/// The whole reason a rejection keeps the text: a path is long, and being
/// sent back to an empty box to fix one character is how a feature stops
/// getting used.
#[test]
fn a_rejected_answer_keeps_what_was_typed_and_the_next_key_clears_the_complaint() {
let mut p = prompt("Europe/Lundon");
p.reject("no such timezone");
assert!(p.error.is_some());
press(&mut p, KeyCode::Backspace);
assert!(p.error.is_none(), "typing dismisses the complaint");
assert_eq!(p.value(), "Europe/Lundo", "and the text is still there");
}
#[test]
fn completion_can_be_turned_off_entirely() {
let mut p = Prompt::new("CITY", "help", "Bost", Completion::None);
press(&mut p, KeyCode::Tab);
assert_eq!(
p.value(),
"Bost",
"a city name has nothing to complete from"
);
}
}