module Common = Common open Notty type state = { go_back : unit -> unit } let render_info = let title = I.strf ~attr:A.(st bold) "%s" "Stitch - About" |> I.pad ~l:0 ~t:0 in let description = I.strf ~attr:A.(st bold) "%s" "Minimal Note Composer. Run stitch --help for more info." |> I.pad ~l:2 ~t:2 in let license = I.strf "%s" "Made by Marc Coquand (https://mccd.space). Licensed under EUPL-1.2." |> I.pad ~l:2 ~t:4 in let open I in 5, title description license let general_help_menu = [ "Toggle this menu", "?" ; "Exit", "Ctrl-c, q, Esc" ; "Down", "Ctrl-n, j" ; "Up", "Ctrl-p, k" ; "Regexp Search", "s" ; "Note View", "1" ; "Todo View", "2" ; "Done View", "3" ; "Edit File in $EDITOR", "Enter, e" ] let pad str n = let padding = n - String.length str in String.concat "" [ str; String.make padding ' ' ] let render_menu ~menu ~title ~start_y = let keybindings = I.strf ~attr:A.(st bold) "%s" title |> I.pad ~l:2 ~t:start_y in let elements = List.mapi (fun i (explanation, keybinding) -> let padding = pad explanation 27 in I.strf "%s%s" padding keybinding |> I.pad ~l:2 ~t:(i + start_y + 1)) menu in let open I in 1 + List.length elements, List.fold_left (fun sum el -> el sum) keybindings elements let note_view_menu = [ "Toggle Full-View Mode", "f" ] let todo_view_menu = [ "Toggle Done", "Ctrl-t"; "Refresh", "g" ] let done_view_menu = [ "Toggle Todo", "Ctrl-t"; "Refresh", "g" ] let rec render t ({ go_back } as state) = let img = let open I in let info_length, info_img = render_info in let general_length, general_img = render_menu ~menu:general_help_menu ~title:"Keybindings (General)" ~start_y:(info_length + 1) in let note_length, note_img = render_menu ~menu:note_view_menu ~title:"Note View" ~start_y:(general_length + info_length + 2) in let todo_length, todo_img = render_menu ~menu:todo_view_menu ~title:"Todo View" ~start_y:(note_length + general_length + info_length + 3) in let _, done_img = render_menu ~menu:done_view_menu ~title:"Done View" ~start_y:(todo_length + note_length + general_length + info_length + 4) in info_img general_img note_img todo_img done_img in Common.Term.image t img; (* TODO: Add controls for scroll *) match Common.Term.event t with | `End | `Key (`Escape, []) | `Key (`ASCII 'q', []) | `Key (`ASCII 'C', [ `Ctrl ]) -> () | `Resize _ -> render t state | `Key (`ASCII '?', []) -> go_back () | _ -> render t state