93d7b85699c6e148dae8104de46466b2712680ba
[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 heading "Today";
42     List.iter (
43       fun (id, desc) ->
44         printf "    %s %s%s\n"
45                (string_of_task_desc desc) (string_of_taskid id)
46                (tags_of_task dbh id)
47     ) rows;
48
49     let rows = PGSQL(dbh) "select todo.taskid, todo.deadline, todo.estimate,
50                                   tasks.description
51                              from todo, tasks
52                             where todo.taskid = tasks.id
53                          order by todo.deadline" in
54     if rows <> [] then heading "To-do list";
55     List.iter (
56       fun (id, deadline, estimate, desc) ->
57         let deadline =
58           if Time.equal (Time.midnight ()) (Calendar.to_time deadline) then
59             Printer.Calendar.sprint "%F" deadline
60           else
61             Printer.Calendar.to_string deadline in
62         let estimate =
63           match estimate with
64           | None -> ""
65           | Some period -> string_of_estimate period ^ " " in
66         printf "    %s %s%s %s%s\n"
67                deadline estimate (string_of_task_desc desc)
68                (string_of_taskid id) (tags_of_task dbh id)
69     ) rows;
70
71     let rows = PGSQL(dbh) "
72          select tasks.id, tasks.description
73           from tasks
74          where not exists (select 1 from imminent where taskid = tasks.id)
75            and not exists (select 1 from todo where taskid = tasks.id)
76            and not exists (select 1 from ideas where taskid = tasks.id)
77            and not exists (select 1 from retired where taskid = tasks.id)
78       order by tasks.description" in
79     if rows <> [] then heading "Unsorted (use 'todo move' to move these)";
80     List.iter (
81       fun (id, desc) ->
82         printf "    %s %s%s\n"
83                (string_of_task_desc desc) (string_of_taskid id)
84                (tags_of_task dbh id)
85     ) rows;
86
87     let rows = PGSQL(dbh) "select ideas.taskid, tasks.description
88                              from ideas, tasks
89                             where ideas.taskid = tasks.id
90                          order by tasks.description" in
91     if rows <> [] then heading "Ideas";
92     List.iter (
93       fun (id, desc) ->
94         printf "    %s %s%s\n"
95                (string_of_task_desc desc) (string_of_taskid id)
96                (tags_of_task dbh id)
97     ) rows
98   ); (* unretired *)
99
100   if show_retired then (
101     let rows = PGSQL(dbh) "select retired.taskid, tasks.description
102                              from retired, tasks
103                             where retired.taskid = tasks.id
104                          order by tasks.description" in
105     if rows <> [] then heading "Retired";
106     List.iter (
107       fun (id, desc) ->
108         printf "    %s %s%s\n"
109                (string_of_task_desc desc) (string_of_taskid id)
110                (tags_of_task dbh id)
111     ) rows
112   )