Add files list view

This commit is contained in:
Dmitrii Kovanikov 2024-01-29 19:05:08 +00:00
parent 1a7618a5c5
commit 4c576beffa
5 changed files with 46 additions and 10 deletions

8
lib/list_extra.ml Normal file
View File

@ -0,0 +1,8 @@
let in_between ~sep list =
let rec loop = function
| [] -> []
| x :: xs -> sep :: x :: loop xs
in
match list with
| [] | [_] -> list
| x :: xs -> x :: loop xs

View File

@ -9,14 +9,8 @@ type doc =
let (---) t b = Vertical (t, b)
let (<|>) l r = Horizontal (l, r)
let graphemes_len =
Uuseg_string.fold_utf_8 `Grapheme_cluster (fun len _ -> len + 1) 0
let fill_right (n : int) (s : string) : string =
s ^ String.concat "" (List.init (n - graphemes_len s) (fun _ -> ""))
let zip_lines l r =
let max_len_l = List.map graphemes_len l |> List.fold_left max 0 in
let max_len_l = List.map String_extra.graphemes_len l |> List.fold_left max 0 in
let padding = String.make max_len_l ' ' in
let rec zip l r =
match (l, r) with
@ -25,7 +19,7 @@ let zip_lines l r =
| ([], r) ->
List.map (fun s -> padding ^ s) r
| (hd_l :: tl_l, hd_r :: tl_r) ->
(fill_right max_len_l hd_l ^ hd_r) :: zip tl_l tl_r
(String_extra.fill_right max_len_l hd_l ^ hd_r) :: zip tl_l tl_r
in
zip l r
@ -42,4 +36,4 @@ let rec render_to_lines = function
let render doc =
doc
|> render_to_lines
|> String.concat "\n"
|> String_extra.unlines

10
lib/string_extra.ml Normal file
View File

@ -0,0 +1,10 @@
let unlines : string list -> string = String.concat "\n"
let graphemes_len =
Uuseg_string.fold_utf_8 `Grapheme_cluster (fun len _ -> len + 1) 0
let repeat_txt n txt =
String.concat "" (List.init n (fun _ -> txt))
let fill_right (n : int) (s : string) : string =
s ^ repeat_txt (n - graphemes_len s) " "

View File

@ -6,10 +6,12 @@ type tab =
type t =
{ repo: string ;
tab: tab ;
files: string list ;
}
let initial_model repo: t =
{
repo ;
tab = Code;
files = [ "src/"; "lib/"; "README.md" ]
}

View File

@ -37,12 +37,34 @@ let tab_section cur_tab =
)
)
let file_widget ~max_name_len files =
(* Add two spaces for padding before and end of the file name *)
let max_len = max_name_len + 4 in
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 =
"" ^ String_extra.fill_right max_name_len line ^ ""
in
files
|> List.map fmt_line
|> List_extra.in_between ~sep:mid
|> (fun lines -> [top] @ lines @ [bot])
|> String_extra.unlines
let files_section files =
let max_name_len = files |> List.map String_extra.graphemes_len |> List.fold_left max 0 in
file_widget ~max_name_len files
let view (model: Model.t) =
let repo = fmt_repo model.repo in
let tabs = tab_section model.tab in
let files = files_section model.files in
Format.sprintf
{|%s
%s
|} repo tabs
%s
|} repo tabs files