From 96b3fc6c523c3375983e06bfd38ab1cc10e2b984 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Fri, 13 Mar 2026 20:56:39 +0100 Subject: [PATCH] Add next, prev and show commands --- bin/inspector.ml | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/bin/inspector.ml b/bin/inspector.ml index 3a7dbe0..f11b17e 100644 --- a/bin/inspector.ml +++ b/bin/inspector.ml @@ -1,10 +1,40 @@ -let load_lines filename = - In_channel.with_open_text filename In_channel.input_lines |> Array.of_list +module State = struct + 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 () = if Array.length Sys.argv < 2 then ( Printf.eprintf "Usage: %s \n" (Filename.basename Sys.argv.(0)); exit 1); - let contents = load_lines Sys.argv.(1) in - Printf.printf "Loaded %d lines\n" (Array.length contents) + let s = State.create Sys.argv.(1) in + Printf.printf "Loaded %d lines\n" s.size; + + State.repl s