Initial commit.
[todo.git] / todo_list.ml
1 (* Implement 'list' subcommand. *)
2
3 open CalendarLib
4
5 open Todo_types
6 open Todo_utils
7 open Todo_tag_utils
8
9 open Printf
10
11 let tags_of_task dbh id =
12   let rows = PGSQL(dbh) "select tags.name, tags.colour
13                            from tags_tasks, tags
14                           where tags_tasks.taskid = $id
15                             and tags_tasks.tagid = tags.id
16                        order by tags.name" in
17   if rows = [] then ""
18   else
19     " " ^
20     String.concat " "
21       (List.map (
22          fun (name, colour) ->
23            string_of_tag name colour
24        ) rows)
25
26 let cmd_list dbh anon_params list_retired list_all =
27   if anon_params <> [] then
28     error "extra parameters to 'list' subcommand";
29
30   let show_unretired, show_retired =
31     match list_retired, list_all with
32     | _, true -> true, true
33     | false, false -> true, false
34     | true, false -> false, true in
35
36   if show_unretired then (
37     let rows = PGSQL(dbh) "select imminent.taskid, tasks.description
38                              from imminent, tasks
39                             where imminent.taskid = tasks.id
40                          order by tasks.description" in
41     if rows <> [] then printf "Today:\n";
42     List.iter (
43       fun (id, desc) ->
44         printf "    %s #%ld%s\n" desc id (tags_of_task dbh id)
45     ) rows;
46
47     let rows = PGSQL(dbh) "select todo.taskid, todo.deadline, todo.estimate,
48                                   tasks.description
49                              from todo, tasks
50                             where todo.taskid = tasks.id
51                          order by todo.deadline" in
52     if rows <> [] then printf "To-do list:\n";
53     List.iter (
54       fun (id, deadline, estimate, desc) ->
55         let deadline =
56           if Time.equal (Time.midnight ()) (Calendar.to_time deadline) then
57             Printer.Calendar.sprint "%F" deadline
58           else
59             Printer.Calendar.to_string deadline in
60         let estimate =
61           match estimate with
62           | None -> ""
63           | Some period -> string_of_estimate period ^ " " in
64         printf "    %s %s%s #%ld%s\n"
65                deadline estimate desc id (tags_of_task dbh id)
66     ) rows;
67
68     let rows = PGSQL(dbh) "
69          select tasks.id, tasks.description
70           from tasks
71          where not exists (select 1 from imminent where taskid = tasks.id)
72            and not exists (select 1 from todo where taskid = tasks.id)
73            and not exists (select 1 from ideas where taskid = tasks.id)
74            and not exists (select 1 from retired where taskid = tasks.id)
75       order by tasks.description" in
76     if rows <> [] then printf "Unsorted (use 'todo move' to move these):\n";
77     List.iter (
78       fun (id, desc) ->
79         printf "    %s #%ld%s\n" desc id (tags_of_task dbh id)
80     ) rows;
81
82     let rows = PGSQL(dbh) "select ideas.taskid, tasks.description
83                              from ideas, tasks
84                             where ideas.taskid = tasks.id
85                          order by tasks.description" in
86     if rows <> [] then printf "Ideas:\n";
87     List.iter (
88       fun (id, desc) ->
89         printf "    %s #%ld%s\n" desc id (tags_of_task dbh id)
90     ) rows
91   ); (* unretired *)
92
93   if show_retired then (
94     let rows = PGSQL(dbh) "select retired.taskid, tasks.description
95                              from retired, tasks
96                             where retired.taskid = tasks.id
97                          order by tasks.description" in
98     if rows <> [] then printf "Retired:\n";
99     List.iter (
100       fun (id, desc) ->
101         printf "    %s #%ld%s\n" desc id (tags_of_task dbh id)
102     ) rows
103   )