Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add header support to tableprinter #139

Merged
merged 4 commits into from
Oct 19, 2023
Merged

Add header support to tableprinter #139

merged 4 commits into from
Oct 19, 2023

Conversation

heaths
Copy link
Contributor

@heaths heaths commented Oct 12, 2023

Related to cli/cli#8090

@samcoe
Copy link
Contributor

samcoe commented Oct 16, 2023

@heaths While in the table header PR for gh there was talk about ways to enforce that tables get headers I think in go-gh we should not have the same requirement and rather allow flexibility to the users to decide if they want to follow the same styling as gh.

How I am envisioning this working is that the tableprinter constructor does not change as illustrated by this PR but instead we update the TablePrinter interface to include a method to add headers:

type TablePrinter interface {
	AddField(string, ...fieldOption)
        AddHeaders([]string, ...fieldOption)
	EndRow()
	Render() error
}

What are your thoughts?

@heaths
Copy link
Contributor Author

heaths commented Oct 16, 2023

That's fine. They main points were to:

  1. Handle cases were we need access to privates for calculated e.g., underlines (if we go that route).
  2. At least provide address to some consistency for extensions.

The latter makes me wonder if we either:

  1. Declare a default style as a public.
  2. Just apply the style by default.

The problem is that there is no style support in this module currently, which is why I passed it in.

@samcoe
Copy link
Contributor

samcoe commented Oct 17, 2023

Hmm my initial thought was that we might just add in a code comment and or code example showing how to achieve the default look achieved by gh rather than actually adding style specific code inside this package. Keeping the styling decoupled from the printing was one of the goals Mislav was trying to achieve when architecting this package.

@heaths
Copy link
Contributor Author

heaths commented Oct 17, 2023

That's fine, too. I was mainly going for consistency - at least making it easily possible to be consistent - for extensions.

@heaths
Copy link
Contributor Author

heaths commented Oct 18, 2023

I made the changes and, IMO, the tables look nice in the CLI (using "white+du"):

image

There is a caveat, though. By padding the last header column, TTY table assertions need to be padded in the header row, which I suspect many CLI devs won't expect. It's solvable, but by exposing some things publicly that might seem odd in isolation. For example, a separate fieldOption could be defined where the callback took a bool whether the column is the last column or something. The CLI color function could then use that to trim the string if the ColorScheme isn't enabled. I have a few other similar ideas.

The underlines really make it look like a header, and even looks nice with a full background color:

image

But perhaps it's not worth the maintenance hassle. Thoughts? if we don't try to support underlines or other background styles, there's no reason not to trim the last column header, which is what happens currently.

/cc @williammartin

@heaths heaths marked this pull request as ready for review October 18, 2023 02:20
Resolves PR feedback
@heaths
Copy link
Contributor Author

heaths commented Oct 18, 2023

To note, if you decide the support for full-column styles isn't worth the maintenance hassle or workaround, no change should be needed to this module and the other PR is pretty much done and ready for review.

Copy link
Contributor

@samcoe samcoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@heaths I have a different proposal for supporting full column styles which I think is a bit less clever than the approach you are thinking. What about introducing a WithPadding method function that is very similar to WithTruncate? We could then add a padding function to the text package which would pad to string as necessary. This would allow for the most amount of customization and follow a pattern that we have already laid out. Obviously this could allow our users to create disproportionate looking tables, but I would say that is their business.

@@ -15,6 +15,7 @@ import (
type fieldOption func(*tableField)

type TablePrinter interface {
AddHeaders([]string, ...fieldOption)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok kind of an odd question, but should this be AddHeader? I don't have an opinion, just linguistically wondering what is correct. Are we adding a table header, or are we adding table headers?

@heaths
Copy link
Contributor Author

heaths commented Oct 18, 2023

I could play around with something like that, which is more or less one idea I was considering. That said, it's not even worth it unless you and the others prefer the underlined table header. Certainly easier - still not bad looking, but then relies entirely on color - to only use a different, IMO dimmer, color for the header.

@heaths
Copy link
Contributor Author

heaths commented Oct 18, 2023

I suppose one interesting use case for a WithPadding func is to allow people to center columns - header or otherwise.

@heaths
Copy link
Contributor Author

heaths commented Oct 18, 2023

I went ahead and made the changes. It's not a lot of work, and I still personally believe the TableHeader style I defined in the CLI looks nice, but certainly open to changes, of course. As I mentioned above, if nothing else, WithPadding can be used to center column headers, of which my new test shows an example.

cli/cli#8090 already takes advantage of this, so the PR will fail, of course. But I wanted to show how I'm using it, basing it on ColorScheme.Enabled() (newly exposed) so I can mitigate the right padding on the last column so that all existing tests and - for better maintainability - all new tests don't have to account for it.

Copy link
Contributor

@samcoe samcoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@heaths This is great. I pushed a small commit to extract text.PadRight function that is reusable for other users. I am glad we are exposing the WithPadding functionality as gh is not the only consumer of this package.

@samcoe samcoe enabled auto-merge (squash) October 19, 2023 12:46
@samcoe samcoe merged commit ec1e1cd into cli:trunk Oct 19, 2023
6 checks passed
@heaths heaths deleted the issue8090 branch October 19, 2023 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants