Skip to content

Commit a319180

Browse files
author
Çagdas Bozman
committed
Add function to interact with user and read input
1 parent 67552bb commit a319180

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed

ocp-jslib/utils.ml

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ let get_by_name id =
2929
List.hd (Dom.list_of_nodeList (doc##getElementsByTagName (Js.string id))) in
3030
Js.to_string div##innerHTML
3131

32+
let read_from_input ?msg:(msg="") ?default:(default="") =
33+
Js.to_string (window##prompt (_s msg, _s default))
34+
3235
let jsnew0 (constr : 'a Js.t Js.constr) () =
3336
(Js.Unsafe.new_obj constr [| |] : 'a Js.t)
3437

ocp-jslib/utils.mli

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ val get_by_id : string -> string
1919

2020
val get_by_name : string -> string
2121

22+
val read_from_input : ?msg:string -> ?default:string -> string
23+
2224
(** {2 Constructors} *)
2325

2426
(** [jsnew0] is a function to build an object using contructor [constr]

toplevel/toplevel.ml

+2
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,8 @@ let run () =
706706
Cookie.set_cookie "step" s;
707707
Tutorial.step (int_of_string s) in
708708
update_lesson_step !Tutorial.this_lesson !Tutorial.this_step;
709+
let _ =
710+
Tutorial.read_fun := (fun msg default -> read_from_input ~msg:msg ~default:default) in
709711
Js._false
710712

711713
let main () =

tutorial/tutorial.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ let debug = ref false
33

44
let debug_fun = ref (fun _ -> ())
55
let update_lang_fun = ref (fun _ -> ())
6+
let read_fun = ref (fun _ _ -> "")
7+
let read_bool = fun () -> bool_of_string (!read_fun "Enter your answer" "")
8+
let read_string = fun () -> !read_fun "Enter your answer" ""
9+
let read_int = fun () -> int_of_string (!read_fun "Enter your answer" "")
10+
let read_float = fun () -> float_of_string (!read_fun "Enter your answer" "")
611

712
let print_debug s = if !debug then (!debug_fun) s
813

@@ -208,7 +213,6 @@ let set_lang lang =
208213

209214
let lang () = !current_lang
210215

211-
212216
external int_of_int : int -> int = "%identity"
213217
external nativeint_of_nativeint : nativeint -> nativeint = "%identity"
214218
external float_of_float : float -> float = "%identity"

tutorial/tutorial.mli

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ val lessons_table : (string * string * (string * (string * string)) list * (stri
1919
val debug_fun : (string -> unit) ref
2020
val message_fun : (string -> unit) ref
2121
val update_lang_fun : (unit -> unit) ref
22+
val read_fun : (string -> string -> string) ref
23+
val read_bool : unit -> bool
24+
val read_string : unit -> string
25+
val read_int : unit -> int
26+
val read_float : unit -> float
27+
2228

2329
val lessons : unit -> unit
2430
val steps : unit -> unit

0 commit comments

Comments
 (0)