shdo 0.7.0

A kanban board for the terminal: tasks are Markdown files in a git repository
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
//! Input fields and the form for new or edited tasks.

use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use rust_i18n::t;

use crate::board::{Board, BucketOrigin, bucket_for_new};
use crate::task::{PRIO_DEFAULT, Task};

#[derive(Default, Clone)]
pub struct Input {
    pub text: String,
    /// Cursor position in characters (not bytes).
    pub cursor: usize,
}

impl Input {
    pub fn with(text: &str) -> Input {
        Input {
            text: text.into(),
            cursor: text.chars().count(),
        }
    }

    fn byte_pos(&self, char_idx: usize) -> usize {
        self.text
            .char_indices()
            .nth(char_idx)
            .map_or(self.text.len(), |(i, _)| i)
    }

    pub fn handle(&mut self, key: KeyEvent) {
        let len = self.text.chars().count();
        match key.code {
            KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                *self = Input::default();
            }
            KeyCode::Char(c) => {
                let at = self.byte_pos(self.cursor);
                self.text.insert(at, c);
                self.cursor += 1;
            }
            KeyCode::Backspace if self.cursor > 0 => {
                self.cursor -= 1;
                let at = self.byte_pos(self.cursor);
                self.text.remove(at);
            }
            KeyCode::Delete if self.cursor < len => {
                let at = self.byte_pos(self.cursor);
                self.text.remove(at);
            }
            KeyCode::Left => self.cursor = self.cursor.saturating_sub(1),
            KeyCode::Right => self.cursor = (self.cursor + 1).min(len),
            KeyCode::Home => self.cursor = 0,
            KeyCode::End => self.cursor = len,
            _ => {}
        }
    }
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Field {
    Title,
    Status,
    Prio,
    Due,
    Bucket,
    Tags,
    Note,
}

impl Field {
    pub fn label(self) -> String {
        let key = match self {
            Field::Title => "form.field.title",
            Field::Status => "form.field.status",
            Field::Prio => "form.field.prio",
            Field::Due => "form.field.due",
            Field::Bucket => "form.field.bucket",
            Field::Tags => "form.field.tags",
            Field::Note => "form.field.note",
        };
        t!(key).into_owned()
    }

    pub fn is_text(self) -> bool {
        matches!(self, Field::Title | Field::Due | Field::Tags | Field::Note)
    }
}

/// Localized name of a priority (1 = high … 3 = low).
pub fn prio_name(prio: u8) -> String {
    let key = match prio {
        1 => "form.prio.high",
        3 => "form.prio.low",
        _ => "form.prio.normal",
    };
    t!(key).into_owned()
}

/// Tags of all tasks, most used (open tasks) first, then by name.
fn known_tags(board: &Board) -> Vec<String> {
    let mut tags = board.tag_counts.clone();
    tags.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
    tags.into_iter().map(|(tag, _)| tag).collect()
}

pub struct Form {
    /// `None` = new task, otherwise the ID of the edited task.
    pub editing: Option<String>,
    pub fields: Vec<Field>,
    pub focus: usize,
    pub title: Input,
    pub status: usize,
    pub status_names: Vec<String>,
    pub prio: u8,
    pub due: Input,
    /// 0 = no bucket, otherwise index + 1 into `bucket_names`.
    pub bucket: usize,
    pub bucket_names: Vec<String>,
    pub bucket_keys: Vec<String>,
    /// The bucket was taken over from the filter and not touched yet.
    pub origin: Option<BucketOrigin>,
    /// Space-separated, `+` optional.
    pub tags: Input,
    pub note: Input,
    /// Multi-line notes can only be edited in the editor.
    pub note_editable: bool,
    /// Known tags, most used first – suggested while typing a tag.
    pub tag_names: Vec<String>,
}

