From cf00deb1edc68af1154509eacd7a99b4c8555aa0 Mon Sep 17 00:00:00 2001 From: cqc Date: Sat, 17 Feb 2024 12:48:17 -0600 Subject: [PATCH] init.ml --- .config/.ocamlformat | 0 .config/dune | 19 ++++++++ .config/dune-project | 2 + .config/init.ml | 114 +++++++++++++++++++++++++++++++++++++++++++ .gitignore | 1 + 5 files changed, 136 insertions(+) create mode 100644 .config/.ocamlformat create mode 100644 .config/dune create mode 100644 .config/dune-project create mode 100644 .config/init.ml create mode 100644 .gitignore diff --git a/.config/.ocamlformat b/.config/.ocamlformat new file mode 100644 index 0000000..e69de29 diff --git a/.config/dune b/.config/dune new file mode 100644 index 0000000..6b9652b --- /dev/null +++ b/.config/dune @@ -0,0 +1,19 @@ +(library + (name init) + (libraries + store + js_of_ocaml-compiler js_of_ocaml-tyxml js_of_ocaml-toplevel + lwt js_of_ocaml-lwt + ;; not used directly + graphics + js_of_ocaml.deriving + react reactiveData + str dynlink + ocp-indent.lib + higlo + js_of_ocaml-lwt.graphics + js_of_ocaml-ppx + uucp + httpaf) + (modules init) +) diff --git a/.config/dune-project b/.config/dune-project new file mode 100644 index 0000000..3795524 --- /dev/null +++ b/.config/dune-project @@ -0,0 +1,2 @@ +(lang dune 3.4) +(name init) diff --git a/.config/init.ml b/.config/init.ml new file mode 100644 index 0000000..3a1205c --- /dev/null +++ b/.config/init.ml @@ -0,0 +1,114 @@ +open Js_of_ocaml +open Js_of_ocaml_tyxml +open Lwt +open Store + +let by_id s = Dom_html.getElementById s + +let by_id_coerce s f = + Js.Opt.get + (f (Dom_html.getElementById s)) + (fun () -> raise Not_found) + +let do_by_id s f = + try f (Dom_html.getElementById s) with Not_found -> () + +let resize ~container ~textbox () = + Lwt.pause () >>= fun () -> + textbox##.style##.height := Js.string "auto"; + textbox##.style##.height + := Js.string (Printf.sprintf "%dpx" (max 18 textbox##.scrollHeight)); + container##.scrollTop := container##.scrollHeight; + Lwt.return () + +let setup_storeview ~container ~textbox ~(storeview : Store.t Lwt.t) : + unit = + let storeview_container = by_id "toplevel-storeview" in + Lwt.async (fun _ -> + storeview >>= fun storeview -> + Firebug.console##log "setup_storeview"; + Store.S.Tree.list storeview [] >>= fun all -> + ignore + (List.fold_left + (fun acc tok -> + match tok with + | step, _tree -> + let a = + Tyxml_js.Html.( + a + ~a: + [ + a_class [ "list-group-item" ]; + a_onclick (fun _ -> + textbox##.value := + (Js.string acc)##trim; + Lwt.async (fun () -> + resize ~container ~textbox () + >>= fun () -> + textbox##focus; + Lwt.return_unit); + true); + ] + [ txt step ]) + in + Dom.appendChild storeview_container + (Tyxml_js.To_dom.of_a a); + "") + "" all); + Lwt.return_unit) + +(* TODO replace with Angstrom like httpaf *) +let headers_of_string_list (sl : string list) : Httpaf.Headers.t = + Httpaf.Headers.of_list + (List.filter_map + (fun s -> + Firebug.console##log (F.str "headerparse: %s" s); + Option.map + (fun i -> + ( String.trim (String.sub s 0 i), + String.trim + (String.sub s (i + 1) (String.length s - i - 1)) )) + (String.index_opt s ':')) + sl) + +let setup_workspace ~container cstore : unit Lwt.t = + let render ~container cstore : unit Lwt.t = + let module Headers = Httpaf.Headers in + Store.S.Tree.list cstore [] >>= fun csl -> + container##.innerHTML := Js.string ""; + Lwt_list.iter_s + (fun (step, tree) -> + Firebug.console##log (F.str ".config/workspace/%s" step); + Store.S.Tree.get tree [] >>= fun contents -> + let headers = + headers_of_string_list (String.split_on_char '\n' contents) + in + Firebug.console##log + (F.str "Headers:\n%a" Headers.pp_hum headers); + let uri = + Uri.of_string + (Option.fold ~none:"" + ~some:(fun s -> s) + (Headers.get headers "path")) + in + Dom.appendChild container + (Tyxml_js.To_dom.of_a + Tyxml_js.Html.( + a + ~a:[ a_class [ "window" ] ] + [ + div + ~a:[ a_class [ "status" ] ] + [ + txt + (F.str "Name: %s; Path: %a" step Uri.pp_hum + uri); + ]; + div + ~a:[ a_class [ "output" ] ] + [ txt (F.str "%s" contents) ]; + ])); + Lwt.return_unit) + csl + in + cstore >>= render ~container diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e4e5f6c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ \ No newline at end of file