Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
shell
repl
modal
read_keyword
read_password
read_yes_no
editor)
Expand Down
48 changes: 48 additions & 0 deletions examples/read_keyword.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
(*
* read_keyword.ml
* ----------------
* Copyright : (c) 2022, Shiwei Weng <[email protected]>
* Licence : BSD3
*
* This file is a part of Lambda-Term.
*)

(* Read a keyword and display it. *)

open Lwt_react
let ( >>= ) = Lwt.( >>= )

type move = Play | Pause

class read_keyword term = object(self)
inherit [move] LTerm_read_line.read_keyword () as super
inherit [move LTerm_read_line.read_keyword_result] LTerm_read_line.term term

method! send_action = function
| LTerm_read_line.Break ->
(* Ignore Ctrl+C *)
()
| action ->
super#send_action action

method! keywords = [
Zed_string.of_utf8 "play", Play;
Zed_string.of_utf8 "pause", Pause
]

initializer
self#set_prompt (S.const (LTerm_text.of_utf8 "Type a read_keyword: "))
end

let main () =
LTerm_inputrc.load ()
>>= fun () ->
Lazy.force LTerm.stdout
>>= fun term ->
(new read_keyword term)#run
>>= function
| Rk_value Play -> Lwt_io.printlf "You played it"
| Rk_value Pause -> Lwt_io.printlf "You paused it"
| Rk_error _ -> Lwt_io.printlf "You didn't type a keyword"

let () = Lwt_main.run (main ())