aboutsummaryrefslogtreecommitdiff
path: root/lib/helpers
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2024-03-14 08:26:58 +0100
committerSébastien Dailly <sebastien@dailly.me>2024-03-14 08:26:58 +0100
commit6b377719c10d5ab3343fd5221f99a4a21008e25a (patch)
treea7c1e9a820d339a2f161af3e09cf9e3161286796 /lib/helpers
Initial commitmain
Diffstat (limited to 'lib/helpers')
-rw-r--r--lib/helpers/console.ml16
-rw-r--r--lib/helpers/console.mli5
-rwxr-xr-xlib/helpers/dune8
-rwxr-xr-xlib/helpers/helpers.ml45
-rw-r--r--lib/helpers/toml.ml31
-rw-r--r--lib/helpers/toml.mli1
6 files changed, 106 insertions, 0 deletions
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