aboutsummaryrefslogtreecommitdiffstats
path: root/bin/repl.ml
diff options
context:
space:
mode:
Diffstat (limited to 'bin/repl.ml')
-rw-r--r--bin/repl.ml52
1 files changed, 52 insertions, 0 deletions
diff --git a/bin/repl.ml b/bin/repl.ml
new file mode 100644
index 0000000..092c503
--- /dev/null
+++ b/bin/repl.ml
@@ -0,0 +1,52 @@
1open Core
2open Option.Let_syntax
3
4let ok = ref true
5let opts = Settings.opts
6
7let rec user_input cb =
8 let prompt = (if !ok then "[okay]" else "[fail]") ^ " (mini)nix> " in
9 try
10 match LNoise.linenoise prompt with
11 | None -> ()
12 | Some v ->
13 cb v;
14 user_input cb
15 with Sys_unix.Break ->
16 printf "\n%!";
17 user_input cb
18
19let split_cmd_prefix cmd =
20 let%bind cmd = String.chop_prefix ~prefix:":" cmd in
21 let cmd' = Repl_cmd.lstrip_space cmd in
22 let space = String.chop_suffix_exn cmd ~suffix:cmd' in
23 return (":" ^ space, cmd')
24
25let handle_cmd cmd =
26 let cmd = Repl_cmd.strip_space cmd in
27 (match split_cmd_prefix cmd with
28 | Some (_, cmd) -> ok := Repl_cmd.invoke cmd
29 | None ->
30 if String.(strip cmd <> "") then
31 ok := Run.eval_expr cmd ~origin:Interactive);
32 printf "\n%!"
33
34let run () =
35 LNoise.set_multiline true;
36 LNoise.history_load ~filename:"mininix_history" |> ignore;
37 LNoise.history_set ~max_length:500 |> ignore;
38 LNoise.set_hints_callback (fun line ->
39 let%bind _, cmd = split_cmd_prefix line in
40 let%bind hint = Repl_cmd.hint cmd in
41 return (hint, LNoise.Yellow, true));
42 LNoise.set_completion_callback (fun line_so_far completions ->
43 match split_cmd_prefix line_so_far with
44 | Some (prefix, cmd_so_far) ->
45 Repl_cmd.complete cmd_so_far
46 |> List.map ~f:(String.append prefix)
47 |> List.iter ~f:(LNoise.add_completion completions)
48 | None -> ());
49 user_input (fun from_user ->
50 LNoise.history_add from_user |> ignore;
51 LNoise.history_save ~filename:"mininix_history" |> ignore;
52 handle_cmd from_user)