From e6215030cfc9076f8c332beee537cb3c6a0aaeda Mon Sep 17 00:00:00 2001 From: Dmitrii Kovanikov Date: Sun, 4 Feb 2024 16:18:52 +0000 Subject: [PATCH] Use styled library in main view --- lib/tui/view.ml | 50 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/tui/view.ml b/lib/tui/view.ml index 2846646..0014562 100644 --- a/lib/tui/view.ml +++ b/lib/tui/view.ml @@ -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 \ No newline at end of file + content; + empty; + ] + +let view (model: Model.t) = + model |> to_doc |> Pretty.render