From 9ce06264bd287299d13f8c1ee53ca3dc27f3e66b Mon Sep 17 00:00:00 2001 From: Dmitrii Kovanikov Date: Sun, 7 Apr 2024 10:50:23 +0100 Subject: [PATCH] Rename doc elements, change to a list --- lib/pretty.ml | 28 +++++++++++++--------------- lib/pretty.mli | 4 ++-- lib/tui/view.ml | 8 ++++---- lib/tui/widget.ml | 16 ++++++++-------- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/lib/pretty.ml b/lib/pretty.ml index 2d76e5d..7ecc6d8 100644 --- a/lib/pretty.ml +++ b/lib/pretty.ml @@ -3,21 +3,14 @@ type styles = ANSITerminal.style list type doc = - | Empty | Str of styles * string - | Vertical of doc * doc - | Horizontal of doc * doc + | Vertical of doc list + | Horizontal of doc list let str string = Str ([], string) let fmt styles string = Str (styles, string) - -let row = function - | [] -> Empty - | hd :: tl -> List.fold_left (fun l r -> Horizontal (l, r)) hd tl - -let col = function - | [] -> Empty - | hd :: tl -> List.fold_left (fun l r -> Vertical (l, r)) hd tl +let horizontal cols = Horizontal cols +let vertical rows = Vertical rows type chunk = { styles : styles; @@ -69,11 +62,16 @@ let zip_lines (l : line list) (r : line list) = zip l r let rec render_to_lines = function - | Empty -> [] | Str (styles, string) -> [ { chunks = [ { styles; string } ] } ] - | Vertical (top, bottom) -> render_to_lines top @ render_to_lines bottom - | Horizontal (left, right) -> - zip_lines (render_to_lines left) (render_to_lines right) + | Vertical rows -> List.concat_map render_to_lines rows + | Horizontal cols -> ( + match cols with + | [] -> [] + (* TODO: This is potentially really slow; optimise *) + | hd :: tl -> + List.fold_left + (fun acc col -> zip_lines acc (render_to_lines col)) + (render_to_lines hd) tl) let render doc = doc |> render_to_lines |> List.map fmt_line |> String_extra.unlines diff --git a/lib/pretty.mli b/lib/pretty.mli index d78c616..a15eac1 100644 --- a/lib/pretty.mli +++ b/lib/pretty.mli @@ -11,10 +11,10 @@ val str : string -> doc val fmt : styles -> string -> doc (** Put all documents in a list horizontally, automatically adding required padding. *) -val row : doc list -> doc +val horizontal : doc list -> doc (** Put all documents in a list vertically after each other with a line separator. *) -val col : doc list -> doc +val vertical : doc list -> doc (** Render the resulting document. *) val render : doc -> string diff --git a/lib/tui/view.ml b/lib/tui/view.ml index b328433..314b99d 100644 --- a/lib/tui/view.ml +++ b/lib/tui/view.ml @@ -10,8 +10,8 @@ let debug_section (model : Model.t) = let tabs_section cur_tab = let open Pretty in - let sep = col [ str " "; str " "; str "─" ] in - row + let sep = vertical [ str " "; str " "; str "─" ] in + horizontal [ Widget.code_tab ~is_selected:(cur_tab = Model.Code); sep; @@ -25,7 +25,7 @@ let code_section (code_tab : Model.code_tab) = Widget.pwd code_tab.root_dir_path (Fs.zipper_parents code_tab.fs) in let fs_doc = Widget.fs code_tab in - Pretty.col [ current_path_doc; fs_doc ] + Pretty.vertical [ current_path_doc; fs_doc ] let tab_content_section (model : Model.t) = match model.current_tab with @@ -40,6 +40,6 @@ let to_doc (model : Model.t) = let tabs = tabs_section model.current_tab in let content = tab_content_section model in - col [ row [ repo; str " "; debug ]; empty; tabs; content; empty ] + vertical [ horizontal [ repo; str " "; debug ]; empty; tabs; content; empty ] let view (model : Model.t) = model |> to_doc |> Pretty.render diff --git a/lib/tui/widget.ml b/lib/tui/widget.ml index d84c573..32fd53a 100644 --- a/lib/tui/widget.ml +++ b/lib/tui/widget.ml @@ -4,7 +4,7 @@ let style_directory = ANSITerminal.[ Bold; magenta ] let tab_doc ~is_selected tab_lines = let open Pretty in let format = if is_selected then fmt style_selected else str in - tab_lines |> List.map format |> col + tab_lines |> List.map format |> vertical let code_tab ~is_selected = tab_doc ~is_selected @@ -48,9 +48,9 @@ let file_contents_to_doc ~file_name:_ ~file_contents = |> String.split_on_char '\n' |> List_extra.take 30 |> List.map Pretty.str - |> Pretty.col + |> Pretty.vertical in - Pretty.(row [ str " "; file_contents_preview ]) + Pretty.(horizontal [ str " "; file_contents_preview ]) (* Extra padding for: @@ -102,7 +102,7 @@ let current_level_to_doc (cursor : Fs.cursor) ~has_next = if i = hi_pos - 1 || i = hi_pos || i = hi_pos + 1 then fmt style_selected s else str s) - |> col + |> vertical let children_to_doc ~prev_total ~pos children = let open Pretty in @@ -121,7 +121,7 @@ let children_to_doc ~prev_total ~pos children = List_extra.generate prev_rows_count (fun i -> let is_current_pos = i = connect_pos in if is_current_pos then str "─" else str " ") - |> col + |> vertical in (* Formatting single file name *) @@ -150,10 +150,10 @@ let children_to_doc ~prev_total ~pos children = in pad_before @ lines) |> List.map str - |> col + |> vertical in - row [ connector_doc; files_doc ] + horizontal [ connector_doc; files_doc ] type next_level = | Empty_directory @@ -191,4 +191,4 @@ let fs (code_tab : Model.code_tab) = match next_level_doc with | Empty_directory -> current_level_doc | Directory_contents next_level_doc | File_contents next_level_doc -> - Pretty.row [ current_level_doc; next_level_doc ] + Pretty.horizontal [ current_level_doc; next_level_doc ]