diff mbox series

[v2,5/5] add-patch: render hunks through the pager

Message ID 310a2904-681a-4bee-96b9-90a2dc107975@gmail.com (mailing list archive)
State Superseded
Headers show
Series use the pager in 'add -p' | expand

Commit Message

Rubén Justo May 21, 2024, 8:52 p.m. UTC
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 <rjusto@gmail.com>
---
 add-patch.c                | 14 +++++++++++---
 t/t3701-add-interactive.sh | 21 +++++++++++++++++++++
 2 files changed, 32 insertions(+), 3 deletions(-)

Comments

Dragan Simic May 22, 2024, 8:09 a.m. UTC | #1
Hello Ruben,

On 2024-05-21 22:52, Rubén Justo wrote:
> 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.
> 
> ...
> 
> @@ -1387,7 +1388,7 @@ 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"

I think it would be better to move the description of "P" into
a separate line after the "p" line, perhaps like this:

   "P - use the pager to print the current hunk\n"

I know, we'd sacrifice one line of the valuable vertical space
this way, but I find it more consistent and much harder to miss
the new "P" option.

Overall, I find the introduction of "P" as the new single-character
menu option fine.  Maybe we can later add "-P" as the new command-
line option, if there will be some demand to do that.
Junio C Hamano May 22, 2024, 6:47 p.m. UTC | #2
Dragan Simic <dsimic@manjaro.org> writes:

>> @@ -1387,7 +1388,7 @@ 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"
>
> I think it would be better to move the description of "P" into
> a separate line after the "p" line, perhaps like this:
>
>   "P - use the pager to print the current hunk\n"

Sounds good to me, too.
Rubén Justo May 22, 2024, 9:23 p.m. UTC | #3
On Wed, May 22, 2024 at 10:09:25AM +0200, Dragan Simic wrote:

> > @@ -1387,7 +1388,7 @@ 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"
> 
> I think it would be better to move the description of "P" into
> a separate line after the "p" line, perhaps like this:
> 
>   "P - use the pager to print the current hunk\n"
> 
> I know, we'd sacrifice one line of the valuable vertical space
> this way, but I find it more consistent and much harder to miss
> the new "P" option.

Making 'P' a /version/ of 'p' allows us to skip adding 'P' to the list
of available options:

    (1/1) Stage this hunk [y,n,q,a,d,j,J,k,K,g,/,s,e,p,P,?]

This is what I though and this long list is what worries me.

But I see your point.  I am not opposed to adding a new line.
Dragan Simic May 22, 2024, 9:27 p.m. UTC | #4
On 2024-05-22 23:23, Rubén Justo wrote:
> On Wed, May 22, 2024 at 10:09:25AM +0200, Dragan Simic wrote:
> 
>> > @@ -1387,7 +1388,7 @@ 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"
>> 
>> I think it would be better to move the description of "P" into
>> a separate line after the "p" line, perhaps like this:
>> 
>>   "P - use the pager to print the current hunk\n"
>> 
>> I know, we'd sacrifice one line of the valuable vertical space
>> this way, but I find it more consistent and much harder to miss
>> the new "P" option.
> 
> Making 'P' a /version/ of 'p' allows us to skip adding 'P' to the list
> of available options:
> 
>     (1/1) Stage this hunk [y,n,q,a,d,j,J,k,K,g,/,s,e,p,P,?]
> 
> This is what I though and this long list is what worries me.

Oh, I wouldn't be worried too much about the length of the list of
the options.  It's feature-rich, so the list has to be a bit long. :)

> But I see your point.  I am not opposed to adding a new line.

Thanks.
diff mbox series

Patch

diff --git a/add-patch.c b/add-patch.c
index 2252895c28..d614536cb2 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -5,6 +5,7 @@ 
 #include "environment.h"
 #include "gettext.h"
 #include "object-name.h"
+#include "pager.h"
 #include "read-cache-ll.h"
 #include "repository.h"
 #include "strbuf.h"
@@ -1387,7 +1388,7 @@  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,
@@ -1398,7 +1399,7 @@  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,
@@ -1448,9 +1449,15 @@  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) {
+				if (use_pager)
+					setup_pager();
 				render_hunk(s, hunk, 0, colored, &s->buf);
 				fputs(s->buf.buf, stdout);
 				rendered_hunk_index = hunk_index;
+				if (use_pager) {
+					wait_for_pager();
+					use_pager = 0;
+				}
 			}
 
 			strbuf_reset(&s->buf);
@@ -1665,8 +1672,9 @@  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;
 
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index 52d7830de2..4be7a14419 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -558,6 +558,27 @@  test_expect_success 'print again the hunk' '
 	test_cmp expect actual.trimmed
 '
 
+test_expect_success TTY 'print again the hunk (PAGER)' '
+	test_when_finished "git reset" &&
+	cat >expect <<-EOF &&
+	<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 |
+	(
+		GIT_PAGER="sed s/^/PAGER\ /" &&
+		export GIT_PAGER &&
+		test_terminal --no-stdin-pty git add -p >actual
+	) &&
+	tail -n 7 <actual | test_decode_color >actual.trimmed &&
+	test_cmp expect actual.trimmed
+'
+
 test_expect_success 'navigate to hunk via regex' '
 	test_when_finished "git reset" &&
 	tr _ " " >expect <<-EOF &&