From 6795c8b8a78b48c3ff0a2866abb561e2a6b4a84a Mon Sep 17 00:00:00 2001 From: R Loader mkII <30044659+loadre@users.noreply.github.com> Date: Tue, 4 Feb 2025 17:16:05 -0500 Subject: [PATCH 1/5] Refactor --- lib/gh/client.ml | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/gh/client.ml b/lib/gh/client.ml index bfb6b64..ce1a5c3 100644 --- a/lib/gh/client.ml +++ b/lib/gh/client.ml @@ -39,7 +39,6 @@ let query query_body = let doc_url = Util.member "documentation_url" json |> Util.to_string in - let code = code in Error (Bad_credentials { msg; doc_url; code })) let parse_response (path : string list) (parse_item : Yojson.Basic.t -> 'a) json From a083feacf2a39ffae814704108b9427391082aad Mon Sep 17 00:00:00 2001 From: R Loader mkII <30044659+loadre@users.noreply.github.com> Date: Tue, 11 Feb 2025 22:17:05 -0500 Subject: [PATCH 2/5] Refactor file tree type --- lib/fs/fs.ml | 35 ++++++++++++++++++++++------------- lib/fs/fs.mli | 11 +++++++++-- lib/tui/tui.ml | 4 ++-- lib/tui/widget/code.ml | 8 ++++---- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/lib/fs/fs.ml b/lib/fs/fs.ml index fc14d9d..2fa293a 100644 --- a/lib/fs/fs.ml +++ b/lib/fs/fs.ml @@ -1,8 +1,15 @@ module Filec = Filec type tree = - | File of string * Filec.t Lazy.t * Filec.file_type Lazy.t - | Dir of string * tree array Lazy.t + | File of { + name : string; + contents : Filec.t Lazy.t; + file_type : Filec.file_type Lazy.t; + } + | Dir of { + name : string; + children : tree array Lazy.t; + } type dir_cursor = { pos : int; @@ -15,8 +22,8 @@ type cursor = (* Extracts the file name from a tree node *) let file_name = function - | File (name, _, _) -> name - | Dir (name, _) -> name + | File { name; _ } -> name + | Dir { name; _ } -> name (* A files comparison: @@ -30,10 +37,10 @@ let order_files t1 t2 = | _, _ -> String.compare (file_name t1) (file_name t2) let rec sort_tree = function - | File (name, contents, ft) -> File (name, contents, ft) - | Dir (name, (lazy children)) -> + | File _ as f -> f + | Dir { name; children = (lazy children) } -> Array.sort order_files children; - Dir (name, lazy (Array.map sort_tree children)) + Dir { name; children = lazy (Array.map sort_tree children) } (* Recursively reads a directory tree *) let rec to_tree path = @@ -45,12 +52,14 @@ let rec to_tree path = (Sys.readdir path)) in let dirname = Filename.basename path in - Dir (dirname, children) + Dir { name = dirname; children } else File - ( Filename.basename path, - lazy (Filec.read path), - lazy (Filec.type_of_path path) ) + { + name = Filename.basename path; + contents = lazy (Filec.read path); + file_type = lazy (Filec.type_of_path path); + } let read_tree path = path |> to_tree |> sort_tree let file_at cursor = cursor.files.(cursor.pos) @@ -100,12 +109,12 @@ let go_next zipper = | Dir_cursor cursor -> ( let next = file_at cursor in match next with - | File (_name, contents, _) -> + | File { contents; _ } -> { parents = cursor :: zipper.parents; current = File_cursor (Lazy.force contents); } - | Dir (_, (lazy next)) -> + | Dir { children = (lazy next); _ } -> if Array.length next = 0 then zipper else { diff --git a/lib/fs/fs.mli b/lib/fs/fs.mli index 49d6611..426ac4a 100644 --- a/lib/fs/fs.mli +++ b/lib/fs/fs.mli @@ -4,8 +4,15 @@ module Filec = Filec (** A definition of a file tree. *) type tree = - | File of string * Filec.t Lazy.t * Filec.file_type Lazy.t - | Dir of string * tree array Lazy.t + | File of { + name : string; + contents : Filec.t Lazy.t; + file_type : Filec.file_type Lazy.t; + } + | Dir of { + name : string; + children : tree array Lazy.t; + } (** Return the name of a given tree node. *) val file_name : tree -> string diff --git a/lib/tui/tui.ml b/lib/tui/tui.ml index f962065..fdb315d 100644 --- a/lib/tui/tui.ml +++ b/lib/tui/tui.ml @@ -29,10 +29,10 @@ let read_root_tree ~root_dir_path = let tree = Fs.read_tree root_dir_path in let files = match tree with - | Fs.File (path, _, _) -> + | Fs.File { name = path; _ } -> Printf.eprintf "Given path '%s' is not a directory!" path; exit 1 - | Fs.Dir (_, files) -> files + | Fs.Dir { children = files; _ } -> files in files diff --git a/lib/tui/widget/code.ml b/lib/tui/widget/code.ml index c4581df..59e0026 100644 --- a/lib/tui/widget/code.ml +++ b/lib/tui/widget/code.ml @@ -54,11 +54,11 @@ let max_file_name_len files = let fmt_file ~max_name_len (tree : Fs.tree) = let pad = Extra.String.fill_right max_name_len in match tree with - | File (name, _, file_type) -> ( + | File { name; file_type; _ } -> ( match Lazy.force file_type with | Fs.Filec.Text -> file_char ^ " " ^ pad name | Fs.Filec.Binary -> bin_char ^ " " ^ pad name) - | Dir (name, (lazy children)) -> ( + | Dir { name; children = (lazy children) } -> ( match children with | [||] -> empty_dir_char ^ " " ^ pad name | _ -> dir_char ^ " " ^ pad name) @@ -193,8 +193,8 @@ let fs_to_view (fs : Fs.zipper) = | File_cursor contents, parent :: _ -> (parent, File_selected contents) | Dir_cursor cursor, _ -> ( match Fs.file_at cursor with - | File (_, contents, _) -> (cursor, File_selected (Lazy.force contents)) - | Dir (_, children) -> + | File { contents; _ } -> (cursor, File_selected (Lazy.force contents)) + | Dir { children; _ } -> ( cursor, Dir_selected { From 45ca2995d67411cd1ae1e0eb14cc7080a070e8d7 Mon Sep 17 00:00:00 2001 From: R Loader mkII <30044659+loadre@users.noreply.github.com> Date: Wed, 12 Feb 2025 21:40:23 -0500 Subject: [PATCH 3/5] Refactor variable names --- lib/fs/fs.ml | 4 ++-- lib/tui/tui.ml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/fs/fs.ml b/lib/fs/fs.ml index 2fa293a..53e5ec1 100644 --- a/lib/fs/fs.ml +++ b/lib/fs/fs.ml @@ -51,8 +51,8 @@ let rec to_tree path = (fun child_name -> to_tree (Filename.concat path child_name)) (Sys.readdir path)) in - let dirname = Filename.basename path in - Dir { name = dirname; children } + let name = Filename.basename path in + Dir { name; children } else File { diff --git a/lib/tui/tui.ml b/lib/tui/tui.ml index fdb315d..2eaeb0d 100644 --- a/lib/tui/tui.ml +++ b/lib/tui/tui.ml @@ -29,8 +29,8 @@ let read_root_tree ~root_dir_path = let tree = Fs.read_tree root_dir_path in let files = match tree with - | Fs.File { name = path; _ } -> - Printf.eprintf "Given path '%s' is not a directory!" path; + | Fs.File { name; _ } -> + Printf.eprintf "Given path '%s' is not a directory!" name; exit 1 | Fs.Dir { children = files; _ } -> files in From 59f3d0ecbd0ee86e178d6aab39ccb4e52297028b Mon Sep 17 00:00:00 2001 From: R Loader mkII <30044659+loadre@users.noreply.github.com> Date: Wed, 12 Feb 2025 21:40:57 -0500 Subject: [PATCH 4/5] Add motivation for storing file type and contents separately --- lib/fs/fs.mli | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/fs/fs.mli b/lib/fs/fs.mli index 426ac4a..c957b70 100644 --- a/lib/fs/fs.mli +++ b/lib/fs/fs.mli @@ -2,6 +2,9 @@ and current offsets. *) module Filec = Filec +(* NOTE: contents and file_type are stored separately so we can know the file + type and assign a proper icon without reading the contents *) + (** A definition of a file tree. *) type tree = | File of { From 18e735a79efecaf8498e81f3758f9eb0d0c2d336 Mon Sep 17 00:00:00 2001 From: R Loader mkII <30044659+loadre@users.noreply.github.com> Date: Wed, 12 Feb 2025 21:59:14 -0500 Subject: [PATCH 5/5] Resolve merge conflict --- lib/tui/widget/code.ml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/tui/widget/code.ml b/lib/tui/widget/code.ml index bad41bd..f5dddf2 100644 --- a/lib/tui/widget/code.ml +++ b/lib/tui/widget/code.ml @@ -52,15 +52,9 @@ let fmt_file ~max_name_len (tree : Fs.tree) = match tree with | File { name; file_type; _ } -> ( match Lazy.force file_type with -<<<<<<< HEAD - | Fs.Filec.Text -> file_char ^ " " ^ pad name - | Fs.Filec.Binary -> bin_char ^ " " ^ pad name) - | Dir { name; children = (lazy children) } -> ( -======= | Fs.Filec.Text -> Pretty.Icon.file_char ^ " " ^ pad name | Fs.Filec.Binary -> Pretty.Icon.bin_char ^ " " ^ pad name) - | Dir (name, (lazy children)) -> ( ->>>>>>> upstream/main + | Dir { name; children = (lazy children) } -> ( match children with | [||] -> Pretty.Icon.empty_dir_char ^ " " ^ pad name | _ -> Pretty.Icon.dir_char ^ " " ^ pad name)