Use styled library in main view

This commit is contained in:
Dmitrii Kovanikov 2024-02-04 16:18:52 +00:00
parent 65e041b606
commit e6215030cf

View File

@ -6,7 +6,7 @@ let style_selected =
let tabs_section cur_tab =
let open Pretty in
let p_tab tab txt =
let p_tab tab txt =
if cur_tab = tab
then fmt style_selected txt
else str txt
@ -18,7 +18,7 @@ let tabs_section cur_tab =
str "";
]
in
render @@ row
row
[ col
[
p_tab Model.Code "╭──────╮";
@ -42,15 +42,23 @@ let tabs_section cur_tab =
]
let file_widget ~max_name_len ~selected files =
let open Pretty in
(* Add two spaces for padding before and end of the file name *)
let max_len = max_name_len + 4 in
(* Frame *)
let top = "" ^ String_extra.repeat_txt (max_len - 2) "" ^ "" in
let mid = "" ^ String_extra.repeat_txt (max_len - 2) "" ^ "" in
let bot = "" ^ String_extra.repeat_txt (max_len - 2) "" ^ "" in
let fmt_line line =
(* Line *)
let fmt_line line =
"" ^ String_extra.fill_right max_name_len line ^ ""
in
let hi_pos = 2 * selected + 1 in
(* Combine *)
files
|> Array.to_list
|> List.map fmt_line
@ -58,16 +66,16 @@ let file_widget ~max_name_len ~selected files =
|> (fun lines -> [top] @ lines @ [bot])
|> List.mapi (fun i s ->
if i = hi_pos - 1 || i = hi_pos || i = hi_pos + 1
then ANSITerminal.sprintf style_selected "%s" s
else s
then fmt style_selected s
else str s
)
|> String_extra.unlines
|> col
let code_section (code_tab: Model.code_tab) =
let cursor = code_tab.fs.current in
let files = Array.map Fs.file_name cursor.files in
let max_name_len =
files
let max_name_len =
files
|> Array.map String_extra.graphemes_len
|> Array.fold_left max 0 in
file_widget ~max_name_len ~selected:cursor.pos files
@ -75,17 +83,27 @@ let code_section (code_tab: Model.code_tab) =
let tab_content_section (model: Model.t) =
match model.current_tab with
| Code -> code_section model.code_tab
| Issues | PullRequests -> ""
| Issues | PullRequests -> Pretty.str ""
let view (model: Model.t) =
let repo = ANSITerminal.sprintf style_repo "%s" model.repo in
let to_doc (model: Model.t) =
let open Pretty in
let empty = str "" in
let repo = fmt style_repo model.repo in
let tabs = tabs_section model.current_tab in
let content = tab_content_section model in
Format.sprintf
{|%s
%s
col
[
repo;
empty;
%s
tabs;
empty;
|} repo tabs content
content;
empty;
]
let view (model: Model.t) =
model |> to_doc |> Pretty.render