refactor(remind): extract buffer primitives for remind rendering
This commit is contained in:
156
bin/remind.ml
156
bin/remind.ml
@@ -42,98 +42,92 @@ let empty =
|
|||||||
exdate = [];
|
exdate = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
let render_yearly month day summary =
|
(* ── buffer primitives ────────────────────────────────────────── *)
|
||||||
let month_str = month_of_int month |> string_of_month in
|
|
||||||
spf "REM %s %d MSG %s\n" month_str day summary
|
|
||||||
|
|
||||||
let render_weekly rem weekly =
|
let add_rem b = Buffer.add_string b "REM "
|
||||||
let b = Buffer.create 256 in
|
let add_info b uuid = Buffer.add_string b (spf "INFO \"UID: %s\" " uuid)
|
||||||
List.iter
|
let add_date b date = Buffer.add_string b (Timedesc.Date.to_rfc3339 date)
|
||||||
begin fun weekday ->
|
let add_weekday b wd = Buffer.add_string b (spf "%s " (string_of_weekday wd))
|
||||||
Buffer.add_string b "REM ";
|
|
||||||
Buffer.add_string b (spf "INFO \"UID: %s\" " rem.original_uuid);
|
|
||||||
Buffer.add_string b (spf "%s " (string_of_weekday weekday));
|
|
||||||
Buffer.add_string b (Timedesc.Date.to_rfc3339 rem.date);
|
|
||||||
Buffer.add_string b " ";
|
|
||||||
(match weekly.interval with
|
|
||||||
| Some interval -> Buffer.add_string b (spf "*%d " (interval * 7))
|
|
||||||
| None -> Buffer.add_string b "*7 ");
|
|
||||||
|
|
||||||
(match weekly.count_or_until with
|
let add_interval b w =
|
||||||
| Some (`Count count) -> begin
|
let n = Option.value ~default:1 w.interval in
|
||||||
(* We must compute the until date based on the count and the interval *)
|
Buffer.add_string b (spf "*%d " (n * 7))
|
||||||
|
|
||||||
|
let add_until b rem w =
|
||||||
|
match w.count_or_until with
|
||||||
|
| None -> ()
|
||||||
|
| Some (`Until d) ->
|
||||||
|
let ts = timedesc_of_utc_or_timestamp_local d in
|
||||||
|
Buffer.add_string b (spf "UNTIL %s " (Timedesc.Date.to_rfc3339 (Timedesc.date ts)))
|
||||||
|
| Some (`Count count) ->
|
||||||
let wd = Timedesc.Date.weekday rem.date in
|
let wd = Timedesc.Date.weekday rem.date in
|
||||||
let wd_int = Timedesc.Utils.tm_int_of_weekday wd in
|
let wd_int = Timedesc.Utils.tm_int_of_weekday wd in
|
||||||
let day_to_subtract =
|
let sub =
|
||||||
match weekly.week_start with
|
match w.week_start with
|
||||||
| Some `Sunday -> wd_int
|
|
||||||
| Some `Monday -> wd_int - 1
|
| Some `Monday -> wd_int - 1
|
||||||
| None -> wd_int (* Default to Sunday if not specified *)
|
| _ -> wd_int
|
||||||
in
|
in
|
||||||
let interval = Option.value ~default:1 weekly.interval in
|
let iv = Option.value ~default:1 w.interval in
|
||||||
let until_date = Timedesc.Date.add ~days:((count * 7 * interval) - day_to_subtract) rem.date in
|
let until = Timedesc.Date.add ~days:((count * 7 * iv) - sub) rem.date in
|
||||||
Buffer.add_string b "UNTIL ";
|
Buffer.add_string b (spf "UNTIL %s " (Timedesc.Date.to_rfc3339 until))
|
||||||
Buffer.add_string b (Timedesc.Date.to_rfc3339 until_date);
|
|
||||||
Buffer.add_string b " "
|
|
||||||
end
|
|
||||||
| Some (`Until until_date) -> begin
|
|
||||||
Buffer.add_string b "UNTIL ";
|
|
||||||
let ts = timedesc_of_utc_or_timestamp_local until_date in
|
|
||||||
Buffer.add_string b (Timedesc.Date.to_rfc3339 (Timedesc.date ts));
|
|
||||||
Buffer.add_string b " "
|
|
||||||
end
|
|
||||||
| None -> ());
|
|
||||||
|
|
||||||
(match rem.time with
|
let add_at b = function
|
||||||
| Some time ->
|
| Some t -> Buffer.add_string b (spf " AT %s" (string_of_time t))
|
||||||
Buffer.add_string b " AT ";
|
| None -> ()
|
||||||
Buffer.add_string b (string_of_time time)
|
|
||||||
| None -> ());
|
|
||||||
|
|
||||||
(match rem.duration with
|
let add_duration b = function
|
||||||
| Some duration ->
|
| Some d -> Buffer.add_string b (spf " DURATION %s" (string_of_span d))
|
||||||
Buffer.add_string b " DURATION ";
|
| None -> ()
|
||||||
Buffer.add_string b (string_of_span duration);
|
|
||||||
Buffer.add_string b ""
|
|
||||||
| None -> ());
|
|
||||||
|
|
||||||
Buffer.add_string b " MSG ";
|
let add_through b = function
|
||||||
Buffer.add_string b rem.summary;
|
| Some d -> Buffer.add_string b (spf " THROUGH %s" (Timedesc.Date.to_rfc3339 d))
|
||||||
Buffer.add_string b "\n"
|
| None -> ()
|
||||||
end
|
|
||||||
weekly.byday;
|
let add_msg b summary = Buffer.add_string b (spf " MSG %s\n" summary)
|
||||||
|
|
||||||
|
(* ── rendering ────────────────────────────────────────────────── *)
|
||||||
|
|
||||||
|
let render_weekly rem w =
|
||||||
|
let b = Buffer.create 256 in
|
||||||
|
List.iter
|
||||||
|
(fun wd ->
|
||||||
|
add_rem b;
|
||||||
|
add_info b rem.original_uuid;
|
||||||
|
add_weekday b wd;
|
||||||
|
add_date b rem.date;
|
||||||
|
Buffer.add_char b ' ';
|
||||||
|
add_interval b w;
|
||||||
|
add_until b rem w;
|
||||||
|
add_at b rem.time;
|
||||||
|
add_duration b rem.duration;
|
||||||
|
add_msg b rem.summary)
|
||||||
|
w.byday;
|
||||||
Buffer.contents b
|
Buffer.contents b
|
||||||
|
|
||||||
|
let render_single rem =
|
||||||
|
let b = Buffer.create 256 in
|
||||||
|
add_rem b;
|
||||||
|
add_info b rem.original_uuid;
|
||||||
|
add_date b rem.date;
|
||||||
|
add_at b rem.time;
|
||||||
|
add_duration b rem.duration;
|
||||||
|
add_through b rem.end_date;
|
||||||
|
add_msg b rem.summary;
|
||||||
|
Buffer.contents b
|
||||||
|
|
||||||
|
let render_yearly month day summary =
|
||||||
|
let b = Buffer.create 64 in
|
||||||
|
add_rem b;
|
||||||
|
Buffer.add_string b (spf "%s %d" (month_of_int month |> string_of_month) day);
|
||||||
|
add_msg b summary;
|
||||||
|
Buffer.contents b
|
||||||
|
|
||||||
|
(* ── dispatcher ───────────────────────────────────────────────── *)
|
||||||
|
|
||||||
let string_of_rem rem =
|
let string_of_rem rem =
|
||||||
match rem.weekly with
|
match rem.weekly with
|
||||||
| Some weekly -> render_weekly rem weekly
|
| Some w -> render_weekly rem w
|
||||||
| None ->
|
| None -> (
|
||||||
begin match rem.yearly with
|
match rem.yearly with
|
||||||
| Some (month, day) -> render_yearly month day rem.summary
|
| Some (month, day) -> render_yearly month day rem.summary
|
||||||
| None -> begin
|
| None -> render_single rem)
|
||||||
let b = Buffer.create 256 in
|
|
||||||
Buffer.add_string b "REM ";
|
|
||||||
Buffer.add_string b (spf "INFO \"UID: %s\" " rem.original_uuid);
|
|
||||||
Buffer.add_string b (Timedesc.Date.to_rfc3339 rem.date);
|
|
||||||
(match rem.time with
|
|
||||||
| Some time ->
|
|
||||||
Buffer.add_string b " AT ";
|
|
||||||
Buffer.add_string b (string_of_time time)
|
|
||||||
| None -> ());
|
|
||||||
(match rem.duration with
|
|
||||||
| Some duration ->
|
|
||||||
Buffer.add_string b " DURATION ";
|
|
||||||
Buffer.add_string b (string_of_span duration);
|
|
||||||
Buffer.add_string b ""
|
|
||||||
| None -> ());
|
|
||||||
(match rem.end_date with
|
|
||||||
| Some end_date ->
|
|
||||||
Buffer.add_string b " THROUGH ";
|
|
||||||
Buffer.add_string b (Timedesc.Date.to_rfc3339 end_date)
|
|
||||||
| None -> ());
|
|
||||||
Buffer.add_string b " MSG ";
|
|
||||||
Buffer.add_string b rem.summary;
|
|
||||||
Buffer.add_string b "\n";
|
|
||||||
Buffer.contents b
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|||||||
Reference in New Issue
Block a user