Skip to content
Browse files

Add function to interact with user and read input

  • Loading branch information...
1 parent 67552bb commit a3191808e9433f275fdfa5461c2663ba33ddffdd @OCamlPro-Bozman OCamlPro-Bozman committed Nov 15, 2012
Showing with 18 additions and 1 deletion.
  1. +3 −0 ocp-jslib/utils.ml
  2. +2 −0 ocp-jslib/utils.mli
  3. +2 −0 toplevel/toplevel.ml
  4. +5 −1 tutorial/tutorial.ml
  5. +6 −0 tutorial/tutorial.mli
View
3 ocp-jslib/utils.ml
@@ -29,6 +29,9 @@ let get_by_name id =
List.hd (Dom.list_of_nodeList (doc##getElementsByTagName (Js.string id))) in
Js.to_string div##innerHTML
+let read_from_input ?msg:(msg="") ?default:(default="") =
+ Js.to_string (window##prompt (_s msg, _s default))
+
let jsnew0 (constr : 'a Js.t Js.constr) () =
(Js.Unsafe.new_obj constr [| |] : 'a Js.t)
View
2 ocp-jslib/utils.mli
@@ -19,6 +19,8 @@ val get_by_id : string -> string
val get_by_name : string -> string
+val read_from_input : ?msg:string -> ?default:string -> string
+
(** {2 Constructors} *)
(** [jsnew0] is a function to build an object using contructor [constr]
View
2 toplevel/toplevel.ml
@@ -706,6 +706,8 @@ let run () =
Cookie.set_cookie "step" s;
Tutorial.step (int_of_string s) in
update_lesson_step !Tutorial.this_lesson !Tutorial.this_step;
+ let _ =
+ Tutorial.read_fun := (fun msg default -> read_from_input ~msg:msg ~default:default) in
Js._false
let main () =
View
6 tutorial/tutorial.ml
@@ -3,6 +3,11 @@ let debug = ref false
let debug_fun = ref (fun _ -> ())
let update_lang_fun = ref (fun _ -> ())
+let read_fun = ref (fun _ _ -> "")
+let read_bool = fun () -> bool_of_string (!read_fun "Enter your answer" "")
+let read_string = fun () -> !read_fun "Enter your answer" ""
+let read_int = fun () -> int_of_string (!read_fun "Enter your answer" "")
+let read_float = fun () -> float_of_string (!read_fun "Enter your answer" "")
let print_debug s = if !debug then (!debug_fun) s
@@ -208,7 +213,6 @@ let set_lang lang =
let lang () = !current_lang
-
external int_of_int : int -> int = "%identity"
external nativeint_of_nativeint : nativeint -> nativeint = "%identity"
external float_of_float : float -> float = "%identity"
View
6 tutorial/tutorial.mli
@@ -19,6 +19,12 @@ val lessons_table : (string * string * (string * (string * string)) list * (stri
val debug_fun : (string -> unit) ref
val message_fun : (string -> unit) ref
val update_lang_fun : (unit -> unit) ref
+val read_fun : (string -> string -> string) ref
+val read_bool : unit -> bool
+val read_string : unit -> string
+val read_int : unit -> int
+val read_float : unit -> float
+
val lessons : unit -> unit
val steps : unit -> unit

0 comments on commit a319180

Please sign in to comment.
Something went wrong with that request. Please try again.