git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v4 6/6] add-patch: introduce the command '|'

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 5, 2024, 05:16 UTC
Message-ID
<xmqq4ja8ynop.fsf@gitster.g>
In-Reply-To
<xmqqy17kws2k.fsf@gitster.g>
Junio C Hamano <gitster@pobox.com> writes:
> By the way, it should be trivial to make the "custom" pager more sticky.

Here is what you can squash into this step. I gave many other pieces of style and design advices in other messages, which are not covered by this patch but the necessary fixes should be obvious.

This message is only about making the custom pager stick during a session. It does not adjust the command help to give the last pager command (or literally "your pager"), either.

---- >8 ----
Subject: [PATCH] add-p: make custom pager sticky during a session

The original design kept resetting the choice of the custom pager every time the '|' command is used. This was way cumbersome to use.

Keep track of the last choice in the add_p_state.custom_pager member. This value can stick across calls to patch_update_file() function, so a custom pager used for choosing hunks in one file can be carried over to the view hunks in the next file.

As we make the custom pager stick, we need a way to reset it back to the default value (which we use NULL for, as set_custom_pager() takes the value to mean "use the default one").

As there is no value that can say "pager is not used" suitable for the custom_pager member to take, we need a separate "use_pager" flag so that the fact that '|' command was used can be propagated to the next iteration of the loop, independent from what custom pager is used.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 add-patch.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)
diff --git a/add-patch.c b/add-patch.c
index da13e267db..71ee7f9a94 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -272,6 +272,7 @@ struct add_p_state {
 	/* patch mode */
 	struct patch_mode *mode;
 	const char *revision;
+	char *custom_pager;
 };
 
 static void add_p_state_clear(struct add_p_state *s)
@@ -285,6 +286,7 @@ static void add_p_state_clear(struct add_p_state *s)
 	for (i = 0; i < s->file_diff_nr; i++)
 		free(s->file_diff[i].hunk);
 	free(s->file_diff);
+	free(s->custom_pager);
 	clear_add_i_state(&s->s);
 }
 
