sara-tasks 0.7.0

Sara — folder-aware task manager
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
use anyhow::Result;
use chrono::{Local, Utc};
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
use ratatui::{Terminal, backend::Backend};
use rusqlite::Connection;
use tui_textarea::TextArea;

use crate::infrastructure::config::Config;
use crate::infrastructure::db;
use crate::infrastructure::model::{Priority, Task};
use crate::infrastructure::tui;

use super::handler::{
    checklist_focus_index, comment_target, compute_overlaps, depends_on_display,
    feedback_for_focus, focusables, open_in_editor, open_url, reconcile_dependencies,
    reorder_focused_step,
};
use super::render::render;
use super::types::{Detail, EditField, EditState, Focusable};

pub(super) fn edit_loop<B: Backend>(
    terminal: &mut Terminal<B>,
    conn: &Connection,
    cfg: &Config,
    detail: Detail,
) -> Result<()> {
    let mut st = EditState {
        detail,
        selected: 0,
        editing: false,
        commenting: false,
        adding_step: false,
        editor: TextArea::default(),
        due_error: false,
        dep_error: None,
        scroll: 0,
    };

    loop {
        terminal.draw(|f| render(f, &st))?;

        if !event::poll(std::time::Duration::from_millis(100))? {
            continue;
        }
        let Event::Key(key) = event::read()? else {
            continue;
        };
        if key.kind == KeyEventKind::Release {
            continue;
        }

        let items = focusables(&st.detail);
        // Keep the cursor in range (links/files can disappear after a reload).
        if !items.is_empty() && st.selected >= items.len() {
            st.selected = items.len() - 1;
        }
        let current = items.get(st.selected).cloned();
        let current_field = match &current {
            Some(Focusable::Field(f)) => Some(*f),
            _ => None,
        };

        if st.adding_step {
            match key.code {
                KeyCode::Enter => {
                    let text = st.editor.lines().join(" ");
                    if !text.trim().is_empty() {
                        // Add to the kind of the focused checklist row (so adding
                        // while on an acceptance criterion adds another criterion);
                        // default to a step otherwise.
                        let kind = match &current {
                            Some(Focusable::Checklist(i)) => st
                                .detail
                                .checklist
                                .get(*i)
                                .map(|c| c.kind.clone())
                                .unwrap_or_else(|| db::STEP_KIND_STEP.to_string()),
                            _ => db::STEP_KIND_STEP.to_string(),
                        };
                        let new_id = db::add_step(
                            conn,
                            &st.detail.task.uuid,
                            text.trim(),
                            None,
                            &kind,
                            "human",
                            None,
                        )
                        .ok();
                        st.detail.checklist =
                            db::get_checklist(conn, &st.detail.task.uuid).unwrap_or_default();
                        if let Some(id) = new_id
                            && let Some(p) = checklist_focus_index(&st.detail, id)
                        {
                            st.selected = p;
                        }
                    }
                    st.adding_step = false;
                }
                KeyCode::Esc => st.adding_step = false,
                _ => {
                    st.editor.input(key);
                }
            }
        } else if st.commenting {
            match key.code {
                KeyCode::Enter => {
                    let text = st.editor.lines().join(" ");
                    if !text.trim().is_empty() {
                        let (tk, tid) = comment_target(&st.detail, &current);
                        let _ = db::add_annotation_full(
                            conn,
                            &st.detail.task.uuid,
                            text.trim(),
                            db::NOTE_KIND_COMMENT,
                            "human",
                            tk.as_deref(),
                            tid.as_deref(),
                            false,
                        );
                        st.detail.annotations =
                            db::get_annotations(conn, &st.detail.task.uuid).unwrap_or_default();
                    }
                    st.commenting = false;
                }
                KeyCode::Esc => st.commenting = false,
                _ => {
                    st.editor.input(key);
                }
            }
        } else if st.editing {
            let field = current_field.unwrap_or(EditField::Description);
            match key.code {
                KeyCode::Enter => {
                    let value = st.editor.lines().join("");
                    if field == EditField::DependsOn {
                        match reconcile_dependencies(conn, cfg, &mut st.detail, &value) {
                            Ok(()) => {
                                st.editing = false;
                                st.dep_error = None;
                            }
                            Err(e) => st.dep_error = Some(e),
                        }
                        continue;
                    }
                    if field == EditField::Due
                        && !value.trim().is_empty()
                        && !crate::infrastructure::dates::is_valid_due(&value)
                    {
                        st.due_error = true;
                        continue;
                    }
                    apply_field(&mut st.detail.task, field, &value, cfg);
                    save(conn, cfg, &mut st.detail)?;
                    st.editing = false;
                    st.due_error = false;
                }
                KeyCode::Esc => {
                    st.editing = false;
                    st.due_error = false;
                    st.dep_error = None;
                }
                _ => {
                    st.editor.input(key);
                    if field == EditField::Due {
                        let v = st.editor.lines().join("");
                        st.due_error =
                            !v.trim().is_empty() && !crate::infrastructure::dates::is_valid_due(&v);
                    }
                }
            }
        } else {
            match key.code {
                KeyCode::Char('q') | KeyCode::Esc => break,
                // Reorder the focused checklist step within its kind.
                // Uppercase J/K are inherently shifted; Shift+Arrow is the alias.
                KeyCode::Char('K') => reorder_focused_step(conn, &mut st, &current, true),
                KeyCode::Char('J') => reorder_focused_step(conn, &mut st, &current, false),
                KeyCode::Up if key.modifiers.contains(KeyModifiers::SHIFT) => {
                    reorder_focused_step(conn, &mut st, &current, true);
                }
                KeyCode::Down if key.modifiers.contains(KeyModifiers::SHIFT) => {
                    reorder_focused_step(conn, &mut st, &current, false);
                }
                KeyCode::Down | KeyCode::Char('j') => {
                    if !items.is_empty() {
                        st.selected = (st.selected + 1).min(items.len() - 1);
                    }
                }
                KeyCode::Up | KeyCode::Char('k') => {
                    st.selected = st.selected.saturating_sub(1);
                }
                KeyCode::PageDown => st.scroll = st.scroll.saturating_add(5),
                KeyCode::PageUp => st.scroll = st.scroll.saturating_sub(5),
                KeyCode::Left if current_field == Some(EditField::Priority) => {
                    cycle_priority(&mut st.detail.task, false);
                    save(conn, cfg, &mut st.detail)?;
                }
                KeyCode::Right if current_field == Some(EditField::Priority) => {
                    cycle_priority(&mut st.detail.task, true);
                    save(conn, cfg, &mut st.detail)?;
                }
                KeyCode::Enter | KeyCode::Char('e') => match current {
                    Some(Focusable::Field(EditField::Priority)) => {
                        cycle_priority(&mut st.detail.task, true);
                        save(conn, cfg, &mut st.detail)?;
                    }
                    Some(Focusable::Field(field)) => {
                        st.editor = if field == EditField::DependsOn {
                            let mut ta = TextArea::default();
                            ta.insert_str(depends_on_display(&st.detail));
                            ta
                        } else {
                            editor_for(&st.detail.task, field)
                        };
                        st.editing = true;
                        st.due_error = false;
                        st.dep_error = None;
                    }
                    Some(Focusable::Link(i)) => {
                        if let Some(link) = st.detail.links.get(i) {
                            open_url(&link.url);
                        }
                    }
                    Some(Focusable::File(path)) => {
                        if db::is_url(&path) {
                            // URL stored as a file (legacy attach) -> browser.
                            open_url(&path);
                        } else {
                            // Real file -> open in the user's editor. Hand the
                            // terminal back while the editor runs.
                            let target = st
                                .detail
                                .project_root
                                .as_ref()
                                .map(|r| r.join(&path))
                                .unwrap_or_else(|| std::path::PathBuf::from(&path));
                            tui::suspend()?;
                            let _ = open_in_editor(&target);
                            tui::resume()?;
                            terminal.clear()?;
                        }
                    }
                    Some(Focusable::Checklist(i)) => {
                        if let Some(item) = st.detail.checklist.get(i) {
                            let _ = db::toggle_checklist_item(conn, item.id);
                            st.detail.checklist =
                                db::get_checklist(conn, &st.detail.task.uuid).unwrap_or_default();
                        }
                    }
                    // Enter on an anchor opens the file in the editor.
                    Some(Focusable::Anchor(i)) => {
                        if let Some(anchor) = st.detail.anchors.get(i) {
                            let target = st
                                .detail
                                .project_root
                                .as_ref()
                                .map(|r| r.join(&anchor.path))
                                .unwrap_or_else(|| std::path::PathBuf::from(&anchor.path));
                            tui::suspend()?;
                            let _ = open_in_editor(&target);
                            tui::resume()?;
                            terminal.clear()?;
                        }
                    }
                    // Enter on a comment opens the comment input to reply.
                    Some(Focusable::Comment(_)) => {
                        st.editor = TextArea::default();
                        st.commenting = true;
                    }
                    // Enter on a typed note opens the comment input to comment on it.
                    Some(Focusable::Note(_)) => {
                        st.editor = TextArea::default();
                        st.commenting = true;
                    }
                    None => {}
                },
                KeyCode::Char(' ') => {
                    if let Some(Focusable::Checklist(i)) = &current
                        && let Some(item) = st.detail.checklist.get(*i)
                    {
                        let _ = db::toggle_checklist_item(conn, item.id);
                        st.detail.checklist =
                            db::get_checklist(conn, &st.detail.task.uuid).unwrap_or_default();
                    }
                }
                // ── Add a new checklist step ────────────────────────────────
                KeyCode::Char('a') => {
                    st.editor = TextArea::default();
                    st.adding_step = true;
                }
                // ── Review & comment loop ────────────────────────────────────
                KeyCode::Char('c') => {
                    st.editor = TextArea::default();
                    st.commenting = true;
                }
                KeyCode::Char('r') => {
                    // Mark the focused element for reconsideration.
                    // If it already has an open comment, toggle the flag on it.
                    // If not, create a new comment with request_revision=true so
                    // the element is flagged even without a written note.
                    let fb = feedback_for_focus(&st.detail, &current);
                    if let Some(existing) = fb.first() {
                        let _ =
                            db::set_request_revision(conn, existing.id, !existing.request_revision);
                    } else {
                        // No existing comment — auto-create a reconsider marker.
                        let (tk, tid) = comment_target(&st.detail, &current);
                        let _ = db::add_annotation_full(
                            conn,
                            &st.detail.task.uuid,
                            "⟳ reconsider this",
                            db::NOTE_KIND_COMMENT,
                            "human",
                            tk.as_deref(),
                            tid.as_deref(),
                            true, // request_revision = true
                        );
                    }
                    st.detail.annotations =
                        db::get_annotations(conn, &st.detail.task.uuid).unwrap_or_default();
                }
                KeyCode::Char('x') => {
                    // Resolve the focused element's latest open feedback.
                    if let Some(fb) = feedback_for_focus(&st.detail, &current).first() {
                        let _ = db::resolve_annotation(conn, fb.id, None);
                        st.detail.annotations =
                            db::get_annotations(conn, &st.detail.task.uuid).unwrap_or_default();
                    }
                }
                _ => {}
            }
        }
    }

    Ok(())
}

