Re: [PATCH 5/5] add-patch: render hunks through the pager
- From
Junio C Hamano <gitster@pobox.com>
- Date
- May 20, 2024, 23:18 UTC
- Message-ID
- <xmqq5xv8dqd2.fsf@gitster.g>
- In-Reply-To
- <dcc9f9bf-3c0f-435f-ba10-35ff31122b7d@gmail.com>
Rubén Justo <rjusto@gmail.com> writes:
Show 6 quoted lines
>> even with "less -F" >> (--quit-if-one-screen). As we know how much output we are throwing >> at the user, we'd want to make this conditional to the size of the >> hunk being shown and the terminal height. > > Are you thinking of something like?:
I don't.
Your hunk may have overly wide lines in which case counting the number of lines may be insuffucient to measure the necessary display height. Besides, comparison with term_columns() is meaningless unless your window is square ;-)
An explicit 'P' might be palatable, though.
Thanks.
Show 30 quoted lines
>
> diff --git a/add-patch.c b/add-patch.c
> index cefa3941a3..495baad3ac 100644
> --- a/add-patch.c
> +++ b/add-patch.c
> @@ -1449,11 +1449,18 @@ 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();
> + int lines = 0;
> render_hunk(s, hunk, 0, colored, &s->buf);
> + for(int i = 0; i < s->buf.len; i++) {
> + if (s->buf.buf[i] == '\n')
> + lines++;
> + }
> + if (lines > term_columns())
> + setup_pager();
> fputs(s->buf.buf, stdout);
> rendered_hunk_index = hunk_index;
> - wait_for_pager();
> + if (lines > term_columns())
> + wait_for_pager();
> }
>
> strbuf_reset(&s->buf);
>
> This would significantly reduce the blast radius.
>
> Thanks.