@@ -1403,7 +1405,7 @@ static int patch_update_file(struct add_p_state *s,
 	struct child_process cp = CHILD_PROCESS_INIT;
 	int colored = !!s->colored.len, quit = 0;
 	enum prompt_mode_type prompt_mode_type;
-	const char* pager = NULL;
+	int use_pager = 0;
 	enum {
 		ALLOW_GOTO_PREVIOUS_HUNK = 1 << 0,
 		ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK = 1 << 1,
@@ -1452,14 +1454,14 @@ 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 (pager)
-					setup_custom_pager(pager);
+				if (use_pager)
+					setup_custom_pager(s->custom_pager);
 				render_hunk(s, hunk, 0, colored, &s->buf);
 				fputs(s->buf.buf, stdout);
 				rendered_hunk_index = hunk_index;
-				if (pager) {
+				if (use_pager) {
 					wait_for_pager();
-					pager = NULL;
+					use_pager = 0;
 				}
 			}
 
@@ -1685,15 +1687,26 @@ static int patch_update_file(struct add_p_state *s,
 		} else if (s->answer.buf[0] == 'p') {
 			rendered_hunk_index = -1;
 		} else if (ch == '|') {
-			strbuf_remove(&s->answer, 0, 1);
-			if (s->s.use_single_key && s->answer.len == 0) {
+			if (!s->s.use_single_key) {
+				strbuf_remove(&s->answer, 0, 1);
+			} else {
 				printf("%s", _("program? "));
 				fflush(stdout);
 				strbuf_getline(&s->answer, stdin);
-				strbuf_trim_trailing_newline(&s->answer);
 			}
-			strbuf_trim(&s->answer);
-			pager = s->answer.buf;
+			strbuf_trim_trailing_newline(&s->answer);
+
+			if (!s->answer.len)
+				; /* empty input - reuse the previous */
+			else {
+				strbuf_trim(&s->answer);
+				FREE_AND_NULL(s->custom_pager);
+				if (!s->answer.len)
+					; /* semi-empty - use your pager */
+				else
+					s->custom_pager = xstrdup(s->answer.buf);
+			}
+			use_pager = 1;
 			rendered_hunk_index = -1;
 		} else if (s->answer.buf[0] == '?') {
 			const char *p = _(help_patch_remainder), *eol = p;
-- 
2.45.2-409-g7b0defb391
Previous: Dragan SimicNext: Jeff King
Message 91 of 113 in “use the pager in 'add -p'”
  1. 0/5 use the pager in 'add -p'Rubén Justo, May 19, 2024
  2. 1/5 add-patch: test for 'p' commandRubén Justo, May 19, 2024
  3. 2/5 pager: do not close fd 2 unnecessarilyRubén Justo, May 19, 2024
  4. Junio C HamanoMay 20, 2024
  5. Rubén JustoMay 20, 2024
  6. Junio C HamanoMay 21, 2024
  7. Rubén JustoMay 21, 2024
  8. Junio C HamanoMay 21, 2024
  9. Rubén JustoMay 22, 2024
  10. Junio C HamanoMay 22, 2024
  11. Rubén JustoMay 26, 2024
  12. Junio C HamanoMay 26, 2024
  13. 3/5 pager: introduce wait_for_pagerRubén Justo, May 19, 2024
  14. 4/5 test-terminal: introduce --no-stdin-ptyRubén Justo, May 19, 2024
  15. 5/5 add-patch: render hunks through the pagerRubén Justo, May 19, 2024
  16. Junio C HamanoMay 20, 2024
  17. Dragan SimicMay 20, 2024
  18. Rubén JustoMay 20, 2024
  19. Dragan SimicMay 20, 2024
  20. Rubén JustoMay 21, 2024
  21. Jeff KingMay 21, 2024
  22. Rubén JustoMay 21, 2024
  23. Jeff KingMay 23, 2024
  24. Junio C HamanoMay 23, 2024
  25. Dragan SimicMay 23, 2024
  26. Junio C HamanoMay 23, 2024
  27. Dragan SimicMay 23, 2024
  28. Dragan SimicMay 23, 2024
  29. Junio C HamanoMay 23, 2024
  30. Dragan SimicMay 23, 2024
  31. Jeff KingMay 25, 2024
  32. Rubén JustoMay 23, 2024
  33. Dragan SimicMay 23, 2024
  34. Rubén JustoMay 20, 2024
  35. Junio C HamanoMay 20, 2024
  36. Rubén JustoMay 20, 2024
  37. 0/5 use the pager in 'add -p'Rubén Justo, May 21, 2024
  38. 1/5 add-patch: test for 'p' commandRubén Justo, May 21, 2024
  39. 2/5 pager: do not close fd 2 unnecessarilyRubén Justo, May 21, 2024
  40. 3/5 pager: introduce wait_for_pagerRubén Justo, May 21, 2024
  41. 4/5 test-terminal: introduce --no-stdin-ptyRubén Justo, May 21, 2024
  42. 5/5 add-patch: render hunks through the pagerRubén Justo, May 21, 2024
  43. Dragan SimicMay 22, 2024
  44. Junio C HamanoMay 22, 2024
  45. Rubén JustoMay 22, 2024
  46. Dragan SimicMay 22, 2024
  47. 0/6 use the pager in 'add -p'Rubén Justo, Jun 2, 2024
  48. 1/6 add-patch: test for 'p' commandRubén Justo, Jun 2, 2024
  49. 2/6 pager: do not close fd 2 unnecessarilyRubén Justo, Jun 2, 2024
  50. 3/6 pager: introduce wait_for_pagerRubén Justo, Jun 2, 2024
  51. 4/6 pager: introduce setup_custom_pagerRubén Justo, Jun 2, 2024
  52. 5/6 test-terminal: introduce --no-stdin-ptyRubén Justo, Jun 2, 2024
  53. 6/6 add-patch: introduce the command '|'Rubén Justo, Jun 2, 2024
  54. Junio C HamanoJun 2, 2024
  55. Junio C HamanoJun 2, 2024
  56. Rubén JustoJun 2, 2024
  57. Rubén JustoJun 2, 2024
  58. Dragan SimicJun 2, 2024
  59. Junio C HamanoJun 3, 2024
  60. Dragan SimicJun 3, 2024
  61. Junio C HamanoJun 3, 2024
  62. Dragan SimicJun 4, 2024
  63. Dragan SimicJun 2, 2024
  64. Junio C HamanoJun 3, 2024
  65. Dragan SimicJun 4, 2024
  66. Dragan SimicJun 4, 2024
  67. Rubén JustoJun 3, 2024
  68. Dragan SimicJun 4, 2024
  69. 0/6 use the pager in 'add -p'Rubén Justo, Jun 3, 2024
  70. 1/6 add-patch: test for 'p' commandRubén Justo, Jun 3, 2024
  71. 2/6 pager: do not close fd 2 unnecessarilyRubén Justo, Jun 3, 2024
  72. Junio C HamanoJun 4, 2024
  73. 3/6 pager: introduce wait_for_pagerRubén Justo, Jun 3, 2024
  74. Phillip WoodJun 4, 2024
  75. Junio C HamanoJun 4, 2024
  76. Rubén JustoJun 5, 2024
  77. Junio C HamanoJun 4, 2024
  78. 4/6 pager: introduce setup_custom_pagerRubén Justo, Jun 3, 2024
  79. Junio C HamanoJun 4, 2024
  80. 5/6 test-terminal: introduce --no-stdin-ptyRubén Justo, Jun 3, 2024
  81. Phillip WoodJun 4, 2024
  82. Jeff KingJun 4, 2024
  83. Junio C HamanoJun 5, 2024
  84. Jeff KingJun 6, 2024
  85. Rubén JustoJun 5, 2024
  86. Jeff KingJun 6, 2024
  87. Rubén JustoJun 9, 2024
  88. 6/6 add-patch: introduce the command '|'Rubén Justo, Jun 3, 2024
  89. Junio C HamanoJun 4, 2024
  90. Dragan SimicJun 4, 2024
  91. Junio C HamanoJun 5, 2024
  92. Jeff KingJun 4, 2024
  93. Junio C HamanoJun 4, 2024
  94. Jeff KingJun 5, 2024
  95. Phillip WoodJun 5, 2024
  96. Dragan SimicJun 8, 2024
  97. Rubén JustoJun 9, 2024
  98. Dragan SimicJun 9, 2024
  99. Rubén JustoJun 10, 2024
  100. Dragan SimicJun 10, 2024
  101. Phillip WoodJun 10, 2024
  102. Junio C HamanoJun 10, 2024
  103. Rubén JustoJun 10, 2024
  104. Junio C HamanoJun 10, 2024
  105. Dragan SimicJun 10, 2024
  106. Dragan SimicJun 10, 2024
  107. Junio C HamanoJun 10, 2024
  108. Dragan SimicJun 10, 2024
  109. phillip.wood123@gmail.comJun 9, 2024
  110. Dragan SimicJun 9, 2024
  111. Phillip WoodJun 10, 2024
  112. Dragan SimicJun 10, 2024
  113. Junio C HamanoJun 5, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.