impl Form {
    fn base(board: &Board) -> Form {
        let mut fields = vec![Field::Title, Field::Status, Field::Prio, Field::Due];
        if !board.buckets.is_empty() {
            fields.push(Field::Bucket);
        }
        fields.push(Field::Tags);
        fields.push(Field::Note);
        Form {
            editing: None,
            fields,
            focus: 0,
            title: Input::default(),
            status: 0,
            status_names: board.columns.iter().map(|c| c.def.name.clone()).collect(),
            prio: PRIO_DEFAULT,
            due: Input::default(),
            bucket: 0,
            bucket_names: board.buckets.iter().map(|b| b.name.clone()).collect(),
            bucket_keys: board.buckets.iter().map(|b| b.key.clone()).collect(),
            origin: None,
            tags: Input::default(),
            note: Input::default(),
            note_editable: true,
            tag_names: known_tags(board),
        }
    }

    /// `default_bucket` from the config preselects the bucket unless the filter decides.
    pub fn new_task(board: &Board, status: usize, default_bucket: Option<&str>) -> Form {
        let mut f = Form::base(board);
        f.status = status;
        let picked = bucket_for_new(None, &board.filter, &board.buckets, default_bucket)
            .ok()
            .flatten();
        if let Some((b, origin)) = picked {
            f.bucket = f
                .bucket_keys
                .iter()
                .position(|k| *k == b)
                .map_or(0, |i| i + 1);
            f.origin = (f.bucket > 0).then_some(origin);
        }
        f
    }

    pub fn edit_task(board: &Board, task: &Task) -> Form {
        let mut f = Form::base(board);
        f.editing = Some(task.id.clone());
        f.title = Input::with(&task.title);
        f.status = board
            .columns
            .iter()
            .position(|c| c.def.key == task.status)
            .unwrap_or(0);
        f.prio = task.prio_or_default();
        f.due = Input::with(&task.due.map(|d| d.to_string()).unwrap_or_default());
        f.bucket = task
            .bucket
            .as_ref()
            .and_then(|b| f.bucket_keys.iter().position(|k| k == b))
            .map_or(0, |i| i + 1);
        // trailing space: the existing tags count as finished (pills), not as typed word
        if !task.tags.is_empty() {
            f.tags = Input::with(&format!("{} ", task.tags.join(" ")));
        }
        f.note_editable = task.note_is_single_line();
        if f.note_editable {
            f.note = Input::with(task.note());
        }
        f
    }

    pub fn focused(&self) -> Field {
        self.fields[self.focus]
    }

    pub fn bucket_key(&self) -> Option<&str> {
        self.bucket
            .checked_sub(1)
            .map(|i| self.bucket_keys[i].as_str())
    }

    pub fn bucket_label(&self) -> String {
        match self.bucket.checked_sub(1) {
            Some(i) => self.bucket_names[i].clone(),
            None => t!("bucket.none").into_owned(),
        }
    }

    pub fn input_mut(&mut self) -> Option<&mut Input> {
        match self.focused() {
            Field::Title => Some(&mut self.title),
            Field::Due => Some(&mut self.due),
            Field::Tags => Some(&mut self.tags),
            Field::Note if self.note_editable => Some(&mut self.note),
            _ => None,
        }
    }

    /// The rest of a known tag (or, in the title, a bucket after `@`) for the word that
    /// ends at the cursor – shown greyed out and taken over with `→`.
    pub fn suggestion(&self) -> Option<&str> {
        let (input, in_title) = match self.focused() {
            Field::Title => (&self.title, true),
            Field::Tags => (&self.tags, false),
            _ => return None,
        };
        let (before, after) = input.text.split_at(input.byte_pos(input.cursor));
        if after.chars().next().is_some_and(|c| !c.is_whitespace()) {
            return None;
        }
        let word = before.rsplit(char::is_whitespace).next().unwrap_or("");
        let (prefix, candidates) = match (in_title, word.chars().next()) {
            (true, Some('+')) | (false, Some('+')) => (&word[1..], &self.tag_names),
            (true, Some('@')) => (&word[1..], &self.bucket_keys),
            (false, Some(_)) => (word, &self.tag_names),
            _ => return None,
        };
        if prefix.is_empty() {
            return None;
        }
        let words: Vec<&str> = input.text.split_whitespace().collect();
        let prefix = prefix.to_lowercase();
        candidates
            .iter()
            .filter(|c| c.len() > prefix.len() && c.starts_with(&prefix))
            // a tag that is already there is not suggested again
            .find(|c| {
                !words
                    .iter()
                    .any(|w| w.trim_start_matches('+') == c.as_str())
            })
            .map(|c| &c[prefix.len()..])
    }

