Add help
This commit is contained in:
parent
f724aba407
commit
c396b3d135
3 changed files with 41 additions and 9 deletions
|
|
@ -6,5 +6,6 @@ let () =
|
||||||
|
|
||||||
let s = State.create Sys.argv.(1) in
|
let s = State.create Sys.argv.(1) in
|
||||||
Printf.printf "Loaded %d lines\n" (State.size s);
|
Printf.printf "Loaded %d lines\n" (State.size s);
|
||||||
|
Repl.help ();
|
||||||
|
|
||||||
Repl.loop s
|
Repl.loop s
|
||||||
|
|
|
||||||
46
lib/repl.ml
46
lib/repl.ml
|
|
@ -1,15 +1,43 @@
|
||||||
|
type command = { description : string; run : State.t -> State.t }
|
||||||
|
|
||||||
|
module Cmd = Map.Make (String)
|
||||||
|
|
||||||
|
let commands =
|
||||||
|
[
|
||||||
|
( "next",
|
||||||
|
{ description = "Move cursor to the next log line"; run = State.next } );
|
||||||
|
( "prev",
|
||||||
|
{ description = "Move cursor to the previous line"; run = State.prev } );
|
||||||
|
( "show",
|
||||||
|
{
|
||||||
|
description = "Display the current log line";
|
||||||
|
run =
|
||||||
|
(fun s ->
|
||||||
|
Printf.printf "[%d]: %s\n" (State.cursor s) (State.show_line s);
|
||||||
|
s);
|
||||||
|
} );
|
||||||
|
]
|
||||||
|
|> List.to_seq |> Cmd.of_seq
|
||||||
|
|
||||||
|
let help () =
|
||||||
|
Printf.printf "Available commands:\n";
|
||||||
|
Cmd.iter
|
||||||
|
(fun cmd args -> Printf.printf " %-11s %s\n" cmd args.description)
|
||||||
|
commands;
|
||||||
|
print_endline " exit, quit Exit the inspector"
|
||||||
|
|
||||||
let rec loop state =
|
let rec loop state =
|
||||||
print_string "inspector> ";
|
print_string "inspector> ";
|
||||||
flush stdout;
|
flush stdout;
|
||||||
try
|
try
|
||||||
match read_line () |> String.lowercase_ascii with
|
let cmd = read_line () |> String.lowercase_ascii in
|
||||||
|
match cmd with
|
||||||
|
| "" -> loop state
|
||||||
| "exit" | "quit" -> print_endline "bye"
|
| "exit" | "quit" -> print_endline "bye"
|
||||||
| "next" -> loop (State.next state)
|
| _ -> (
|
||||||
| "prev" -> loop (State.prev state)
|
match Cmd.find_opt cmd commands with
|
||||||
| "show" ->
|
| Some c -> loop (c.run state)
|
||||||
Printf.printf "[%d]: %s\n" (State.cursor state) (State.show_line state);
|
| None ->
|
||||||
loop state
|
Printf.printf "Unknown command: %s\n" cmd;
|
||||||
| str ->
|
loop state)
|
||||||
print_endline str;
|
|
||||||
loop state
|
|
||||||
with End_of_file -> print_endline "bye"
|
with End_of_file -> print_endline "bye"
|
||||||
|
|
|
||||||
|
|
@ -19,3 +19,6 @@ val loop : State.t -> unit
|
||||||
the loop.
|
the loop.
|
||||||
|
|
||||||
The function runs until the user exits the program. *)
|
The function runs until the user exits the program. *)
|
||||||
|
|
||||||
|
val help : unit -> unit
|
||||||
|
(** [help] print all commands available in the repl. *)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue