From 43f1cb0b5d4559983a2772801f66c783375d13ea Mon Sep 17 00:00:00 2001 From: Dmitrii Kovanikov Date: Sun, 23 Jun 2024 17:29:16 +0100 Subject: [PATCH] Clone any repository --- lib/cli.ml | 5 +++-- lib/tui/dune | 3 ++- lib/tui/tui.ml | 41 +++++++++++++++++++++++++++++++++++++---- lib/tui/tui.mli | 2 +- lib/tui/update.ml | 2 +- 5 files changed, 44 insertions(+), 9 deletions(-) diff --git a/lib/cli.ml b/lib/cli.ml index 6cf54d0..0cd59be 100644 --- a/lib/cli.ml +++ b/lib/cli.ml @@ -8,10 +8,11 @@ let path_arg = let doc = "Path to a local directory of a GitHub repository" in Arg.( value - & opt string "." + & opt (some string) None & info [ "d"; "directory" ] ~docv:"DIRECTORY_PATH" ~doc) -let gh_tui_term = Term.(const Tui.start $ repo_arg $ path_arg) +let run repo local_path = Tui.start ~repo ~local_path +let gh_tui_term = Term.(const run $ repo_arg $ path_arg) let cmd = let doc = "TUI of a GitHub repository" in diff --git a/lib/tui/dune b/lib/tui/dune index f30afa1..98e4f2b 100644 --- a/lib/tui/dune +++ b/lib/tui/dune @@ -7,4 +7,5 @@ extra fs pretty - scroll)) + scroll + shell)) diff --git a/lib/tui/tui.ml b/lib/tui/tui.ml index 8305275..25dbf80 100644 --- a/lib/tui/tui.ml +++ b/lib/tui/tui.ml @@ -1,4 +1,23 @@ -let init ~repo ~root_dir_path : Model.initial_data = +let clone_repo ~repo ~local_path = + match local_path with + | Some path -> path + | None -> + let owner, repo = + match String.split_on_char '/' repo with + | [ owner; repo ] -> (owner, repo) + | _ -> ("chshersh", "zbg") + in + let temp_dir = + Filename.temp_dir "github-tui-" (Printf.sprintf "%s-%s" owner repo) + in + let cmd = + Printf.sprintf "git clone git@github.com:%s/%s.git %s" owner repo + temp_dir + in + let _ = Shell.proc_stdout cmd in + temp_dir + +let read_root_tree ~root_dir_path = let tree = Fs.read_tree root_dir_path in let files = match tree with @@ -7,14 +26,28 @@ let init ~repo ~root_dir_path : Model.initial_data = exit 1 | Fs.Dir (_, files) -> files in - let height = Option.value (Terminal_size.get_rows ()) ~default:120 in + files + +type terminal = { + height : int; + width : int; +} + +let get_terminal_dimensions () = + let height = Option.value (Terminal_size.get_rows ()) ~default:40 in let width = Option.value (Terminal_size.get_columns ()) ~default:140 in + { height; width } + +let init ~repo ~local_path : Model.initial_data = + let root_dir_path = clone_repo ~repo ~local_path in + let files = read_root_tree ~root_dir_path in + let { height; width } = get_terminal_dimensions () in { repo; root_dir_path; files; width; height } let app = Minttea.app ~init:Init.init ~update:Update.update ~view:View.view () -let start repo root_dir_path = - let initial_data = init ~repo ~root_dir_path in +let start ~repo ~local_path = + let initial_data = init ~repo ~local_path in let initial_model = Model.initial_model initial_data in let config = Minttea.make_config ~fps:1 () in Minttea.start ~config app ~initial_model diff --git a/lib/tui/tui.mli b/lib/tui/tui.mli index dc25363..a77024d 100644 --- a/lib/tui/tui.mli +++ b/lib/tui/tui.mli @@ -1 +1 @@ -val start : string -> string -> unit +val start : repo:string -> local_path:string option -> unit diff --git a/lib/tui/update.ml b/lib/tui/update.ml index e2748f0..c23817a 100644 --- a/lib/tui/update.ml +++ b/lib/tui/update.ml @@ -28,7 +28,7 @@ let update event (model : Model.t) = match event with (* if we press `q` or the escape key, we exit *) | Event.KeyDown ((Key "q" | Escape), _modifier) -> - (model, Command.Seq [ Command.Exit_alt_screen; Command.Quit ]) + (model, Command.(Seq [ Exit_alt_screen; Quit ])) (* if we press a digit, we switch to the corresponding tab *) | Event.KeyDown (Key "1", _modifier) -> ({ model with current_tab = Model.Code }, Command.Noop)