    fn accept_suggestion(&mut self) -> bool {
        let Some(rest) = self.suggestion().map(str::to_owned) else {
            return false;
        };
        let Some(input) = self.input_mut() else {
            return false;
        };
        let at = input.byte_pos(input.cursor);
        let space = !input.text[at..].starts_with(' ');
        input
            .text
            .insert_str(at, &format!("{rest}{}", if space { " " } else { "" }));
        input.cursor += rest.chars().count() + 1;
        true
    }

    /// Backspace in the tags field with nothing typed yet removes the whole last tag.
    fn remove_last_tag(&mut self) -> bool {
        let input = &mut self.tags;
        let before = &input.text[..input.byte_pos(input.cursor)];
        if before.trim().is_empty() || !before.ends_with(char::is_whitespace) {
            return false;
        }
        let kept = before.trim_end();
        let kept = &kept[..kept.rfind(char::is_whitespace).map_or(0, |i| i + 1)];
        let rest = input.text[before.len()..].to_string();
        input.cursor = kept.chars().count();
        input.text = format!("{kept}{rest}");
        true
    }

    /// Whether `tag` (with or without `+`) is used by any task already.
    pub fn is_known_tag(&self, tag: &str) -> bool {
        let tag = tag.trim_start_matches('+').to_lowercase();
        self.tag_names.contains(&tag)
    }

    fn cycle(&mut self, delta: isize) {
        let wrap = |v: usize, n: usize| ((v as isize + delta).rem_euclid(n as isize)) as usize;
        match self.focused() {
            Field::Status => self.status = wrap(self.status, self.status_names.len()),
            Field::Prio => self.prio = wrap(self.prio as usize - 1, 3) as u8 + 1,
            Field::Bucket => {
                self.bucket = wrap(self.bucket, self.bucket_names.len() + 1);
                self.origin = None;
            }
            _ => {}
        }
    }

