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
open Core
open Import
module View_spec = struct
type t =
{ key : string -> Vdom.Node.t
; plain_text : string -> Vdom.Node.t
}
let plain =
let open Vdom in
{ key = Node.text; plain_text = Node.text }
;;
let with_classes ~key_class ~plain_text_class =
let text_span class_ text =
let open Vdom in
Node.span ~attr:(Attr.class_ class_) [ Node.text text ]
in
{ key = text_span key_class; plain_text = text_span plain_text_class }
;;
end
module Command = struct
type t =
{ keys : Keystroke.t list
; description : string
}
[@@deriving sexp, compare]
module Format = struct
type t =
[ `Keys of [ `Sep of string ]
| `Description of (string -> string) option
| `Text of string
]
list
let default =
[ `Text "Press "
; `Keys (`Sep " or ")
; `Text " to "
; `Description (Some String.uncapitalize)
; `Text "."
]
;;
end
let view_keys t (view_spec : View_spec.t) ~sep =
let keys =
t.keys
|> List.map ~f:Keystroke.to_string_hum
|> List.dedup_and_sort ~compare:String.compare
|> List.map ~f:view_spec.key
in
List.intersperse keys ~sep:(view_spec.plain_text sep)
;;
let view_description ?(f = Fn.id) t (view_spec : View_spec.t) =
view_spec.plain_text (f t.description)
;;
let view t view_spec format =
let open Vdom in
Node.div
(List.concat_map format ~f:(function
| `Keys (`Sep sep) -> view_keys t view_spec ~sep
| `Description f -> [ view_description ?f t view_spec ]
| `Text text -> [ view_spec.plain_text text ]))
;;
end
type t = Command.t list [@@deriving sexp, compare]
let empty = []
let is_empty = List.is_empty
let of_command_list = Fn.id
let commands = Fn.id
let add_command t command = t @ [ command ]
let view_rows ?(sep = " or ") t (view_spec : View_spec.t) =
let open Vdom in
let align how = Css_gen.(text_align how) |> Attr.style in
List.map (commands t) ~f:(fun command ->
Node.tr
[ Node.td
~attr:(align `Right)
(Command.view_keys command view_spec ~sep @ [ view_spec.plain_text " : " ])
; Node.td ~attr:(align `Left) [ Command.view_description command view_spec ]
])
;;
let view ?sep t view_spec = Vdom.Node.table (view_rows ?sep t view_spec)