Command-line and Emacs Calendar Client
1open Cmdliner 2open Caledonia_lib 3open Query_args 4 5let run ?from_str ?to_str ?calendar ?count ~format ~today ~tomorrow ~week ~month 6 ?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 `From 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 calendar with 54 | Some collection_id -> 55 Some (Query.in_collections [ Collection.Col collection_id ]) 56 | None -> None 57 in 58 let comparator = Query_args.create_event_comparator sort in 59 let* results = 60 Query.query ~fs calendar_dir ?filter ~from ~to_ ~comparator ?limit:count () 61 in 62 if results = [] then print_endline "No events found." 63 else print_endline (Event.format_events ~format ~tz results); 64 Ok () 65 66let cmd ~fs calendar_dir = 67 let run from_str to_str calendar count format today tomorrow week month 68 timezone sort = 69 match 70 run ?from_str ?to_str ?calendar ?count ~format ~today ~tomorrow ~week 71 ~month ?timezone ~sort ~fs calendar_dir 72 with 73 | Error (`Msg msg) -> 74 Printf.eprintf "Error: %s\n%!" msg; 75 1 76 | Ok () -> 0 77 in 78 let term = 79 Term.( 80 const run $ from_arg $ to_arg $ calendar_arg $ count_arg $ format_arg 81 $ today_arg $ tomorrow_arg $ week_arg $ month_arg $ timezone_arg 82 $ sort_arg) 83 in 84 let doc = "List calendar events" in 85 let man = 86 [ 87 `S Manpage.s_description; 88 `P "List calendar events within a specified date range."; 89 `P "By default, events from today to one month from today are shown."; 90 `P "You can use date flags to show events for a specific time period."; 91 `P "You can also filter events by calendar using the --calendar flag."; 92 `P "Use the --sort option to control the sorting of results."; 93 `S Manpage.s_options; 94 ] 95 @ date_format_manpage_entries 96 @ [ 97 `S Manpage.s_examples; 98 `I ("List all events for today:", "caled list --today"); 99 `I ("List all events for tomorrow:", "caled list --tomorrow"); 100 `I ("List all events for the current week:", "caled list --week"); 101 `I ("List all events for the current month:", "caled list --month"); 102 `I 103 ( "List events within a specific date range:", 104 "caled list --from 2025-03-27 --to 2025-04-01" ); 105 `I 106 ("List events from a specific calendar:", "caled list --calendar work"); 107 `I ("List events in JSON format:", "caled list --format json"); 108 `I ("Limit the number of events shown:", "caled list --count 5"); 109 `I 110 ( "Sort by multiple fields (start time and summary):", 111 "caled list --sort start --sort summary" ); 112 `I 113 ( "Sort by calendar name in descending order:", 114 "caled list --sort calendar:desc" ); 115 ] 116 in 117 let exit_info = 118 [ Cmd.Exit.info ~doc:"on success." 0; Cmd.Exit.info ~doc:"on error." 1 ] 119 in 120 let info = Cmd.info "list" ~doc ~man ~exits:exit_info in 121 Cmd.v info term