pub(super) fn editor_for(task: &Task, field: EditField) -> TextArea<'static> {
    let value = current_value(task, field);
    let mut ta = TextArea::default();
    ta.insert_str(&value);
    ta
}

pub(super) fn current_value(task: &Task, field: EditField) -> String {
    match field {
        EditField::Description => task.description.clone(),
        EditField::Project => task.project.clone(),
        EditField::Priority => task
            .priority
            .as_ref()
            .map(|p| p.label().to_string())
            .unwrap_or_default(),
        EditField::Due => task
            .due
            .map(|d| d.with_timezone(&Local).format("%Y-%m-%d").to_string())
            .unwrap_or_default(),
        EditField::Tags => task.tags.join(", "),
        EditField::Estimate => task
            .estimate_mins
            .map(|m| {
                if m >= 60 {
                    let h = m / 60;
                    let rem = m % 60;
                    if rem == 0 {
                        format!("{h}h")
                    } else {
                        format!("{h}h{rem}m")
                    }
                } else {
                    format!("{m}m")
                }
            })
            .unwrap_or_default(),
        EditField::Recur => task.recur.clone().unwrap_or_default(),
        // Dependencies live in a separate table; handled via depends_on_display.
        EditField::DependsOn => String::new(),
    }
}

pub(super) fn apply_field(task: &mut Task, field: EditField, value: &str, cfg: &Config) {
    match field {
        EditField::Description => {
            if !value.trim().is_empty() {
                task.description = value.trim().to_string();
            }
        }
        EditField::Project => {
            if !value.trim().is_empty() {
                task.project = value.trim().to_string();
            }
        }
        EditField::Due => {
            if value.trim().is_empty() {
                task.due = None;
            } else {
                task.due = crate::commands::add::parse_due(value, cfg);
            }
        }
        EditField::Tags => {
            task.tags = value
                .split(',')
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect();
        }
        EditField::Priority => {}
        EditField::Estimate => {
            task.estimate_mins = parse_duration_to_mins(value);
        }
        EditField::Recur => {
            let v = value.trim().to_lowercase();
            task.recur = if v.is_empty() { None } else { Some(v) };
        }
        // Reconciled against the dependencies table in reconcile_dependencies.
        EditField::DependsOn => {}
    }
}

