diff --git a/bin/main.ml b/bin/main.ml index 030f002..f13b176 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -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 = diff --git a/lib/interactive_viewer.ml b/lib/interactive_viewer.ml index 3dd2b9a..063cf99 100644 --- a/lib/interactive_viewer.ml +++ b/lib/interactive_viewer.ml @@ -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 diff --git a/lib/interactive_viewer.mli b/lib/interactive_viewer.mli index b55057a..0c09b44 100644 --- a/lib/interactive_viewer.mli +++ b/lib/interactive_viewer.mli @@ -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