Command-line and Emacs Calendar Client
1open Cmdliner
2open Caledonia_lib
3open Query_args
4
5let run ?from_str ?to_str ~calendar:calendars ?count ~format ~today ~tomorrow
6 ~week ~month ?timezone ~sort ~fs calendar_dir =
7 let ( let* ) = Result.bind in
8 let tz = Query_args.parse_timezone ~timezone in
9 let* from, to_ =
10 match
11 Date.convert_relative_date_formats ~tz ~today ~tomorrow ~week ~month ()
12 with
13 | Some (from, to_) ->
14 let* _ =
15 match (from_str, to_str) with
16 | None, None -> Ok ()
17 | _ ->
18 Error
19 (`Msg
20 "Can't specify --from / --to when using --today, --week, \
21 --month")
22 in
23 Ok (Some from, to_)
24 | None -> (
25 let* from =
26 match from_str with
27 | None -> Ok None
28 | Some s ->
29 let* d = Date.parse_date ~tz s `From in
30 Ok (Some d)
31 in
32 let* to_ =
33 match to_str with
34 | None -> Ok None
35 | Some s ->
36 let* d = Date.parse_date ~tz s `To in
37 Ok (Some d)
38 in
39 match (from, to_) with
40 | Some f, Some t -> Ok (Some f, Date.to_end_of_day t)
41 | Some f, None ->
42 let one_month_later = Date.add_months f 1 in
43 Ok (Some f, one_month_later)
44 | None, Some t ->
45 let today_date = !Date.get_today ~tz () in
46 Ok (Some today_date, Date.to_end_of_day t)
47 | None, None ->
48 let today_date = !Date.get_today ~tz () in
49 let one_month_later = Date.add_months today_date 1 in
50 Ok (Some today_date, one_month_later))
51 in
52 let filter =
53 match calendars with
54 | [] -> None
55 | calendar -> Some (Query.in_calendars calendar)
56 in
57 let comparator = Query_args.create_event_comparator sort in
58 let* results =
59 Query.query ~fs calendar_dir ?filter ~from ~to_ ~comparator ?limit:count ()
60 in
61 if results = [] then print_endline "No events found."
62 else print_endline (Event.format_events ~format ~tz results);
63 Ok ()
64
65let cmd ~fs calendar_dir =
66 let run from_str to_str calendars count format today tomorrow week month
67 timezone sort =
68 match
69 run ?from_str ?to_str ~calendar:calendars ?count ~format ~today ~tomorrow
70 ~week ~month ?timezone ~sort ~fs calendar_dir
71 with
72 | Error (`Msg msg) ->
73 Printf.eprintf "Error: %s\n%!" msg;
74 1
75 | Ok () -> 0
76 in
77 let term =
78 Term.(
79 const run $ from_arg $ to_arg $ calendar_arg $ count_arg $ format_arg
80 $ today_arg $ tomorrow_arg $ week_arg $ month_arg $ timezone_arg
81 $ sort_arg)
82 in
83 let doc = "List calendar events" in
84 let man =
85 [
86 `S Manpage.s_description;
87 `P
88 "List calendar events within a specified date range. By default, \
89 events from today to one month from today are shown. You can use date \
90 flags to show events for a specific time period, and filter events \
91 with the --sort option.";
92 `S Manpage.s_examples;
93 `I ("List all events for today:", "caled list --today");
94 `I ("List all events for tomorrow:", "caled list --tomorrow");
95 `I ("List all events for the current week:", "caled list --week");
96 `I ("List all events for the current month:", "caled list --month");
97 `I
98 ( "List events within a specific date range:",
99 "caled list --from 2025-03-27 --to 2025-04-01" );
100 `I ("List events from a specific calendar:", "caled list --calendar work");
101 `I ("List events in JSON format:", "caled list --format json");
102 `I ("Limit the number of events shown:", "caled list --count 5");
103 `I
104 ( "Sort by multiple fields (start time and summary):",
105 "caled list --sort start --sort summary" );
106 `I
107 ( "Sort by calendar name in descending order:",
108 "caled list --sort calendar:desc" );
109 `S Manpage.s_options;
110 ]
111 @ date_format_manpage_entries
112 @ [ `S Manpage.s_see_also ]
113 in
114 let exit_info =
115 [ Cmd.Exit.info ~doc:"on success." 0; Cmd.Exit.info ~doc:"on error." 1 ]
116 in
117 let info = Cmd.info "list" ~doc ~man ~exits:exit_info in
118 Cmd.v info term