Add next, prev and show commands
This commit is contained in:
parent
e1cc1d3858
commit
96b3fc6c52
1 changed files with 34 additions and 4 deletions
|
|
@ -1,10 +1,40 @@
|
||||||
let load_lines filename =
|
module State = struct
|
||||||
In_channel.with_open_text filename In_channel.input_lines |> Array.of_list
|
type t = { lines : string array; cursor : int; size : int }
|
||||||
|
|
||||||
|
let create (fname : string) : t =
|
||||||
|
let lines =
|
||||||
|
In_channel.with_open_text fname In_channel.input_lines |> Array.of_list
|
||||||
|
in
|
||||||
|
{ lines; cursor = 0; size = Array.length lines }
|
||||||
|
|
||||||
|
let show_line s : string = s.lines.(s.cursor)
|
||||||
|
|
||||||
|
let rec repl s =
|
||||||
|
print_string "inspector> ";
|
||||||
|
flush stdout;
|
||||||
|
try
|
||||||
|
match read_line () |> String.lowercase_ascii with
|
||||||
|
| "exit" | "quit" -> print_endline "bye"
|
||||||
|
| "next" ->
|
||||||
|
if s.cursor < s.size - 1 then repl { s with cursor = s.cursor + 1 }
|
||||||
|
else repl s
|
||||||
|
| "prev" ->
|
||||||
|
if s.cursor > 0 then repl { s with cursor = s.cursor - 1 } else repl s
|
||||||
|
| "show" ->
|
||||||
|
Printf.printf "[%d]: %s\n" s.cursor (show_line s);
|
||||||
|
repl s
|
||||||
|
| str ->
|
||||||
|
print_endline str;
|
||||||
|
repl s
|
||||||
|
with End_of_file -> print_endline "bye"
|
||||||
|
end
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
if Array.length Sys.argv < 2 then (
|
if Array.length Sys.argv < 2 then (
|
||||||
Printf.eprintf "Usage: %s <logfile>\n" (Filename.basename Sys.argv.(0));
|
Printf.eprintf "Usage: %s <logfile>\n" (Filename.basename Sys.argv.(0));
|
||||||
exit 1);
|
exit 1);
|
||||||
|
|
||||||
let contents = load_lines Sys.argv.(1) in
|
let s = State.create Sys.argv.(1) in
|
||||||
Printf.printf "Loaded %d lines\n" (Array.length contents)
|
Printf.printf "Loaded %d lines\n" s.size;
|
||||||
|
|
||||||
|
State.repl s
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue