Subject: Re: [PATCH v4 5/8] fetch: introduce `display_format` enum

From: Patrick Steinhardt <[email-masked]>
Date: Wed, 10 May 2023 14:35:03 +0200

On Tue, May 09, 2023 at 01:19:29PM -0700, Junio C Hamano wrote:
> Patrick Steinhardt <[email-masked]> writes:
[snip]
> > -	display_state->refcol_width = 10;
> > -	for (rm = ref_map; rm; rm = rm->next) {
> > -		int width;
> > +	switch (display_state->format) {
> > +	case DISPLAY_FORMAT_FULL:
> > +	case DISPLAY_FORMAT_COMPACT: {
> > +		struct ref *rm;
> >  
> > -		if (rm->status == REF_STATUS_REJECT_SHALLOW ||
> > -		    !rm->peer_ref ||
> > -		    !strcmp(rm->name, "HEAD"))
> > -			continue;
> > +		display_state->refcol_width = 10;
> > +		for (rm = ref_map; rm; rm = rm->next) {
> > +			int width;
> >  
> > -		width = refcol_width(rm, display_state->compact_format);
> > +			if (rm->status == REF_STATUS_REJECT_SHALLOW ||
> > +			    !rm->peer_ref ||
> > +			    !strcmp(rm->name, "HEAD"))
> > +				continue;
> >  
> > -		/*
> > -		 * Not precise calculation for compact mode because '*' can
> > -		 * appear on the left hand side of '->' and shrink the column
> > -		 * back.
> > -		 */
> > -		if (display_state->refcol_width < width)
> > -			display_state->refcol_width = width;
> > +			width = refcol_width(rm, display_state->format == DISPLAY_FORMAT_COMPACT);
> > +
> > +			/*
> > +			 * Not precise calculation for compact mode because '*' can
> > +			 * appear on the left hand side of '->' and shrink the column
> > +			 * back.
> > +			 */
> > +			if (display_state->refcol_width < width)
> > +				display_state->refcol_width = width;
> > +		}
> > +
> > +		break;
> > +	}
> > +	default:
> > +		BUG("unexpected display format %d", display_state->format);
> >  	}
> 
> Due to reindentation, the patch is noisier than what it does (which
> should be "nothing, other than allowing another value in the .format
> member").
> 
> It makes me wonder if it would make it easier to read to move the
> bulk of this code to a helper function.  If we are to give a name to
> what is being done in the above hunk, what would it be?  It computes
> display->refcol_width in which all records would fit, but presumably
> if we are to add more things to be shown per ref and align them in a
> simlar way, we would compute widths for these other things there as
> well.  Perhaps compute_display_alignment() or somesuch?

The code already has such a function and calls it `refcol_width()`, but
it only computes the width for a single reference. The most natural
thing to do here would thus be to merge the loop over the references
into that function. This would also allow us to skip some weirdness,
like the fact that we skip some references inside `refcol_width()` while
we skip other references in the `ref_map` loop.

This refactoring is also quite noisy, but it makes the code simpler
overall and will make the patch introducing the enum less so.

Patrick