Skip to content

Commit 464691e

Browse files
authored
[#36] Refactor init (#54)
1 parent e85d1a8 commit 464691e

File tree

7 files changed

+99
-81
lines changed

7 files changed

+99
-81
lines changed

lib/cli.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ let ignore_size_warning_arg =
2323
Arg.(value & flag & info [ "i"; "ignore-size-warning" ] ~doc)
2424

2525
let run owner_repo local_path log_file ignore_size_warning =
26-
Tui.start ~owner_repo ~local_path ~log_file ~ignore_size_warning
26+
Tui.start { owner_repo; local_path; log_file; ignore_size_warning }
2727

2828
let gh_tui_term =
2929
Term.(

lib/tui/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
extra
77
fs
88
gh
9+
init
910
log
1011
model
1112
pretty

lib/tui/init/dune

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(library
2+
(name init)
3+
(libraries
4+
terminal_size
5+
;; Internal dependencies
6+
model
7+
fs))

lib/tui/init/init.ml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
type owner_repo = {
2+
owner : string;
3+
repo : string;
4+
}
5+
6+
let parse_owner_repo owner_repo =
7+
match String.split_on_char '/' owner_repo with
8+
| [ owner; repo ] -> { owner; repo }
9+
| _ ->
10+
Printf.eprintf "❌ Expected <owner>/<repo> but got: '%s'\n" owner_repo;
11+
exit 1
12+
13+
let clone_repo ~owner_repo ~local_path =
14+
match local_path with
15+
| Some path -> path
16+
| None ->
17+
let { owner; repo } = owner_repo in
18+
let temp_dir =
19+
Filename.temp_dir "github-tui-" (Printf.sprintf "%s-%s" owner repo)
20+
in
21+
let cmd =
22+
Printf.sprintf "git clone [email protected]:%s/%s.git %s" owner repo
23+
temp_dir
24+
in
25+
Shell.proc cmd;
26+
temp_dir
27+
28+
let read_root_tree ~root_dir_path =
29+
let tree = Fs.read_tree root_dir_path in
30+
let files =
31+
match tree with
32+
| Fs.File { name; _ } ->
33+
Printf.eprintf "Given path '%s' is not a directory!" name;
34+
exit 1
35+
| Fs.Dir { children = files; _ } -> files
36+
in
37+
files
38+
39+
type terminal = {
40+
height : int;
41+
width : int;
42+
}
43+
44+
let min_height, min_width = (40, 140)
45+
46+
let get_terminal_dimensions ignore_size_warning =
47+
match (Terminal_size.get_rows (), Terminal_size.get_columns ()) with
48+
| Some height, Some width ->
49+
if (not ignore_size_warning) && (height < min_height || width < min_width)
50+
then (
51+
Printf.eprintf
52+
{|⚠️ Terminal size is too small! GitHub TUI works better on bigger terminals.
53+
Expected size: %3d width x %3d height
54+
But got: %3d width x %3d height
55+
56+
Pass the --ignore-size-warning flag to run anyway.
57+
|}
58+
min_width min_height width height;
59+
exit 1);
60+
{ height; width }
61+
| _ ->
62+
Printf.eprintf "⚠️ Not able to get the terminal size.\n";
63+
exit 1
64+
65+
type t = {
66+
owner_repo : string;
67+
local_path : string option;
68+
log_file : string option;
69+
ignore_size_warning : bool;
70+
}
71+
72+
let init { owner_repo; local_path; ignore_size_warning; log_file = _ } :
73+
Model.initial_data =
74+
let ({ owner; repo } as owner_repo) = parse_owner_repo owner_repo in
75+
let root_dir_path = clone_repo ~owner_repo ~local_path in
76+
let files = Lazy.force (read_root_tree ~root_dir_path) in
77+
let { height; width } = get_terminal_dimensions ignore_size_warning in
78+
{ owner; repo; root_dir_path; files; width; height }

lib/tui/init/init.mli

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type t = {
2+
owner_repo : string;
3+
local_path : string option;
4+
log_file : string option;
5+
ignore_size_warning : bool;
6+
}
7+
8+
val init : t -> Model.initial_data

lib/tui/tui.ml

Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,5 @@
1-
type owner_repo = {
2-
owner : string;
3-
repo : string;
4-
}
5-
6-
let parse_owner_repo owner_repo =
7-
match String.split_on_char '/' owner_repo with
8-
| [ owner; repo ] -> { owner; repo }
9-
| _ ->
10-
Printf.eprintf "❌ Expected <owner>/<repo> but got: '%s'\n" owner_repo;
11-
exit 1
12-
13-
let clone_repo ~owner_repo ~local_path =
14-
match local_path with
15-
| Some path -> path
16-
| None ->
17-
let { owner; repo } = owner_repo in
18-
let temp_dir =
19-
Filename.temp_dir "github-tui-" (Printf.sprintf "%s-%s" owner repo)
20-
in
21-
let cmd =
22-
Printf.sprintf "git clone [email protected]:%s/%s.git %s" owner repo
23-
temp_dir
24-
in
25-
Shell.proc cmd;
26-
temp_dir
27-
28-
let read_root_tree ~root_dir_path =
29-
let tree = Fs.read_tree root_dir_path in
30-
let files =
31-
match tree with
32-
| Fs.File { name; _ } ->
33-
Printf.eprintf "Given path '%s' is not a directory!" name;
34-
exit 1
35-
| Fs.Dir { children = files; _ } -> files
36-
in
37-
files
38-
39-
type terminal = {
40-
height : int;
41-
width : int;
42-
}
43-
44-
let min_height, min_width = (40, 140)
45-
46-
let get_terminal_dimensions ignore_size_warning =
47-
match (Terminal_size.get_rows (), Terminal_size.get_columns ()) with
48-
| Some height, Some width ->
49-
if (not ignore_size_warning) && (height < min_height || width < min_width)
50-
then (
51-
Printf.eprintf
52-
{|⚠️ Terminal size is too small! GitHub TUI works better on bigger terminals.
53-
Expected size: %3d width x %3d height
54-
But got: %3d width x %3d height
55-
56-
Pass the --ignore-size-warning flag to run anyway.
57-
|}
58-
min_width min_height width height;
59-
exit 1);
60-
{ height; width }
61-
| _ ->
62-
Printf.eprintf "⚠️ Not able to get the terminal size.\n";
63-
exit 1
64-
65-
let init ~owner_repo ~local_path ~ignore_size_warning : Model.initial_data =
66-
let ({ owner; repo } as owner_repo) = parse_owner_repo owner_repo in
67-
let root_dir_path = clone_repo ~owner_repo ~local_path in
68-
let files = Lazy.force (read_root_tree ~root_dir_path) in
69-
let { height; width } = get_terminal_dimensions ignore_size_warning in
70-
{ owner; repo; root_dir_path; files; width; height }
71-
72-
let start ~owner_repo ~local_path ~log_file ~ignore_size_warning =
73-
let initial_data = init ~owner_repo ~local_path ~ignore_size_warning in
1+
let start args =
2+
let initial_data = Init.init args in
743
let init = Model.initial_model initial_data in
754
let app = Tea.make ~init ~update:Update.update ~view:View.view in
76-
Tea.run ?path:log_file app
5+
Tea.run ?path:args.log_file app

lib/tui/tui.mli

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
val start :
2-
owner_repo:string ->
3-
local_path:string option ->
4-
log_file:string option ->
5-
ignore_size_warning:bool ->
6-
unit
1+
val start : Init.t -> unit

0 commit comments

Comments
 (0)