    /// Handles everything except Enter/Esc.
    pub fn handle(&mut self, key: KeyEvent) {
        let n = self.fields.len();
        match key.code {
            KeyCode::Tab | KeyCode::Down => self.focus = (self.focus + 1) % n,
            KeyCode::BackTab | KeyCode::Up => self.focus = (self.focus + n - 1) % n,
            KeyCode::Right if self.accept_suggestion() => {}
            KeyCode::Backspace if self.focused() == Field::Tags && self.remove_last_tag() => {}
            _ if self.focused().is_text() => {
                if let Some(input) = self.input_mut() {
                    input.handle(key);
                }
            }
            KeyCode::Left | KeyCode::Char('h') => self.cycle(-1),
            KeyCode::Right | KeyCode::Char('l') | KeyCode::Char(' ') => self.cycle(1),
            KeyCode::Char(c @ '1'..='3') if self.focused() == Field::Prio => {
                self.prio = c as u8 - b'0'
            }
            _ => {}
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::board::Filter;
    use crate::store::parse_board;
    use ratatui::crossterm::event::KeyEvent;

    fn board(filter: &[&str]) -> Board {
        let cfg = parse_board(
            "[[buckets]]\nkey='home'\nname='Privat'\n[[buckets]]\nkey='work'\nname='Arbeit'",
        )
        .unwrap();
        let filter = Filter {
            buckets: filter.iter().map(|s| s.to_string()).collect(),
            ..Filter::default()
        };
        Board::build(cfg, vec![], vec![], filter)
    }

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::from(code)
    }

    #[test]
    fn new_task_inherits_single_bucket() {
        let f = Form::new_task(&board(&["work"]), 1, None);
        assert_eq!(f.bucket_key(), Some("work"));
        assert_eq!(f.origin, Some(BucketOrigin::Filter));
        assert_eq!(f.status, 1);
        let f = Form::new_task(&board(&["work", "home"]), 0, None);
        assert_eq!(f.bucket_key(), None);
        assert_eq!(f.origin, None);
        let f = Form::new_task(&board(&["work", "home"]), 0, Some("home"));
        assert_eq!(f.bucket_key(), Some("home"));
        assert_eq!(f.origin, Some(BucketOrigin::Default));
    }

    fn typed(f: &mut Form, text: &str) {
        for ch in text.chars() {
            f.handle(key(KeyCode::Char(ch)));
        }
    }

    #[test]
    fn suggests_known_tags_and_buckets() {
        let mut f = Form::new_task(&board(&[]), 0, None);
        f.tag_names = vec!["reporting".into(), "dev".into(), "deploy".into()];
        typed(&mut f, "Report +rep");
        assert_eq!(f.suggestion(), Some("orting"));
        f.handle(key(KeyCode::Right));
        assert_eq!(f.title.text, "Report +reporting ");
        assert_eq!(f.suggestion(), None);
        // most used first, already used tags are skipped
        typed(&mut f, "+de");
        assert_eq!(f.suggestion(), Some("v"));
        typed(&mut f, "v +d");
        assert_eq!(f.suggestion(), Some("eploy"));
        // buckets after @ in the title
        typed(&mut f, " @wo");
        assert_eq!(f.suggestion(), Some("rk"));
        // nothing for plain words, a complete tag or the cursor inside a word
        typed(&mut f, "rk");
        assert_eq!(f.suggestion(), None);
        f.handle(key(KeyCode::Left));
        assert_eq!(f.suggestion(), None);
        // without a suggestion → still moves the cursor
        f.handle(key(KeyCode::Right));
        assert_eq!(f.title.cursor, f.title.text.chars().count());
        // tags field: + is optional
        while f.focused() != Field::Tags {
            f.handle(key(KeyCode::Tab));
        }
        typed(&mut f, "dev re");
        assert_eq!(f.suggestion(), Some("porting"));
        f.handle(key(KeyCode::Right));
        assert_eq!(f.tags.text, "dev reporting ");
    }

    #[test]
    fn backspace_removes_whole_tag() {
        let mut f = Form::new_task(&board(&[]), 0, None);
        while f.focused() != Field::Tags {
            f.handle(key(KeyCode::Tab));
        }
        typed(&mut f, "dev +ops re");
        // a word being typed loses one character at a time
        f.handle(key(KeyCode::Backspace));
        assert_eq!(f.tags.text, "dev +ops r");
        f.handle(key(KeyCode::Backspace));
        assert_eq!(f.tags.text, "dev +ops ");
        // then whole tags
        f.handle(key(KeyCode::Backspace));
        assert_eq!(f.tags.text, "dev ");
        f.handle(key(KeyCode::Backspace));
        assert_eq!((f.tags.text.as_str(), f.tags.cursor), ("", 0));
        f.handle(key(KeyCode::Backspace));
        assert_eq!(f.tags.text, "");
    }

    #[test]
    fn navigation_and_choices() {
        let mut f = Form::new_task(&board(&["work"]), 0, None);
        assert_eq!(f.fields.len(), 7);
        for ch in "Hallo h".chars() {
            f.handle(key(KeyCode::Char(ch)));
        }
        assert_eq!(f.title.text, "Hallo h");
        f.handle(key(KeyCode::Tab));
        f.handle(key(KeyCode::Tab));
        assert_eq!(f.focused(), Field::Prio);
        f.handle(key(KeyCode::Char('h')));
        assert_eq!(f.prio, 1);
        f.handle(key(KeyCode::Left));
        assert_eq!(f.prio, 3);
        f.handle(key(KeyCode::Char('2')));
        assert_eq!(f.prio, 2);
        f.handle(key(KeyCode::Down));
        f.handle(key(KeyCode::Down));
        assert_eq!(f.focused(), Field::Bucket);
        f.handle(key(KeyCode::Right));
        assert_eq!(f.bucket_key(), None);
        assert_eq!(f.origin, None);
        f.handle(key(KeyCode::BackTab));
        f.handle(key(KeyCode::Up));
        f.handle(key(KeyCode::Up));
        f.handle(key(KeyCode::Up));
        f.handle(key(KeyCode::Up));
        assert_eq!(f.focused(), Field::Note);
    }

    #[test]
    fn no_bucket_field_without_buckets() {
        let b = Board::build(parse_board("").unwrap(), vec![], vec![], Filter::all());
        let f = Form::new_task(&b, 0, None);
        assert!(!f.fields.contains(&Field::Bucket));
    }
}