Skip to content

Commit

Permalink
added the pager functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
yokurang committed Jul 2, 2024
1 parent 03e1995 commit 90519f7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
22 changes: 14 additions & 8 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ open Cmdliner
open ExtUnix.Specific

let main file_path =
let file = match file_path with
| Some path -> In_channel.open_bin path
let input_channel, term =
match file_path with
| Some path -> (In_channel.open_bin path, None)
| None ->
let tty = ttyname Unix.stdin
in In_channel.open_bin tty
in let s = In_channel.input_all file
in let patch = Patch.to_diffs s in
Interactive_viewer.start patch
let tty_path = ttyname Unix.stdout in
let tty_fd = Unix.openfile tty_path [Unix.O_RDWR] 0o500 in
let term = Notty_unix.Term.create ~output:tty_fd ~input:tty_fd () in
(In_channel.stdin, Some term)
in
let input_content = In_channel.input_all input_channel in
In_channel.close input_channel;
let patch = Patch.to_diffs input_content in
Interactive_viewer.start ?term patch;
(match term with Some t -> Notty_unix.Term.release t | None -> ())

let file_arg =
let doc = "Path to the file containing the Git diff. If not provided, reads from the terminal." in
let doc = "Path to the file containing the Git diff. If not provided, reads from stdin." in
Arg.(value & pos 0 (some string) None & info [] ~docv:"FILE" ~doc)

let cmd =
Expand Down
2 changes: 1 addition & 1 deletion lib/interactive_viewer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ let view (patches : Patch.t list) =
in
W.vbox [ ui ]

let start patch = Ui_loop.run ~quit ~tick_period:0.2 (view patch)
let start ?term patch = Ui_loop.run ?term ~quit ~tick_period:0.2 (view patch)

let start_test patch events width height =
let convert_char_to_key (c : char) : Ui.key = (`ASCII c, []) in
Expand Down
31 changes: 28 additions & 3 deletions lib/interactive_viewer.mli
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
(** Render and navigate through a diff. *)

val start : Patch.t list -> unit
module W = Nottui_widgets
val operation_info : Patch.t Zipper.t Lwd.var -> Nottui.ui Lwd.t
val ui_of_operation : Patch.operation -> Nottui.ui
val string_of_hunk : Patch.hunk -> string
val current_operation : Patch.t Zipper.t Lwd.var -> Nottui.ui Lwd.t
val current_hunks : Patch.t Zipper.t Lwd.var -> Nottui.ui Lwd.t
type direction = Prev | Next
val navigate : 'a Zipper.t Lwd.var -> direction -> unit
val quit : bool Lwd.var
val help : bool Lwd.var
val additions_and_removals :
[< `Common of 'a | `Mine of 'b | `Their of 'c ] list -> int * int
val accumulate_count : Patch.hunk list -> int * int
val change_summary : Patch.t Zipper.t Lwd.var -> Nottui.ui Lwd.t
type view_mode = SideBySide | Normal
val view_mode : view_mode Lwd.var
val toggle_view_mode : unit -> unit
type line = Change of string | Common of string | Empty
val split_and_align_hunk :
[< `Common of string | `Mine of string | `Their of string > `Common ] list ->
line list -> line list -> line list * line list
val lines_with_numbers : line list -> Notty.attr -> string -> Nottui.ui list
val create_summary :
int -> int -> Notty.attr -> [< `Add | `Remove ] -> Nottui.ui
val ui_of_hunk_side_by_side : Patch.hunk -> Nottui.ui
val current_hunks_side_by_side : Patch.t Zipper.t Lwd.var -> Nottui.ui Lwd.t
val view : Patch.t list -> Nottui.ui Lwd.t
val start : ?term:Notty_unix.Term.t -> Patch.t list -> unit
val start_test : Patch.t list -> char list -> int -> int -> unit

0 comments on commit 90519f7

Please sign in to comment.