From 6b377719c10d5ab3343fd5221f99a4a21008e25a Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 14 Mar 2024 08:26:58 +0100 Subject: Initial commit --- lib/helpers/console.ml | 16 ++++++++++++++++ lib/helpers/console.mli | 5 +++++ lib/helpers/dune | 8 ++++++++ lib/helpers/helpers.ml | 45 +++++++++++++++++++++++++++++++++++++++++++++ lib/helpers/toml.ml | 31 +++++++++++++++++++++++++++++++ lib/helpers/toml.mli | 1 + 6 files changed, 106 insertions(+) create mode 100644 lib/helpers/console.ml create mode 100644 lib/helpers/console.mli create mode 100755 lib/helpers/dune create mode 100755 lib/helpers/helpers.ml create mode 100644 lib/helpers/toml.ml create mode 100644 lib/helpers/toml.mli (limited to 'lib/helpers') diff --git a/lib/helpers/console.ml b/lib/helpers/console.ml new file mode 100644 index 0000000..838b25a --- /dev/null +++ b/lib/helpers/console.ml @@ -0,0 +1,16 @@ +let cursors = [| '|'; '/'; '-'; '\\' |] +let pos = ref 0 + +let update_cursor () = + if Unix.(isatty stdout) then ( + Printf.printf "%c[?25l%c[1D%c[0K%c" (char_of_int 27) (char_of_int 27) + (char_of_int 27) (Array.get cursors !pos); + pos := (!pos + 1) mod Array.length cursors) + +let close_cursor () = + if Unix.(isatty stdout) then + Printf.printf "%c[?25h%c[1D%c[0K\n%!" (char_of_int 27) (char_of_int 27) + (char_of_int 27) + +let clear_line () = + if Unix.(isatty stdout) then Printf.printf "%c[2K\r%!" (char_of_int 27) diff --git a/lib/helpers/console.mli b/lib/helpers/console.mli new file mode 100644 index 0000000..289d55c --- /dev/null +++ b/lib/helpers/console.mli @@ -0,0 +1,5 @@ +val update_cursor : unit -> unit +val close_cursor : unit -> unit + +val clear_line : unit -> unit +(** Clear the entire line *) diff --git a/lib/helpers/dune b/lib/helpers/dune new file mode 100755 index 0000000..8e30d2b --- /dev/null +++ b/lib/helpers/dune @@ -0,0 +1,8 @@ +(library + (name helpers) + (libraries + calendar + decoders + otoml + ) +) diff --git a/lib/helpers/helpers.ml b/lib/helpers/helpers.ml new file mode 100755 index 0000000..9d6fcb8 --- /dev/null +++ b/lib/helpers/helpers.ml @@ -0,0 +1,45 @@ +module Toml = Toml +module Console = Console + +let date_from_csv : string -> CalendarLib.Date.t option = + fun value -> + let open CalendarLib.Date in + try Some (Scanf.sscanf value "%d/%d/%d" (fun d m y -> make y m d)) with + | _ -> ( + (* If the date is a number, try from julian day *) + match int_of_string_opt value with + | None -> None + | Some v -> Some (add (make 1899 12 30) (Period.day v))) + +let fold_opt : ('a -> 'b -> 'a option) -> 'a -> 'b -> 'a = + fun f acc b -> + match f acc b with + | None -> acc + | Some v -> v + +let try_opt exp = + try Some (exp ()) with + | _ -> None + +let repr_date formatter date = + Format.fprintf formatter "%02d/%02d/%d" + (CalendarLib.Date.day_of_month date) + CalendarLib.Date.(int_of_month @@ month date) + (CalendarLib.Date.year date) + +let s_repr_date date = + Format.sprintf "%02d/%02d/%d" + (CalendarLib.Date.day_of_month date) + CalendarLib.Date.(int_of_month @@ month date) + (CalendarLib.Date.year date) + +let repr_opt f channel = function + | None -> () + | Some v -> f channel v + +let str_format f = + let buffer = Buffer.create 16 in + let formatter = Format.formatter_of_buffer buffer in + f formatter; + Format.pp_print_flush formatter (); + Buffer.contents buffer diff --git a/lib/helpers/toml.ml b/lib/helpers/toml.ml new file mode 100644 index 0000000..1b7fb15 --- /dev/null +++ b/lib/helpers/toml.ml @@ -0,0 +1,31 @@ +module Decode = struct + module S = struct + type value = Otoml.t + + let pp : Format.formatter -> value -> unit = + fun format t -> Format.pp_print_string format (Otoml.Printer.to_string t) + + let of_string : string -> (value, string) result = + Otoml.Parser.from_string_result + + let of_file : string -> (value, string) result = + Otoml.Parser.from_file_result + + let get_string : value -> string option = Otoml.get_opt Otoml.get_string + let get_int : value -> int option = Otoml.get_opt Otoml.get_integer + let get_float : value -> float option = Otoml.get_opt Otoml.get_float + let get_bool : value -> bool option = Otoml.get_opt Otoml.get_boolean + let get_null : value -> unit option = fun _ -> None + + let get_list : value -> value list option = + Otoml.get_opt @@ Otoml.get_array Fun.id + + let get_key_value_pairs : value -> (value * value) list option = + Otoml.get_opt (fun key -> + Otoml.get_table key |> List.map (fun (k, v) -> (Otoml.string k, v))) + + let to_list : value list -> value = Otoml.array + end + + include Decoders.Decode.Make (S) +end diff --git a/lib/helpers/toml.mli b/lib/helpers/toml.mli new file mode 100644 index 0000000..08d30b8 --- /dev/null +++ b/lib/helpers/toml.mli @@ -0,0 +1 @@ +module Decode : Decoders.Decode.S with type value = Otoml.t -- cgit v1.2.3