module Grep = Grep module Common = Common open Notty module Help_screen = Help_screen type state = { pos : int * int ; scroll : int ; content : (string * string) array ; content_pretty : string array ; goto_headlines : (unit -> unit) -> unit ; goto_done : (unit -> unit) -> unit ; output : string option ; tag : string ; hide_file_name : bool } let title = I.strf ~attr:A.(st bold) "%s" "Todo" |> I.pad ~l:0 ~t:0 let content_start = 2 let init ~goto_done ~goto_headlines = let content = Grep.get_todos () |> Grep.parse_todo_string in let content_pretty = Grep.pretty_format_todo content in { pos = 0, content_start ; scroll = 0 ; content = content |> Array.of_list ; content_pretty = content_pretty |> Array.of_list ; goto_headlines ; goto_done ; output = None ; tag = "" ; hide_file_name = false } let load_todos ?(hide_file_name = false) () = let todo_content = Grep.get_todos () |> Grep.parse_todo_string in let todo_pretty = Grep.pretty_format_todo ~hide_file_name todo_content in todo_content, todo_pretty let rec render t ({ pos ; scroll ; content ; content_pretty ; goto_headlines ; goto_done ; output ; hide_file_name ; _ } as state) = let x, y = pos in let size_x, size_y = Common.Term.size t in let content_position = y - content_start 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 in let dot = if Array.length content_pretty = 0 then I.empty else I.string A.(st bold) ">" |> I.pad ~l:0 ~t:(y - scroll) and elements = Array.mapi (fun i el -> if i == y - scroll then I.strf "%s" el |> I.pad ~l:2 ~t:(i + content_start) else I.strf "%s" el |> I.pad ~l:2 ~t:(i + content_start)) (content_pretty |> Basic.array_drop scroll) in let open I in Array.fold_left (fun sum el -> el sum) (title dot output_info) elements in Common.Term.image t img; let content_end = Array.length content_pretty + (content_start - 1) 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_end; 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 | `Mouse ((`Press _ | `Drag), (_, y), _) -> render t { state with pos = 0, min y content_end } | `Key (`ASCII '?', []) -> Help_screen.render t { go_back = (fun () -> render t state) } | `Key (`ASCII '1', []) -> goto_headlines (fun () -> render t state) | `Key (`ASCII '3', []) -> goto_done (fun () -> render t state) | `Key (`ASCII 'g', []) -> let content, content_pretty = load_todos ~hide_file_name () in let y = min (List.length content_pretty + content_start) y in render t { state with pos = x, y ; content = content |> Array.of_list ; content_pretty = Array.of_list content_pretty } | `Key (`ASCII 's', []) -> let (input_state : Input_prompt.state) = { screen = img ; user_input = "" ; prompt = "REGEXP" ; on_enter = (fun tag -> let content = Grep.get_tagged_todo tag () |> Grep.parse_todo_string in let content_pretty = Grep.pretty_format_todo content in Common.Term.cursor t None; render t { state with content = content |> Array.of_list ; content_pretty = content_pretty |> Array.of_list ; tag }) ; on_cancel = (fun _ -> Common.Term.cursor t None; render t state) } in Input_prompt.render t input_state | `Key (`ASCII 'j', []) | `Key (`ASCII 'N', [ `Ctrl ]) -> scroll_down () | `Key (`ASCII 'k', []) | `Key (`ASCII 'P', [ `Ctrl ]) -> scroll_up () | `Key (`ASCII '!', []) -> let selected_file, content = Array.get content content_position 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 Arbitrary_command.run t ~command:(Scanf.unescaped command) ~content ~selected_file ~on_return:(fun result -> let content, content_pretty = load_todos ~hide_file_name () in let y = min (List.length content_pretty + content_start) y in Common.Term.cursor t None; render t { state with content = Array.of_list content ; content_pretty = Array.of_list content_pretty ; pos = 0, y ; 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 'h', []) -> let hide_file_name = not hide_file_name in let content_pretty = Grep.pretty_format_todo ~hide_file_name (content |> Array.to_list) |> Array.of_list in render t { state with hide_file_name; content_pretty } | `Key (`ASCII 'T', [ `Ctrl ]) -> let selected_file, _ = Array.get content (y - content_start) in let _ = Grep.toggle_done selected_file in let content, content_pretty = load_todos ~hide_file_name () in let y = min (List.length content_pretty + content_start) y in render t { state with pos = x, y ; content = content |> Array.of_list ; content_pretty = Array.of_list content_pretty } | `Key (`Arrow d, _) -> (match d with | `Up -> scroll_up () | `Down -> scroll_down () | _ -> 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 selected_file, _ = Array.get content content_position in let full_path_file = Grep.execution_directory ^ "/" ^ selected_file in let full_args = Array.append (Array.of_list args) [| "+1"; 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 () | _ -> render t state