Rename doc elements, change to a list

This commit is contained in:
Dmitrii Kovanikov 2024-04-07 10:50:23 +01:00
parent 77fac2dbe6
commit 9ce06264bd
4 changed files with 27 additions and 29 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 ]