module Grep = Grep module Common = Common open Notty module Input_prompt = Input_prompt module Help_screen = Help_screen type state = { pos : int * int ; scroll : int ; content : (string * int * string * int) array ; go_back : unit -> unit ; content_pretty : (int * Grep.display_type) array ; goto_todos_view : (unit -> unit) -> unit ; goto_done_view : (unit -> unit) -> unit ; output : string option ; tag : string } let title ~tag = match tag with | "" -> I.strf ~attr:A.(st bold) "%s" "Notes" |> I.pad ~l:0 ~t:0 | a -> I.strf ~attr:A.(st bold) "%s > %s" "Notes" a |> I.pad ~l:0 ~t:0 let content_start = 1 (* TODO: Use grep -l to filter notes by regexp and rerender those files*) let rec render t ({ output ; pos ; scroll ; content_pretty ; go_back ; content ; goto_todos_view ; goto_done_view ; tag } as state) = let size_x, size_y = Common.Term.size t in let x, y = pos in let img = let output_info = match output with | Some line -> I.strf "%s%s" (String.escaped line) (String.make size_x ' ') |> I.pad ~t:(size_y - 1) | None -> I.empty and elements = Array.mapi (fun i (_, el) -> Compontent.current_line size_x y scroll el (i + content_start)) (* TODO: Fix this ugly slow conversion *) (Array.to_seq content_pretty |> Seq.drop scroll |> Array.of_seq) in let open I in output_info Array.fold_left (fun sum el -> el sum) (title ~tag) elements in Common.Term.image t img; let content_length = Array.length content_pretty in let scroll_up () = let scroll = if y - content_start - scroll = 0 then max (scroll - 1) 0 else scroll in render t @@ { state with pos = x, max (y - 1) content_start; scroll; output = None } in let scroll_down () = let scroll = if y - scroll >= size_y - 1 then scroll + 1 else scroll in render t @@ { state with pos = x, min (y + 1) (content_length - 1); scroll; output = None } in match Common.Term.event t with | `End | `Key (`Escape, []) | `Key (`ASCII 'q', []) | `Key (`ASCII 'C', [ `Ctrl ]) -> () | `Mouse (`Press (`Scroll s), _, _) -> (match s with | `Down -> scroll_down () | `Up -> scroll_up ()) | `Resize _ -> render t state | `Key (`ASCII 'f', []) -> 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 '!', []) -> let file_number_offset, content_line = Array.get content_pretty (y - content_start) in let content_line = Grep.display_type_line content_line in let selected_file, _, _, _ = Array.get content (y - content_start - file_number_offset) in let selected_file = Grep.execution_directory ^ "/" ^ selected_file in let (input_state : Input_prompt.state) = { screen = img ; user_input = "" ; prompt = "COMMAND" ; on_enter = (fun command -> if String.equal (String.trim command) String.empty then ( Common.Term.cursor t None; render t state) else ( let content = Grep.get_file_names tag |> Array.of_list in let content = Array.map (fun file_name -> Grep.get_full_file_content_content file_name) content |> Array.to_list in let content = Grep.parse_full_content content in let content_pretty = Grep.pretty_print_parsed_content content |> Array.of_list in Arbitrary_command.run t ~command:(Scanf.unescaped command) ~content:content_line ~selected_file ~on_return:(fun result -> Common.Term.cursor t None; render t { state with content = Array.of_list content ; content_pretty ; pos = 0, content_start ; scroll = 0 ; output = Some result }))) ; on_cancel = (fun _ -> Common.Term.cursor t None; render t state) } in Input_prompt.render t input_state | `Key (`ASCII 's', []) -> let (input_state : Input_prompt.state) = { screen = img ; user_input = "" ; prompt = "REGEXP" ; on_enter = (fun tag -> try let content = Grep.get_file_names tag |> Array.of_list in let oc = open_out "/tmp/stitch-output" in Printf.fprintf oc "%s\n" (String.concat "\n" (content |> Array.to_list)); close_out oc; let content = Array.map (fun file_name -> Grep.get_full_file_content_content file_name) content |> Array.to_list in let content = Grep.parse_full_content content in let content_pretty = Grep.pretty_print_parsed_content content |> Array.of_list in Common.Term.cursor t None; render t { state with content = content |> Array.of_list ; content_pretty ; pos = 0, content_start ; scroll = 0 ; tag } with | _ -> let oc = open_out "/tmp/stitch-error" in Printf.fprintf oc "%s\n" (Printexc.get_backtrace ()); close_out oc; raise (Invalid_argument "FAILURE")) ; on_cancel = (fun _ -> Common.Term.cursor t None; render t state) } in Input_prompt.render t input_state | `Key (`ASCII '?', []) -> Help_screen.render t { go_back = (fun () -> render t state) } | `Key (`ASCII '2', []) -> goto_todos_view (fun () -> render t state) | `Key (`ASCII '3', []) -> goto_done_view (fun () -> render t state) | `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 file_number_offset, _ = Array.get content_pretty (y - content_start) in let selected_file, line_number, _, _ = Array.get content (y - content_start - file_number_offset) in let full_path_file = Grep.execution_directory ^ "/" ^ selected_file in (* Because each file title consists of two lines, we need to account for the offset it adds by removing the file_number *) let line_number_arg = "+" ^ Int.to_string line_number 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 (_, str_err, str_err_2) -> let oc = open_out "/tmp/stitch-error" in Printf.fprintf oc "%s: %s" str_err str_err_2; close_out oc; failwith "ERROR" in run_editor () | `Key (`Arrow d, _) -> (match d with | `Up -> scroll_up () | `Down -> scroll_down () | _ -> render t state) | _ -> render t state