Add FS tree zipper

This commit is contained in:
Dmitrii Kovanikov 2024-02-04 14:26:18 +00:00
parent 9fd83e2cdc
commit 64e9a9f1f9
4 changed files with 58 additions and 14 deletions

33
lib/fs.ml Normal file
View File

@ -0,0 +1,33 @@
type tree =
| File of string
| Dir of string * tree array
let file_name = function
| File path -> path
| Dir (path, _) -> path
type cursor =
{
pos: int;
files: tree array;
}
type zipper =
{
parents: cursor list;
current: cursor;
}
let go_down zipper =
let cursor = zipper.current in
let len = Array.length cursor.files in
let new_pos = (cursor.pos + 1) mod len in
let new_cursor = { cursor with pos = new_pos } in
{ zipper with current = new_cursor }
let go_up zipper =
let cursor = zipper.current in
let len = Array.length cursor.files in
let new_pos = (cursor.pos + len - 1) mod len in
let new_cursor = { cursor with pos = new_pos } in
{ zipper with current = new_cursor }

View File

@ -1,7 +1,6 @@
type code_tab =
{
pos: int ;
files: string list ;
fs: Fs.zipper ;
}
type tab =
@ -20,7 +19,17 @@ let initial_model repo: t =
repo ;
current_tab = Code;
code_tab = {
pos = 0;
files = [ "src/"; "lib/"; "README.md" ]
fs =
{
parents = [];
current = {
pos = 0;
files =
[| Dir ("src/", [||]);
Dir ("lib/", [||]);
File "README.md";
|]
}
}
};
}

View File

@ -2,18 +2,14 @@ open Minttea
let move_up (model: Model.t) = match model.current_tab with
| Code ->
let files_num = List.length model.code_tab.files in
let new_pos = (model.code_tab.pos + files_num - 1) mod files_num in
let code_tab = { model.code_tab with pos = new_pos } in
{ model with code_tab }
let fs = Fs.go_up model.code_tab.fs in
{ model with code_tab = { fs } }
| Issues | PullRequests -> model
let move_down (model: Model.t) = match model.current_tab with
| Code ->
let files_num = List.length model.code_tab.files in
let new_pos = (model.code_tab.pos + 1) mod files_num in
let code_tab = { model.code_tab with pos = new_pos } in
{ model with code_tab }
let fs = Fs.go_down model.code_tab.fs in
{ model with code_tab = { fs } }
| Issues | PullRequests -> model
let move_left model = model

View File

@ -48,6 +48,7 @@ let file_widget ~max_name_len ~selected files =
in
let hi_pos = 2 * selected + 1 in
files
|> Array.to_list
|> List.map fmt_line
|> List_extra.in_between ~sep:mid
|> (fun lines -> [top] @ lines @ [bot])
@ -59,8 +60,13 @@ let file_widget ~max_name_len ~selected files =
|> String_extra.unlines
let code_section (code_tab: Model.code_tab) =
let max_name_len = code_tab.files |> List.map String_extra.graphemes_len |> List.fold_left max 0 in
file_widget ~max_name_len ~selected:code_tab.pos code_tab.files
let cursor = code_tab.fs.current in
let files = Array.map Fs.file_name cursor.files in
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
let tab_content_section (model: Model.t) =
match model.current_tab with