Improve ordering of todo items

* Show uncompleted items first.
* Order uncompleted and completed items by priority.
* Order items with the same priority alphabetically.

Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Lukas Fleischer 2016-01-18 18:52:24 +01:00
parent 4bd8717d80
commit 25a049951c
3 changed files with 29 additions and 4 deletions

View File

@ -1024,6 +1024,7 @@ char *todo_hash(struct todo *);
void todo_write(struct todo *, FILE *); void todo_write(struct todo *, FILE *);
void todo_delete_note(struct todo *); void todo_delete_note(struct todo *);
void todo_delete(struct todo *); void todo_delete(struct todo *);
void todo_resort(struct todo *);
void todo_flag(struct todo *); void todo_flag(struct todo *);
int todo_get_position(struct todo *); int todo_get_position(struct todo *);
void todo_edit_note(struct todo *, const char *); void todo_edit_note(struct todo *, const char *);

View File

@ -62,8 +62,15 @@ struct todo *todo_get_item(int item_number, int skip_completed)
return LLIST_GET_DATA(i); return LLIST_GET_DATA(i);
} }
static int todo_cmp_id(struct todo *a, struct todo *b) static int todo_cmp(struct todo *a, struct todo *b)
{ {
if (a->completed && !b->completed)
return 1;
if (b->completed && !a->completed)
return -1;
if (a->id == b->id)
return strcmp(a->mesg, b->mesg);
return a->id - b->id; return a->id - b->id;
} }
@ -81,7 +88,7 @@ struct todo *todo_add(char *mesg, int id, int completed, char *note)
todo->note = (note != NULL todo->note = (note != NULL
&& note[0] != '\0') ? mem_strdup(note) : NULL; && note[0] != '\0') ? mem_strdup(note) : NULL;
LLIST_ADD_SORTED(&todolist, todo, todo_cmp_id); LLIST_ADD_SORTED(&todolist, todo, todo_cmp);
return todo; return todo;
} }
@ -139,10 +146,21 @@ void todo_delete(struct todo *todo)
mem_free(todo); mem_free(todo);
} }
/*
* Make sure an item is located at the right position within the sorted list.
*/
void todo_resort(struct todo *t)
{
llist_item_t *i = LLIST_FIND_FIRST(&todolist, t, NULL);
LLIST_REMOVE(&todolist, i);
LLIST_ADD_SORTED(&todolist, t, todo_cmp);
}
/* Flag a todo item. */ /* Flag a todo item. */
void todo_flag(struct todo *t) void todo_flag(struct todo *t)
{ {
t->completed = !t->completed; t->completed = !t->completed;
todo_resort(t);
} }
/* /*

View File

@ -120,7 +120,11 @@ void ui_todo_edit(void)
status_mesg(mesg, ""); status_mesg(mesg, "");
updatestring(win[STA].p, &item->mesg, 0, 1); updatestring(win[STA].p, &item->mesg, 0, 1);
todo_resort(item);
ui_todo_load_items();
io_set_modified(); io_set_modified();
listbox_set_sel(&lb_todo, todo_get_position(item));
} }
/* Pipe a todo item to an external program. */ /* Pipe a todo item to an external program. */
@ -287,6 +291,7 @@ void ui_todo_chg_priority(int diff)
item_new = todo_add(item->mesg, id, item->completed, item->note); item_new = todo_add(item->mesg, id, item->completed, item->note);
todo_delete(item); todo_delete(item);
io_set_modified(); io_set_modified();
listbox_set_sel(&lb_todo, todo_get_position(item_new)); listbox_set_sel(&lb_todo, todo_get_position(item_new));
} }
@ -306,9 +311,10 @@ void ui_todo_flag(void)
struct todo *item = ui_todo_selitem(); struct todo *item = ui_todo_selitem();
todo_flag(item); todo_flag(item);
if (ui_todo_view == TODO_HIDE_COMPLETED_VIEW)
ui_todo_load_items(); ui_todo_load_items();
io_set_modified(); io_set_modified();
listbox_set_sel(&lb_todo, todo_get_position(item));
} }
void ui_todo_view_note(void) void ui_todo_view_note(void)