aboutsummaryrefslogtreecommitdiff
path: root/lib/help_screen.ml
blob: 1995fafa784840311958355dc5376ce11e720c3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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 BSD-3."
    |> 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