From ba61dfd69504ec6263a9dee9931d93adeb6f3142 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Mon, 7 Jul 2025 21:52:08 +0200 Subject: Initialize repository --- bin/repl.ml | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 bin/repl.ml (limited to 'bin/repl.ml') 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 @@ +open Core +open Option.Let_syntax + +let ok = ref true +let opts = Settings.opts + +let rec user_input cb = + let prompt = (if !ok then "[okay]" else "[fail]") ^ " (mini)nix> " in + try + match LNoise.linenoise prompt with + | None -> () + | Some v -> + cb v; + user_input cb + with Sys_unix.Break -> + printf "\n%!"; + user_input cb + +let split_cmd_prefix cmd = + let%bind cmd = String.chop_prefix ~prefix:":" cmd in + let cmd' = Repl_cmd.lstrip_space cmd in + let space = String.chop_suffix_exn cmd ~suffix:cmd' in + return (":" ^ space, cmd') + +let handle_cmd cmd = + let cmd = Repl_cmd.strip_space cmd in + (match split_cmd_prefix cmd with + | Some (_, cmd) -> ok := Repl_cmd.invoke cmd + | None -> + if String.(strip cmd <> "") then + ok := Run.eval_expr cmd ~origin:Interactive); + printf "\n%!" + +let run () = + LNoise.set_multiline true; + LNoise.history_load ~filename:"mininix_history" |> ignore; + LNoise.history_set ~max_length:500 |> ignore; + LNoise.set_hints_callback (fun line -> + let%bind _, cmd = split_cmd_prefix line in + let%bind hint = Repl_cmd.hint cmd in + return (hint, LNoise.Yellow, true)); + LNoise.set_completion_callback (fun line_so_far completions -> + match split_cmd_prefix line_so_far with + | Some (prefix, cmd_so_far) -> + Repl_cmd.complete cmd_so_far + |> List.map ~f:(String.append prefix) + |> List.iter ~f:(LNoise.add_completion completions) + | None -> ()); + user_input (fun from_user -> + LNoise.history_add from_user |> ignore; + LNoise.history_save ~filename:"mininix_history" |> ignore; + handle_cmd from_user) -- cgit v1.2.3