

Task

Obtain a valid   Y   or   N   response from the keyboard.

The keyboard should be flushed, so that any outstanding key-presses are removed, preventing any existing   Y   or   N   key-press from being evaluated.

The response should be obtained as soon as   Y   or   N   are pressed, and there should be no need to press an   enter   key.




let attrs = Unix.(tcgetattr stdin)
let buf = Bytes.create 1
 
let prompt switch =
    Unix.(tcsetattr stdin TCSAFLUSH)
      @@ if switch then { attrs with c_icanon = false } else attrs
 
let getchar () =
    let len = Unix.(read stdin) buf 0 1 in
      if len = 0 then raise End_of_file else Bytes.get buf 0