package quickterface

  1. Overview
  2. Docs

Source file log.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
open! Core
open! Js_of_ocaml

type t = {
  document : Dom_html.document Js.t;
  container : Dom_html.divElement Js.t;
}

let make ~document ~main_container : t =
  let container = Dom_html.createDiv document in
  (container##.className := Class.(to_js_string Log_container));
  Dom.appendChild main_container container;

  { document; container }

let add_item { document; container } ~item_element () =
  let container_div = Dom_html.createDiv document in
  container_div##.className := Class.to_js_string Class.Log_item;
  Dom.appendChild container_div item_element;

  Dom.appendChild container container_div;
  Lwt.return ()

let input_text ?prompt ({ document; container = _ } as t) () =
  let input_text = Inputs.Text.make prompt ~document in
  let%lwt () = add_item t ~item_element:(Inputs.Text.element input_text) () in
  Inputs.Text.wait_for_input input_text ()

let input_integer ({ document; container = _ } as t) () =
  let input = Inputs.Integer.make () ~document in
  let%lwt () = add_item t ~item_element:(Inputs.Integer.element input) () in
  Inputs.Integer.wait_for_input input ()

let input_single_selection ({ document; container = _ } as t) options
    option_to_string () =
  let input =
    Inputs.Single_selection.make (options, option_to_string) ~document
  in
  let%lwt () =
    add_item t ~item_element:(Inputs.Single_selection.element input) ()
  in
  Inputs.Single_selection.wait_for_input input ()

let input_single_selection_string t options =
  input_single_selection t options Fn.id

let input_multi_selection ({ document; container = _ } as t) options
    option_to_string () =
  let input =
    Inputs.Multi_selection.make (options, option_to_string) ~document
  in
  let%lwt () =
    add_item t ~item_element:(Inputs.Multi_selection.element input) ()
  in
  Inputs.Multi_selection.wait_for_input input ()

let input_multi_selection_string t options =
  input_multi_selection t options Fn.id

let add_output_text ?(options = Quickterface.Output_text_options.default)
    ({ document; container = _ } as t) ~value () =
  let%lwt output_text = Outputs.Text.make ~document ~options ~value in
  let%lwt () = add_item t ~item_element:(Outputs.Text.element output_text) () in
  Lwt.return ()

let add_output_math ?(options = Quickterface.Output_text_options.default)
    ({ document; container = _ } as t) ~value () =
  let%lwt output_math = Outputs.Math.make ~document ~options ~value in
  let%lwt () = add_item t ~item_element:(Outputs.Math.element output_math) () in
  Lwt.return ()

let add_output_title ({ document; container = _ } as t) ~value () =
  let%lwt output_title = Outputs.Title.make ~document ~options:() ~value in
  let%lwt () =
    add_item t ~item_element:(Outputs.Title.element output_title) ()
  in
  Lwt.return ()

let with_progress_bar ?label ({ document; container = _ } as t) ~maximum ~f () =
  let%lwt progress_bar = Outputs.Progress_bar.make ~document ~label ~maximum in
  let%lwt () =
    add_item t ~item_element:(Outputs.Progress_bar.element progress_bar) ()
  in

  let curr_value = ref 0 in
  let increment_progress_bar () =
    curr_value := !curr_value + 1;
    Outputs.Progress_bar.set_value progress_bar !curr_value ()
  in
  let%lwt result = f ~increment_progress_bar () in

  let%lwt () = Outputs.Progress_bar.finish progress_bar () in
  Lwt.return result