From a5e6cf13f197519666084ff53dceaf60c7006e2e Mon Sep 17 00:00:00 2001 From: Dmitrii Kovanikov Date: Sun, 24 Mar 2024 11:42:12 +0000 Subject: [PATCH] Add terminal size debug info --- dune-project | 3 ++- github_tui.opam | 1 + lib/dune | 2 +- lib/tui/app.ml | 17 ++++++++++++----- lib/tui/model.ml | 14 ++++++++++++-- lib/tui/view.ml | 7 ++++++- 6 files changed, 34 insertions(+), 10 deletions(-) diff --git a/dune-project b/dune-project index 1985dd0..fb949a0 100644 --- a/dune-project +++ b/dune-project @@ -16,13 +16,14 @@ (name github_tui) (synopsis "A short synopsis") (description "A longer description") - (depends + (depends (ocaml (>= "5.1")) dune (ANSITerminal (>= "0.8.5")) (cmdliner (>= "1.2.0")) (ezcurl (>= "0.2.4")) (minttea (>= "0.0.2")) + (terminal_size (>= "0.2.0")) uuseg ) (tags diff --git a/github_tui.opam b/github_tui.opam index 1f9bd8f..60168bc 100644 --- a/github_tui.opam +++ b/github_tui.opam @@ -15,6 +15,7 @@ depends: [ "cmdliner" {>= "1.2.0"} "ezcurl" {>= "0.2.4"} "minttea" {>= "0.0.2"} + "terminal_size" {>= "0.2.0"} "uuseg" "odoc" {with-doc} ] diff --git a/lib/dune b/lib/dune index c548e54..724e6b8 100644 --- a/lib/dune +++ b/lib/dune @@ -2,4 +2,4 @@ (library (name github_tui) - (libraries ANSITerminal cmdliner ezcurl minttea uuseg.string)) + (libraries ANSITerminal cmdliner ezcurl minttea terminal_size uuseg.string)) diff --git a/lib/tui/app.ml b/lib/tui/app.ml index 9cb1927..b5104eb 100644 --- a/lib/tui/app.ml +++ b/lib/tui/app.ml @@ -1,10 +1,17 @@ let init ~repo ~path : Model.initial_data = let tree = Fs.read_tree path in - match tree with - | Fs.File path -> - Printf.printf "Given path '%s' is not a directory!" path; - exit 1 - | Fs.Dir (dirname, files) -> { repo; dirname; files } + let dirname, files = + match tree with + | Fs.File path -> + Printf.printf "Given path '%s' is not a directory!" path; + exit 1 + | Fs.Dir (dirname, files) -> (dirname, files) + in + let terminal_rows = Option.value (Terminal_size.get_rows ()) ~default:120 in + let terminal_cols = + Option.value (Terminal_size.get_columns ()) ~default:140 + in + { repo; dirname; files; terminal_rows; terminal_cols } let app = Minttea.app ~init:Init.init ~update:Update.update ~view:View.view () diff --git a/lib/tui/model.ml b/lib/tui/model.ml index a0060cf..75828f2 100644 --- a/lib/tui/model.ml +++ b/lib/tui/model.ml @@ -11,6 +11,8 @@ type tab = | PullRequests type t = { + terminal_rows : int; + terminal_cols : int; repo : string; current_tab : tab; code_tab : code_tab; @@ -20,7 +22,15 @@ type initial_data = { repo : string; dirname : string; files : Fs.tree array; + terminal_rows : int; + terminal_cols : int; } -let initial_model { repo; dirname; files } = - { repo; current_tab = Code; code_tab = { dirname; fs = Fs.zip_it files } } +let initial_model { repo; dirname; files; terminal_rows; terminal_cols } = + { + terminal_rows; + terminal_cols; + repo; + current_tab = Code; + code_tab = { dirname; fs = Fs.zip_it files }; + } diff --git a/lib/tui/view.ml b/lib/tui/view.ml index 3cf827a..79feae2 100644 --- a/lib/tui/view.ml +++ b/lib/tui/view.ml @@ -2,6 +2,10 @@ let style_repo = ANSITerminal.[ Bold; blue ] let style_selected = ANSITerminal.[ Bold; green ] let style_directory = ANSITerminal.[ Bold; magenta ] +let debug_section (model: Model.t) = + let debug_info = Printf.sprintf "%dw x %dh" model.terminal_cols model.terminal_rows in + Pretty.str debug_info + let tabs_section cur_tab = let open Pretty in let sep = col [ str " "; str " "; str "─" ] in @@ -29,10 +33,11 @@ let tab_content_section (model : Model.t) = let to_doc (model : Model.t) = let open Pretty in let empty = str "" in + let debug = debug_section model in let repo = fmt style_repo model.repo in let tabs = tabs_section model.current_tab in let content = tab_content_section model in - col [ repo; empty; tabs; content; empty ] + col [ row [repo; str " "; debug ]; empty; tabs; content; empty ] let view (model : Model.t) = model |> to_doc |> Pretty.render