Subject: [PATCH v2 0/5] use the pager in 'add -p'

From: Rubén Justo <[email-masked]>
Date: Tue, 21 May 2024 22:49:44 +0200

Invoke the pager when displaying hunks during "add -p" sessions, to make                                                                     
it easier for the user to review hunks longer than one screen height.                                                                        
                                                                                                                                             
This iteration, v2, reduces the use of the pager to the 'print' command
when invoked using a capital 'P'.                 

Rubén Justo (5):
  add-patch: test for 'p' command
  pager: do not close fd 2 unnecessarily
  pager: introduce wait_for_pager
  test-terminal: introduce --no-stdin-pty
  add-patch: render hunks through the pager

 add-patch.c                | 14 ++++++++++---
 pager.c                    | 41 ++++++++++++++++++++++++++++++++------
 pager.h                    |  1 +
 t/t3701-add-interactive.sh | 37 ++++++++++++++++++++++++++++++++++
 t/test-terminal.perl       | 32 ++++++++++++++++-------------
 5 files changed, 102 insertions(+), 23 deletions(-)

Range-diff against v1:
1:  340b090e64 = 1:  05edb72326 add-patch: test for 'p' command
2:  7cb16742ef = 2:  8fe915a820 pager: do not close fd 2 unnecessarily
3:  207faad1b1 = 3:  fc629a7334 pager: introduce wait_for_pager
4:  a3815e20c3 = 4:  d2bd591e65 test-terminal: introduce --no-stdin-pty
5:  d5886bf9cd ! 5:  d3c11dbb1d add-patch: render hunks through the pager
    @@ Metadata
      ## Commit message ##
         add-patch: render hunks through the pager
     
    -    Invoke the pager when displaying hunks during "add -p" sessions, to make
    -    it easier for the user to review hunks longer than one screen height.
    +    Make the print command to trigger the pager when invoked using a capital
    +    'P', to make it easier for the user to review long hunks.
     
         Signed-off-by: Rubén Justo <[email-masked]>
     
    @@ add-patch.c
      #include "read-cache-ll.h"
      #include "repository.h"
      #include "strbuf.h"
    +@@ add-patch.c: N_("j - leave this hunk undecided, see next undecided hunk\n"
    +    "/ - search for a hunk matching the given regex\n"
    +    "s - split the current hunk into smaller hunks\n"
    +    "e - manually edit the current hunk\n"
    +-   "p - print the current hunk\n"
    ++   "p - print the current hunk, 'P' to use the pager\n"
    +    "? - print help\n");
    + 
    + static int patch_update_file(struct add_p_state *s,
    +@@ add-patch.c: static int patch_update_file(struct add_p_state *s,
    + 	struct hunk *hunk;
    + 	char ch;
    + 	struct child_process cp = CHILD_PROCESS_INIT;
    +-	int colored = !!s->colored.len, quit = 0;
    ++	int colored = !!s->colored.len, quit = 0, use_pager = 0;
    + 	enum prompt_mode_type prompt_mode_type;
    + 	enum {
    + 		ALLOW_GOTO_PREVIOUS_HUNK = 1 << 0,
     @@ add-patch.c: static int patch_update_file(struct add_p_state *s,
      		strbuf_reset(&s->buf);
      		if (file_diff->hunk_nr) {
      			if (rendered_hunk_index != hunk_index) {
    -+				setup_pager();
    ++				if (use_pager)
    ++					setup_pager();
      				render_hunk(s, hunk, 0, colored, &s->buf);
      				fputs(s->buf.buf, stdout);
      				rendered_hunk_index = hunk_index;
    -+				wait_for_pager();
    ++				if (use_pager) {
    ++					wait_for_pager();
    ++					use_pager = 0;
    ++				}
      			}
      
      			strbuf_reset(&s->buf);
    +@@ add-patch.c: static int patch_update_file(struct add_p_state *s,
    + 				hunk->use = USE_HUNK;
    + 				goto soft_increment;
    + 			}
    +-		} else if (s->answer.buf[0] == 'p') {
    ++		} else if (ch == 'p') {
    + 			rendered_hunk_index = -1;
    ++			use_pager = (s->answer.buf[0] == 'P') ? 1 : 0;
    + 		} else if (s->answer.buf[0] == '?') {
    + 			const char *p = _(help_patch_remainder), *eol = p;
    + 
     
      ## t/t3701-add-interactive.sh ##
     @@ t/t3701-add-interactive.sh: test_expect_success 'print again the hunk' '
    @@ t/t3701-add-interactive.sh: test_expect_success 'print again the hunk' '
     +test_expect_success TTY 'print again the hunk (PAGER)' '
     +	test_when_finished "git reset" &&
     +	cat >expect <<-EOF &&
    -+	PAGER <GREEN>+<RESET><GREEN>15<RESET>
    -+	PAGER  20<RESET>
    ++	<GREEN>+<RESET><GREEN>15<RESET>
    ++	 20<RESET>
     +	<BOLD;BLUE>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>PAGER <CYAN>@@ -1,2 +1,3 @@<RESET>
     +	PAGER  10<RESET>
     +	PAGER <GREEN>+<RESET><GREEN>15<RESET>
     +	PAGER  20<RESET>
     +	<BOLD;BLUE>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
     +	EOF
    -+	test_write_lines s y g 1 p |
    ++	test_write_lines s y g 1 P |
     +	(
     +		GIT_PAGER="sed s/^/PAGER\ /" &&
     +		export GIT_PAGER &&
-- 
2.45.1.221.gd3c11dbb1d