-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasics_Lwt_Term_simple_terminal_timeout.ml
More file actions
61 lines (51 loc) · 1.44 KB
/
Copy pathbasics_Lwt_Term_simple_terminal_timeout.ml
File metadata and controls
61 lines (51 loc) · 1.44 KB
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
53
54
55
56
57
58
59
60
61
(* ocamlfind ocamlc -o double_threads -package lwt,notty.lwt -linkpkg -g double_threads.ml*)
open Lwt
open Lwt.Infix
open Notty
open Notty_lwt
open Notty.Infix
module Term = Notty_lwt.Term
let counter = ref 0
let rec increase_counter () =
Lwt_unix.sleep 0.1
>>= fun () ->
(
if !counter < max_int then counter := (!counter + 1)
else counter := 0
);
Lwt.return ()
>>= fun () ->
increase_counter ()
let render (w, h) =
I.(strf ~attr:A.(fg lightblack) "[counter %d]" !counter)
let timer () = Lwt_unix.sleep 0.1 >|= fun () -> `Timer
let event term = Lwt_stream.get (Term.events term) >|= function
| Some (`Resize _ | #Unescape.event as x) -> x
| None -> `End
let rec loop term (e, t) dim =
(e <?> t) >>= function
| `End | `Key (`Escape, []) ->
Lwt.return_unit
| `Timer ->
Term.image term (render dim)
>>= fun () ->
loop term (e, timer ()) dim
| `Mouse ((`Press _|`Drag), (x, y), _) ->
loop term (event term, t) dim
| `Resize dim ->
Term.image term (render dim)
>>= fun () ->
loop term (event term, t) dim
| _ -> loop term (event term, t) dim
let interface () =
let tc = Unix.(tcgetattr stdin) in
Unix.(tcsetattr stdin TCSANOW { tc with c_isig = false });
let term = Term.create () in
let size = Term.size term in
loop term (event term, timer ()) size
let main () =
Lwt.choose [
increase_counter ();
interface ();
]
let () = Lwt_main.run (main ())