-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.ml
57 lines (49 loc) · 1.54 KB
/
controller.ml
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
(* Controller Module *)
open Lwt
open LTerm_geom
open State
open Clview
open CamomileLibrary
let rec repl ui stref =
LTerm_ui.wait ui >>= fun event ->
match event, State.get_typing_area !stref with
(* close program *)
| LTerm_event.Key{ code = Escape; _ }, _ ->
return ()
(* enter command *)
| LTerm_event.Key{ code = Enter; _ }, Command ->
begin
stref := match get_command_in !stref with
| None -> failwith "unused"
(* User presses enter to execute a command *)
| Some cmd_str -> Plugin.parse_command cmd_str
|> fun cmd_opt -> begin
match cmd_opt with
| Some cmd_in -> Plugin.execute_command cmd_in !stref |> update_commands
| None -> set_command_out (!stref |> update_commands) "unrecognized command"
end;
end;
LTerm_ui.draw ui;
repl ui stref
(* resize size of window *)
| LTerm_event.Resize{ rows=rows; cols=cols }, _ ->
stref := set_total_height !stref rows;
stref := set_width !stref cols;
LTerm_ui.draw ui;
repl ui stref
(* for any other event, consult plugins *)
| _ ->
stref := Plugin.respond_to_event event !stref;
LTerm_ui.draw ui;
repl ui stref
let main () =
Unix.chdir "../..";
let stref = ref empty_state in
Lazy.force LTerm.stdout
>>= fun term ->
stref := set_total_height !stref ((LTerm.size term).rows);
stref := set_width !stref (LTerm.size term).cols;
Clview.draw term stref
>>= fun ui ->
Lwt.finalize (fun () -> repl ui stref) (fun () -> LTerm_ui.quit ui)
let () = Lwt_main.run (main ())