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
|
(*
* read_yes_no.ml
* --------------
* Copyright : (c) 2011, Jeremie Dimino <jeremie@dimino.org>
* Licence : BSD3
*
* This file is a part of Lambda-Term.
*)
open Lwt
let rec read_char term =
LTerm.read_event term >>= function
| LTerm_event.Key { LTerm_key.code = LTerm_key.Char ch; LTerm_key.control = true ; _ } when ch = Uchar.of_char 'c' ->
(* Exit on Ctrl+C *)
Lwt.fail (Failure "interrupted")
| LTerm_event.Key { LTerm_key.code = LTerm_key.Char ch ; _ } ->
Lwt.return ch
| _ ->
read_char term
let rec read_yes_no term =
LTerm.fprint term "Do you accept (y/n) ? "
>>= fun () ->
read_char term >|= Zed_utf8.singleton
>>= fun ch ->
LTerm.fprintl term ch
>>= fun () ->
match ch with
| "y" ->
return true
| "n" ->
return false
| _ ->
LTerm.fprintl term "Please enter 'y' or 'n'!"
>>= fun () ->
read_yes_no term
let main () =
Lazy.force LTerm.stdout
>>= fun term ->
LTerm.enter_raw_mode term
>>= fun mode ->
Lwt.finalize (fun () ->
read_yes_no term >>= function
| true ->
LTerm.fprintl term "You accepted."
| false ->
LTerm.fprintl term "You did not accept.")
(fun () -> LTerm.leave_raw_mode term mode)
let () = Lwt_main.run (main ())
|