diff options
author | Marc Coquand <marc@mccd.space> | 2024-05-14 18:44:58 -0500 |
---|---|---|
committer | Marc Coquand <marc@mccd.space> | 2024-05-14 18:44:58 -0500 |
commit | a76fb32e7c5815e37b5772f15326f00ec58a322d (patch) | |
tree | 99047fecba7c7a7f47844e586266579b7a3a2a59 /lib/stitched_article.ml | |
parent | bdd2c807e52e90b340a8b09c41e7353c52080b46 (diff) | |
download | stitch-a76fb32e7c5815e37b5772f15326f00ec58a322d.tar.gz stitch-a76fb32e7c5815e37b5772f15326f00ec58a322d.tar.bz2 stitch-a76fb32e7c5815e37b5772f15326f00ec58a322d.zip |
Fix offset
Diffstat (limited to '')
-rw-r--r-- | lib/stitched_article.ml | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/lib/stitched_article.ml b/lib/stitched_article.ml index 41efa75..bbdf7f4 100644 --- a/lib/stitched_article.ml +++ b/lib/stitched_article.ml @@ -6,7 +6,8 @@ module Input_screen = Input_screen type state = { pos : int * int ; scroll : int - ; content : (string * int * string) array + ; content : (string * int * string * int) array + ; go_back : unit -> unit ; content_pretty : Grep.display_type array } @@ -26,7 +27,7 @@ let render_line size_x y scroll (el : Grep.display_type) i = else I.strf "%s" el |> I.pad ~l:0 ~t:i -let rec render t ({ pos; scroll; content_pretty; _ } as state) = +let rec render t ({ pos; scroll; content_pretty; go_back; content } as state) = let size_x, size_y = Common.Term.size t in let x, y = pos in let img = @@ -56,10 +57,42 @@ let rec render t ({ pos; scroll; content_pretty; _ } as state) = | `Down -> scroll_down () | `Up -> scroll_up ()) | `Resize _ -> render t state + | `Key (`ASCII '@', []) -> go_back () | `Mouse ((`Press _ | `Drag), (_, y), _) -> render t { state with pos = 0, min y content_length } | `Key (`ASCII 'j', []) | `Key (`ASCII 'N', [ `Ctrl ]) -> scroll_down () | `Key (`ASCII 'k', []) | `Key (`ASCII 'P', [ `Ctrl ]) -> scroll_up () + | `Key (`ASCII 'e', []) | `Key (`Enter, []) -> + (* Editor might be set with extra args, in that case we need to separate these *) + let[@warning "-8"] (editor :: args) = + String.split_on_char ' ' (Sys.getenv "EDITOR") + in + let selected_file, line_number, _, file_number_offset = Array.get content y in + let full_path_file = Grep.execution_directory ^ "/" ^ selected_file in + let line_number_arg = "+" ^ Int.to_string (line_number - file_number_offset) in + let full_args = + Array.append (Array.of_list args) [| line_number_arg; full_path_file |] + in + Common.Term.cursor t (Some (0, 0)); + let _ = + Unix.create_process_env + editor + full_args + (Unix.environment ()) + Unix.stdin + Unix.stdout + Unix.stderr + in + let rec run_editor () = + match Unix.wait () with + | _, _ -> + Common.Term.cursor t None; + render t state + (* Capture resizing events *) + | exception Unix.Unix_error (Unix.EINTR, _, _) -> run_editor () + | exception Unix.Unix_error (_, _, _) -> failwith "ERROR" + in + run_editor () | `Key (`Arrow d, _) -> (match d with | `Up -> scroll_up () |