From 8e02c69b9f32ec7c56f60bc56bfd4a6873f2f7e9 Mon Sep 17 00:00:00 2001 From: Weng Shiwei Date: Mon, 28 Mar 2022 12:39:09 -0400 Subject: [PATCH] Add an example for `LTerm_read_line.read_keyword`. --- examples/dune | 1 + examples/read_keyword.ml | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 examples/read_keyword.ml diff --git a/examples/dune b/examples/dune index 5e7152a8..9c42a4b0 100644 --- a/examples/dune +++ b/examples/dune @@ -16,6 +16,7 @@ shell repl modal + read_keyword read_password read_yes_no editor) diff --git a/examples/read_keyword.ml b/examples/read_keyword.ml new file mode 100644 index 00000000..f488ee9c --- /dev/null +++ b/examples/read_keyword.ml @@ -0,0 +1,48 @@ +(* + * read_keyword.ml + * ---------------- + * Copyright : (c) 2022, Shiwei Weng + * 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 ())