Skip to content

Commit

Permalink
Upgraded key binding info
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenldl committed Apr 30, 2023
1 parent b7d571d commit 57c5d3d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 56 deletions.
57 changes: 35 additions & 22 deletions src/multi_file_view.ml
Original file line number Diff line number Diff line change
Expand Up @@ -248,30 +248,43 @@ module Bottom_pane = struct

module Key_binding_info = struct
let grid_contents : Ui_base.Key_binding_info.grid_contents =
let open Ui_base.Key_binding_info in
let navigate_grid =
[
[
{ label = "Enter"; msg = "open document" };
{ label = "/"; msg = "switch to search mode" };
{ label = "x"; msg = "clear search" };
];
[
{ label = "Tab";
msg = "switch to single file view" };
{ label = "q"; msg = "exit" };
];
]
in
let search_grid =
[
[
{ label = "Enter"; msg = "confirm and exit search mode" };
];
[
{ label = ""; msg = "" };
];
]
in
[
(Navigate,
[
[
{ key = "Enter"; msg = "open document" };
{ key = "/"; msg = "switch to search mode" };
{ key = "x"; msg = "clear search" };
];
[
{ key = "Tab";
msg = "switch to single file view" };
{ key = "q"; msg = "exit" };
];
]
({ input_mode = Navigate; init_ui_mode = Ui_multi_file },
navigate_grid
);
({ input_mode = Navigate; init_ui_mode = Ui_single_file },
navigate_grid
);
({ input_mode = Search; init_ui_mode = Ui_multi_file },
search_grid
);
(Search,
[
[
{ key = "Enter"; msg = "confirm and exit search mode" };
];
[
{ key = ""; msg = "" };
];
]
({ input_mode = Search; init_ui_mode = Ui_single_file },
search_grid
);
]

Expand Down
44 changes: 31 additions & 13 deletions src/single_file_view.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,49 @@ module Bottom_pane = struct

module Key_binding_info = struct
let grid_contents : Ui_base.Key_binding_info.grid_contents =
let open Ui_base.Key_binding_info in
let navigate_line0 =
[
{ label = "Enter"; msg = "open document" };
{ label = "/"; msg = "switch to search mode" };
{ label = "x"; msg = "clear search" };
]
in
let search_grid =
[
[
{ label = "Enter"; msg = "confirm and exit search mode" };
];
[
{ label = ""; msg = "" };
];
]
in
[
(Navigate,
({ input_mode = Navigate; init_ui_mode = Ui_multi_file },
[
navigate_line0;
[
{ key = "Enter"; msg = "open document" };
{ key = "/"; msg = "switch to search mode" };
{ key = "x"; msg = "clear search" };
];
[
{ key = "Tab";
{ label = "Tab";
msg = "switch to multi file view" };
{ key = "q"; msg = "exit" };
{ label = "q"; msg = "exit" };
];
]
);
(Search,
({ input_mode = Navigate; init_ui_mode = Ui_single_file },
[
navigate_line0;
[
{ key = "Enter"; msg = "confirm and exit search mode" };
];
[
{ key = ""; msg = "" };
{ label = "q"; msg = "exit" };
];
]
);
({ input_mode = Search; init_ui_mode = Ui_multi_file },
search_grid
);
({ input_mode = Search; init_ui_mode = Ui_single_file },
search_grid
);
]

let grid_lookup = Ui_base.Key_binding_info.make_grid_lookup grid_contents
Expand Down
47 changes: 26 additions & 21 deletions src/ui_base.ml
Original file line number Diff line number Diff line change
Expand Up @@ -232,53 +232,58 @@ module Status_bar = struct
end

module Key_binding_info = struct
type key_msg = {
key : string;
type label_msg = {
label : string;
msg : string;
}

type key_msg_line = key_msg list
type label_msg_line = label_msg list

type grid_contents = (input_mode * (key_msg_line list)) list
type grid_key = {
input_mode : input_mode;
init_ui_mode : ui_mode;
}

type grid_contents = (grid_key * (label_msg_line list)) list

type grid_lookup = (input_mode * Nottui.ui Lwd.t) list
type grid_lookup = (grid_key * Nottui.ui Lwd.t) list

let make_grid_lookup grid_contents : grid_lookup =
let max_key_msg_len_lookup =
let max_label_msg_len_lookup =
grid_contents
|> List.map (fun (mode, grid) ->
let max_key_len, max_msg_len =
List.fold_left (fun (max_key_len, max_msg_len) row ->
List.fold_left (fun (max_key_len, max_msg_len) { key; msg } ->
(max max_key_len (String.length key),
let max_label_len, max_msg_len =
List.fold_left (fun (max_label_len, max_msg_len) row ->
List.fold_left (fun (max_label_len, max_msg_len) { label; msg } ->
(max max_label_len (String.length label),
max max_msg_len (String.length msg))
)
(max_key_len, max_msg_len)
(max_label_len, max_msg_len)
row
)
(0, 0)
grid
in
(mode, (max_key_len, max_msg_len))
(mode, (max_label_len, max_msg_len))
)
in
let key_msg_pair modes { key; msg } : Nottui.ui Lwd.t =
let (max_key_len, max_msg_len) =
List.assoc modes max_key_msg_len_lookup
let label_msg_pair modes { label; msg } : Nottui.ui Lwd.t =
let (max_label_len, max_msg_len) =
List.assoc modes max_label_msg_len_lookup
in
let key_attr = Notty.A.(fg lightyellow ++ st bold) in
let label_attr = Notty.A.(fg lightyellow ++ st bold) in
let msg_attr = Notty.A.empty in
let msg = String.capitalize_ascii msg in
let key_background = Notty.I.void max_key_len 1 in
let label_background = Notty.I.void max_label_len 1 in
let content = Notty.(I.hcat
[ I.(string key_attr key </> key_background)
[ I.(string label_attr label </> label_background)
; I.string A.empty " "
; I.string msg_attr msg
]
)
in
let full_background =
Notty.I.void (max_key_len + 2 + max_msg_len + 2) 1
Notty.I.void (max_label_len + 2 + max_msg_len + 2) 1
in
Notty.I.(content </> full_background)
|> Nottui.Ui.atom
Expand All @@ -288,7 +293,7 @@ module Key_binding_info = struct
(mode,
grid_contents
|> List.map (fun l ->
List.map (key_msg_pair mode) l
List.map (label_msg_pair mode) l
)
|> Nottui_widgets.grid
~pad:(Nottui.Gravity.make ~h:`Negative ~v:`Negative)
Expand All @@ -297,7 +302,7 @@ module Key_binding_info = struct
grid_contents

let main ~(grid_lookup : grid_lookup) ~(input_mode : input_mode) =
List.assoc input_mode grid_lookup
List.assoc { input_mode; init_ui_mode = !Vars.init_ui_mode } grid_lookup
end

module Search_bar = struct
Expand Down

0 comments on commit 57c5d3d

Please sign in to comment.