pub(super) fn cycle_priority(task: &mut Task, forward: bool) {
    task.priority = match (&task.priority, forward) {
        (None, true) => Some(Priority::L),
        (Some(Priority::L), true) => Some(Priority::M),
        (Some(Priority::M), true) => Some(Priority::H),
        (Some(Priority::H), true) => None,
        (None, false) => Some(Priority::H),
        (Some(Priority::H), false) => Some(Priority::M),
        (Some(Priority::M), false) => Some(Priority::L),
        (Some(Priority::L), false) => None,
    };
}

pub(super) fn save(conn: &Connection, cfg: &Config, detail: &mut Detail) -> Result<()> {
    let task = &mut detail.task;
    task.modified = Utc::now();
    task.urgency = db::compute_urgency(task, &cfg.urgency, false, 0);
    db::update_task(conn, task)?;
    db::refresh_urgency(conn, &cfg.urgency, &task.uuid)?;
    // Pull back the authoritative urgency (refresh accounts for blocking).
    if let Some(t) = db::get_task_by_uuid_prefix(conn, &task.uuid.to_string()[..8])? {
        task.urgency = t.urgency;
    }
    detail.history = db::get_history(conn, &detail.task.uuid)?;
    // Reload branch / overlaps in case project changed.
    detail.branch = db::get_task_branch(conn, &detail.task.uuid);
    detail.overlaps = compute_overlaps(conn, &detail.task, &detail.branch);
    // Reload similar tasks and checklist after any save.
    detail.similar = db::similar_tasks(
        conn,
        &detail.task.uuid,
        &detail.task.project,
        &detail.task.tags,
    )
    .unwrap_or_default();
    detail.checklist = db::get_checklist(conn, &detail.task.uuid).unwrap_or_default();
    let blockers = db::get_blockers(conn, &detail.task.uuid).unwrap_or_default();
    let blocking_tasks = db::get_blocking(conn, &detail.task.uuid).unwrap_or_default();
    detail.urgency_breakdown = Some(db::compute_urgency_breakdown(
        &detail.task,
        &cfg.urgency,
        !blockers.is_empty(),
        blocking_tasks.len(),
    ));
    // A project change can move the task into a different feature chain.
    detail.chain = db::feature_chain(conn, &detail.task.uuid).unwrap_or_default();
    Ok(())
}

/// Parse a human duration string like "2h30m", "90m", "1h", "45" (minutes) into minutes.
pub(super) fn parse_duration_to_mins(s: &str) -> Option<i64> {
    let s = s.trim();
    if s.is_empty() {
        return None;
    }
    let s_lower = s.to_lowercase();
    let rest = s_lower.as_str();
    // Parse hours (handles "2h", "2h30m", "2h 30m")
    if let Some(h_pos) = rest.find('h')
        && let Ok(h) = rest[..h_pos].trim().parse::<i64>()
    {
        let mut total = h * 60;
        let after_h = rest[h_pos + 1..].trim().trim_end_matches('m').trim();
        if !after_h.is_empty()
            && let Ok(m) = after_h.parse::<i64>()
        {
            total += m;
        }
        return Some(total);
    }
    // "Ym" or bare number (minutes)
    let m_part = rest.trim_end_matches('m').trim();
    m_part.parse::<i64>().ok()
}