Clone any repository

This commit is contained in:
Dmitrii Kovanikov 2024-06-23 17:29:16 +01:00
parent 2cba1ab3fb
commit 43f1cb0b5d
5 changed files with 44 additions and 9 deletions

View File

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

View File

@ -7,4 +7,5 @@
extra
fs
pretty
scroll))
scroll
shell))

View File

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

View File

@ -1 +1 @@
val start : string -> string -> unit
val start : repo:string -> local_path:string option -> unit

View File

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