use chrono::{DateTime, Local, TimeDelta};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use tui_input::backend::crossterm::EventHandler;
use super::action::{Action, Effect};
use super::model::{HistoryView, Mode, Model, Problem};
const TOAST_TTL: TimeDelta = TimeDelta::seconds(3);
pub fn update(model: &mut Model, action: Action, now: DateTime<Local>) -> Vec<Effect> {
model.now = now;
if let Action::Key(key) = &action
&& key.modifiers.contains(KeyModifiers::CONTROL)
&& key.code == KeyCode::Char('c')
{
return vec![Effect::Quit];
}
match action {
Action::Tick => tick(model, now),
Action::Reload { board, damaged } => {
model.board = board;
model.ui.board_damage = damaged;
model.settle(now);
model.clamp_cursor();
Vec::new()
}
Action::Merge(remote) => {
let merged = remote.merged_with(&model.board);
if merged == model.board {
return Vec::new();
}
model.board = merged;
model.settle(now);
model.clamp_cursor();
vec![Effect::Save]
}
Action::Key(key) => match model.ui.mode {
Mode::Normal => normal(model, key, now),
Mode::Add | Mode::Edit => editing(model, key, now),
Mode::Completed => completed(model, key, now),
Mode::Help => help(model, key),
Mode::Details => details(model, key),
},
}
}
fn tick(model: &mut Model, now: DateTime<Local>) -> Vec<Effect> {
model.ui.tick = model.ui.tick.wrapping_add(1);
if let Some(toast) = &model.ui.toast {
let age = now.signed_duration_since(toast.born);
if age > TOAST_TTL || age < TimeDelta::zero() {
model.ui.toast = None;
}
}
if model.settle_if_new_day(now) {
model.clamp_cursor();
vec![Effect::Save]
} else {
Vec::new()
}
}
fn normal(model: &mut Model, key: KeyEvent, now: DateTime<Local>) -> Vec<Effect> {
let mut effects = Vec::new();
match key.code {
KeyCode::Esc | KeyCode::Char('q') => return vec![Effect::Quit],
KeyCode::Char('j') | KeyCode::Down => model.cursor_down(),
KeyCode::Char('k') | KeyCode::Up => model.cursor_up(),
KeyCode::Char('g') | KeyCode::Home => model.cursor_first(),
KeyCode::Char('G') | KeyCode::End => model.cursor_last(),
KeyCode::Char(' ') | KeyCode::Enter => {
if let Some(index) = model.selected()
&& model.board.complete(index, now)
{
model.clamp_cursor();
effects.push(Effect::Save);
}
}
KeyCode::Char('a') => {
model.ui.input.reset();
model.ui.edit_target = None;
model.ui.mode = Mode::Add;
}
KeyCode::Char('e') => {
if let Some(index) = model.selected()
&& let Some(task) = model.board.task(index)
{
model.ui.input = tui_input::Input::new(task.title.clone());
model.ui.edit_target = Some(task.created_at);
model.ui.mode = Mode::Edit;
}
}
KeyCode::Char('t') => {
if selected_mutates(model, |board, index| board.pin_today(index, now)) {
effects.push(Effect::Save);
}
}
KeyCode::Char('h') | KeyCode::Left => model.cursor_bucket(-1),
KeyCode::Char('l') | KeyCode::Right => model.cursor_bucket(1),
KeyCode::Char('[') => {
if selected_mutates(model, |board, index| board.move_bucket(index, -1, now)) {
effects.push(Effect::Save);
}
}
KeyCode::Char(']') => {
if selected_mutates(model, |board, index| board.move_bucket(index, 1, now)) {
effects.push(Effect::Save);
}
}
KeyCode::Char('x') => {
if selected_mutates(model, |board, index| board.archive(index, now)) {
effects.push(Effect::Save);
}
}
KeyCode::Char('c') => {
model.ui.mode = Mode::Completed;
model.ui.done_cursor = 0;
model.ui.done_filter.reset();
model.ui.history_view = HistoryView::Done;
}
KeyCode::Char('?') => {
model.ui.overlay_scroll = 0;
model.ui.mode = Mode::Help;
}
KeyCode::Char('!') if model.problems().next().is_some() => {
model.ui.overlay_scroll = 0;
model.ui.mode = Mode::Details;
}
_ => {}
}
effects
}
fn selected_mutates(
model: &mut Model,
change: impl FnOnce(&mut crate::domain::Board, usize) -> bool,
) -> bool {
let Some(index) = model.selected() else {
return false;
};
if change(&mut model.board, index) {
model.clamp_cursor();
true
} else {
false
}
}
fn editing(model: &mut Model, key: KeyEvent, now: DateTime<Local>) -> Vec<Effect> {
match key.code {
KeyCode::Enter => {
let title = model.ui.input.value().trim().to_string();
if title.is_empty() {
return Vec::new();
}
let mut changed = true;
match model.ui.mode {
Mode::Add => {
model.board.add(title, now);
model.ui.cursor = 0;
model.set_toast("saved to today");
}
Mode::Edit => {
let target = model.ui.edit_target;
let index = target.and_then(|created| {
model
.board
.tasks()
.iter()
.position(|task| task.created_at == created)
});
match index {
Some(index) => {
model.board.rename(index, title, now);
}
None => {
model.ui.error = Some(Problem::new(
"edit target gone",
"the task was replaced before the rename was applied",
));
changed = false;
}
}
}
_ => {}
}
model.ui.input.reset();
model.ui.edit_target = None;
model.ui.mode = Mode::Normal;
model.clamp_cursor();
if changed {
vec![Effect::Save]
} else {
Vec::new()
}
}
KeyCode::Esc => {
model.ui.input.reset();
model.ui.edit_target = None;
model.ui.mode = Mode::Normal;
Vec::new()
}
_ => {
model.ui.input.handle_event(&Event::Key(key));
Vec::new()
}
}
}
fn completed(model: &mut Model, key: KeyEvent, now: DateTime<Local>) -> Vec<Effect> {
match key.code {
KeyCode::Esc => {
if model.ui.done_filter.value().is_empty() {
model.ui.mode = Mode::Normal;
} else {
model.ui.done_filter.reset();
model.ui.done_cursor = 0;
}
Vec::new()
}
KeyCode::Tab | KeyCode::Left | KeyCode::Right => {
model.ui.history_view = match model.ui.history_view {
HistoryView::Done => HistoryView::Dropped,
HistoryView::Dropped => HistoryView::Done,
};
model.ui.done_cursor = 0;
Vec::new()
}
KeyCode::Down => {
if model.ui.done_cursor + 1 < model.history_items().len() {
model.ui.done_cursor += 1;
}
Vec::new()
}
KeyCode::Up => {
model.ui.done_cursor = model.ui.done_cursor.saturating_sub(1);
Vec::new()
}
KeyCode::Enter => {
let Some(&index) = model.history_items().get(model.ui.done_cursor) else {
return Vec::new();
};
let restored = match model.ui.history_view {
HistoryView::Done => model.board.restore(index, now),
HistoryView::Dropped => model.board.unarchive(index, now),
};
if !restored {
return Vec::new();
}
model.ui.mode = Mode::Normal;
model.ui.done_filter.reset();
model.clamp_cursor();
vec![Effect::Save]
}
_ => {
model.ui.done_filter.handle_event(&Event::Key(key));
model.ui.done_cursor = 0;
Vec::new()
}
}
}
fn help(model: &mut Model, key: KeyEvent) -> Vec<Effect> {
overlay(model, key, |code| {
matches!(code, KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('?'))
})
}
fn details(model: &mut Model, key: KeyEvent) -> Vec<Effect> {
overlay(model, key, |code| {
matches!(code, KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('!'))
})
}
fn overlay(model: &mut Model, key: KeyEvent, close: impl Fn(KeyCode) -> bool) -> Vec<Effect> {
let scroll = model.ui.overlay_scroll;
if close(key.code) {
model.ui.mode = Mode::Normal;
model.ui.overlay_scroll = 0;
} else {
match key.code {
KeyCode::Down | KeyCode::Char('j') => model.ui.overlay_scroll = scroll + 1,
KeyCode::Up | KeyCode::Char('k') => {
model.ui.overlay_scroll = scroll.saturating_sub(1);
}
_ => {}
}
}
Vec::new()
}
#[cfg(test)]
mod tests {
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use super::*;
use crate::app::Problem;
use crate::domain::{Bucket, test_time::at};
fn press(code: KeyCode) -> Action {
Action::Key(KeyEvent::new(code, KeyModifiers::NONE))
}
fn model() -> Model {
Model::new(crate::domain::Board::new(), at(2026, 10, 5))
}
fn typ(model: &mut Model, action: Action) {
update(model, action, at(2026, 10, 5));
}
#[test]
fn add_records_into_today_and_persists() {
let mut model = model();
typ(&mut model, press(KeyCode::Char('a')));
for c in "å¦ GPUI".chars() {
typ(&mut model, press(KeyCode::Char(c)));
}
let effects = update(&mut model, press(KeyCode::Enter), at(2026, 10, 5));
assert_eq!(effects, vec![Effect::Save]);
assert_eq!(model.board.task(0).unwrap().title, "å¦ GPUI");
assert_eq!(model.board.task(0).unwrap().bucket, Bucket::Today);
assert!(model.ui.toast.is_some());
}
#[test]
fn completing_hides_the_task_from_the_list() {
let mut model = model();
model.board.add("done me", at(2026, 10, 5));
let effects = update(&mut model, press(KeyCode::Char(' ')), at(2026, 10, 5));
assert_eq!(effects, vec![Effect::Save]);
assert_eq!(model.selectable_len(), 0);
}
#[test]
fn promote_and_demote_move_between_buckets() {
let mut model = model();
model.board.add("move me", at(2026, 10, 5));
update(&mut model, press(KeyCode::Char(']')), at(2026, 10, 5));
assert_eq!(model.board.task(0).unwrap().bucket, Bucket::Week);
update(&mut model, press(KeyCode::Char('t')), at(2026, 10, 5));
assert_eq!(model.board.task(0).unwrap().bucket, Bucket::Today);
}
#[test]
fn quit_is_requested_by_effect() {
let mut model = model();
let effects = update(&mut model, press(KeyCode::Char('q')), at(2026, 10, 5));
assert_eq!(effects, vec![Effect::Quit]);
}
#[test]
fn tick_settles_and_saves_only_when_something_moved() {
let mut model = model();
model.board.add("stale", at(2026, 10, 5));
assert!(
update(&mut model, Action::Tick, at(2026, 10, 5)).is_empty(),
"same day: no movement"
);
assert_eq!(
update(&mut model, Action::Tick, at(2026, 10, 6)),
vec![Effect::Save]
);
assert_eq!(model.board.task(0).unwrap().bucket, Bucket::Week);
}
#[test]
fn a_toast_expires_after_its_ttl_or_a_backwards_clock_step() {
let mut model = model();
model.set_toast("saved");
update(&mut model, Action::Tick, at(2026, 10, 5));
assert!(model.ui.toast.is_some());
update(&mut model, Action::Tick, at(2026, 10, 4));
assert!(
model.ui.toast.is_none(),
"a backwards clock step must clear the toast"
);
model.set_toast("saved");
update(
&mut model,
Action::Tick,
at(2026, 10, 5) + TimeDelta::seconds(4),
);
assert!(
model.ui.toast.is_none(),
"the toast must expire after its TTL"
);
}
#[test]
fn h_and_l_move_the_cursor_between_buckets() {
let mut model = model();
model.board.add("today task", at(2026, 10, 5));
model.board.add("later task", at(2026, 10, 5));
model.board.move_bucket(1, 2, at(2026, 10, 5));
update(&mut model, press(KeyCode::Char('l')), at(2026, 10, 5));
assert_eq!(model.selected(), Some(1), "l should jump to Later");
update(&mut model, press(KeyCode::Char('h')), at(2026, 10, 5));
assert_eq!(model.selected(), Some(0), "h should jump back to Today");
}
#[test]
fn overlay_scrolls_and_closes_explicitly() {
let mut model = model();
update(&mut model, press(KeyCode::Char('?')), at(2026, 10, 5));
assert_eq!(model.ui.mode, Mode::Help);
update(&mut model, press(KeyCode::Down), at(2026, 10, 5));
update(&mut model, press(KeyCode::Down), at(2026, 10, 5));
assert_eq!(model.ui.overlay_scroll, 2);
update(&mut model, press(KeyCode::Char('x')), at(2026, 10, 5));
assert_eq!(model.ui.mode, Mode::Help);
update(&mut model, press(KeyCode::Esc), at(2026, 10, 5));
assert_eq!(model.ui.mode, Mode::Normal);
assert_eq!(model.ui.overlay_scroll, 0);
}
#[test]
fn bang_opens_the_details_only_when_something_is_wrong() {
let mut model = model();
update(&mut model, press(KeyCode::Char('!')), at(2026, 10, 5));
assert_eq!(model.ui.mode, Mode::Normal);
model.ui.error = Some(Problem::new(
"could not save",
"Permission denied (os error 13)",
));
update(&mut model, press(KeyCode::Char('!')), at(2026, 10, 5));
assert_eq!(model.ui.mode, Mode::Details);
update(&mut model, press(KeyCode::Down), at(2026, 10, 5));
assert_eq!(model.ui.overlay_scroll, 1);
update(&mut model, press(KeyCode::Esc), at(2026, 10, 5));
assert_eq!(model.ui.mode, Mode::Normal);
assert_eq!(model.ui.overlay_scroll, 0);
}
#[test]
fn ctrl_c_quits_from_any_mode() {
let mut model = model();
for mode in [
Mode::Normal,
Mode::Add,
Mode::Completed,
Mode::Help,
Mode::Details,
] {
model.ui.mode = mode;
let action = Action::Key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL));
assert_eq!(
update(&mut model, action, at(2026, 10, 5)),
vec![Effect::Quit],
"ctrl+c should quit from {mode:?}"
);
}
}
#[test]
fn completed_search_filters_then_restores() {
use crate::domain::TaskState;
let mut model = model();
model.board.add("买猫粮", at(2026, 10, 5));
model.board.add("å¦ GPUI", at(2026, 10, 5));
model.board.complete(0, at(2026, 10, 5));
model.board.complete(1, at(2026, 10, 5));
update(&mut model, press(KeyCode::Char('c')), at(2026, 10, 5));
assert_eq!(model.history_items().len(), 2);
for c in "gpui".chars() {
update(&mut model, press(KeyCode::Char(c)), at(2026, 10, 5));
}
assert_eq!(model.history_items().len(), 1);
let effects = update(&mut model, press(KeyCode::Enter), at(2026, 10, 5));
assert_eq!(effects, vec![Effect::Save]);
assert_eq!(model.board.task(1).unwrap().state, TaskState::Open);
}
#[test]
fn dropped_view_lists_and_restores_archived_tasks() {
use crate::domain::TaskState;
let mut model = model();
model.board.add("abandoned", at(2026, 10, 5));
model.board.archive(0, at(2026, 10, 5));
update(&mut model, press(KeyCode::Char('c')), at(2026, 10, 5));
assert!(model.history_items().is_empty(), "done view is empty");
update(&mut model, press(KeyCode::Tab), at(2026, 10, 5));
assert_eq!(model.history_items().len(), 1);
let effects = update(&mut model, press(KeyCode::Enter), at(2026, 10, 5));
assert_eq!(effects, vec![Effect::Save]);
assert_eq!(model.board.task(0).unwrap().state, TaskState::Open);
}
#[test]
fn a_pull_merge_keeps_local_tasks_and_saves() {
let mut model = model();
model.board.add("local task", at(2026, 10, 5));
let mut remote = crate::domain::Board::new();
remote.add("remote task", at(2026, 10, 4));
let effects = update(&mut model, Action::Merge(remote), at(2026, 10, 5));
assert_eq!(effects, vec![Effect::Save]);
let titles: Vec<&str> = model
.board
.tasks()
.iter()
.map(|task| task.title.as_str())
.collect();
assert!(titles.contains(&"local task"), "{titles:?}");
assert!(titles.contains(&"remote task"), "{titles:?}");
}
#[test]
fn a_pull_merge_that_changes_nothing_does_not_save() {
let mut model = model();
model.board.add("only task", at(2026, 10, 5));
let effects = update(
&mut model,
Action::Merge(crate::domain::Board::new()),
at(2026, 10, 5),
);
assert!(effects.is_empty(), "an unchanged merge must not write");
}
#[test]
fn a_reload_while_editing_still_renames_the_opened_task() {
let mut model = model();
model.board.add("oldest", at(2026, 10, 4));
model.board.add("target", at(2026, 10, 5));
assert_eq!(
model.board.task(model.selected().unwrap()).unwrap().title,
"target"
);
update(&mut model, press(KeyCode::Char('e')), at(2026, 10, 5));
assert_eq!(model.ui.mode, Mode::Edit);
let mut reloaded = model.board.clone();
reloaded.add("newer", at(2026, 10, 6));
update(
&mut model,
Action::Reload {
board: reloaded,
damaged: None,
},
at(2026, 10, 6),
);
model.ui.input = tui_input::Input::new("renamed".to_string());
let effects = update(&mut model, press(KeyCode::Enter), at(2026, 10, 6));
assert_eq!(effects, vec![Effect::Save]);
let titles: Vec<&str> = model
.board
.tasks()
.iter()
.map(|task| task.title.as_str())
.collect();
assert!(titles.contains(&"renamed"), "{titles:?}");
assert!(
!titles.contains(&"target"),
"the opened task should have been renamed: {titles:?}"
);
assert!(
titles.contains(&"newer"),
"the newer task is untouched: {titles:?}"
);
assert!(titles.contains(&"oldest"), "{titles:?}");
}
#[test]
fn a_clean_reload_clears_a_stale_damage_problem() {
let mut model = model();
model.ui.board_damage = Some(Problem::new("board unreadable", "old parse error"));
update(
&mut model,
Action::Reload {
board: crate::domain::Board::new(),
damaged: None,
},
at(2026, 10, 6),
);
assert!(
model.ui.board_damage.is_none(),
"a reloaded healthy board must clear the stale problem"
);
}
#[test]
fn renaming_a_task_a_reload_removed_is_reported_not_dropped() {
let mut model = model();
model.board.add("target", at(2026, 10, 5));
update(&mut model, press(KeyCode::Char('e')), at(2026, 10, 5));
assert_eq!(model.ui.mode, Mode::Edit);
let mut reloaded = crate::domain::Board::new();
reloaded.add("different", at(2026, 10, 6));
update(
&mut model,
Action::Reload {
board: reloaded,
damaged: None,
},
at(2026, 10, 6),
);
model.ui.input = tui_input::Input::new("renamed".to_string());
let effects = update(&mut model, press(KeyCode::Enter), at(2026, 10, 6));
assert!(effects.is_empty(), "nothing changed, so nothing to save");
assert_eq!(model.ui.mode, Mode::Normal);
assert!(
model.ui.error.is_some(),
"the vanished edit target must be reported, not dropped silently"
);
}
}