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

Fix table spacing and omitEmpty option #3139

Merged
merged 15 commits into from
Mar 20, 2024
Merged

Fix table spacing and omitEmpty option #3139

merged 15 commits into from
Mar 20, 2024

Conversation

MDrakos
Copy link
Member

@MDrakos MDrakos commented Feb 23, 2024

BugDX-2578 Incorrect vertical table spacing with long key names

As we discussed in stand, it might be worthwhile to have a separate vertical table representation rather than trying to have a vertical toggle in the standard table representation. We could do this within the current library or look into the charm family of libraries to see if we can get something 3rd party to work.

@github-actions github-actions bot changed the base branch from master to version/0-44-0-RC1 February 23, 2024 23:46
internal/output/plain.go Outdated Show resolved Hide resolved
internal/output/plain.go Outdated Show resolved Hide resolved
@MDrakos MDrakos marked this pull request as ready for review March 1, 2024 19:01
@MDrakos MDrakos requested a review from Naatan March 1, 2024 19:06
internal/output/plain.go Outdated Show resolved Hide resolved
internal/output/plain.go Outdated Show resolved Hide resolved
internal/output/plain.go Outdated Show resolved Hide resolved
internal/table/table.go Outdated Show resolved Hide resolved
@MDrakos
Copy link
Member Author

MDrakos commented Mar 18, 2024

I expect we may want to iterate on this but I wanted to try a different approach where we bind the header to the row content. This fixes the issue where the headers and rows could get misaligned. The new function for printing the vertical table does have some repeated logic but I think this starts to take us in the direction we ultimately want to go where we have a separate path for vertical tables rather than trying to have our existing tables with a vertical option.

@MDrakos MDrakos requested a review from Naatan March 18, 2024 20:31
Comment on lines 305 to 307
if funk.Contains(field.opts, string(OmitEmpty)) && (stringValue == "" || stringValue == nilText) {
stringValue = ""
}
Copy link
Member

Choose a reason for hiding this comment

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

This feels inappropriate. OmitEmpty means "don't print if it's empty". This is doing the exact opposite.

The old implementation is definitely awkward, because if we don't omit the column for each row you're going to run into some weird results. But then further down we also have shiftColsVal which allows for columns to span based on the shiftCols= param. Point is, this may all be by design, and this change you're making is backward incompatible. So either we need to audit existing use of this code and ensure it is compatible with your change, or we don't make any backward incompatible changes.

Copy link
Member

Choose a reason for hiding this comment

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

Note this is for the horizontal table mechanic, I suspect you can just revert this back to what it looked like before this PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

Whoops, that's leftover from my initial implementation. It's been reverted now.

internal/output/plain.go Outdated Show resolved Hide resolved
Comment on lines +107 to +110
var verticalHeaderWidth int
if len(colWidths) > 0 && t.Vertical {
verticalHeaderWidth = colWidths[0]
}
Copy link
Member

Choose a reason for hiding this comment

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

This is only used on rescaleColumns and is based on inputs that are also passed to that function. So I suggest moving this logic inside rescaleColumns.

But also; what is the rationale here? Comment explaining this would be good.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a comment, but left this here. There is a call further down to an equalizeWidths function that changes the column widths. Before that call the value of the first column is the original header width, which we want to respect when rescaling. I think it makes sense to preserve this, equalize all of the columns, and then ensure the header width is respected when rescaling, but I'm open to move this somewhere else or take a different approach.

Copy link
Member

Choose a reason for hiding this comment

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

Ok. I think you missed the first part of my comment though:

This is only used on rescaleColumns and is based on inputs that are also passed to that function. So I suggest moving this logic inside rescaleColumns.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah sorry, I didn't explain well exactly what's happening here.

The call to equalizeWidths changes the actual values in the colWidths slice. So we have to grab the column width of the first entry in the slice at this point as this is the max width of the headers column. So verticalHeaderWidth can be a different value from colWidths[0] after the equalizeWidths call.

colWidths[n] = int(float64(colWidths[n]) * multiplier)
}

// Account for floats that got rounded
if len(colWidths) > 0 {
colWidths[len(colWidths)-1] += targetTotal - mathutils.Total(colWidths...)
}

// If vertical, respect the header width
if vertical && len(colWidths) > 0 && colWidths[0] < verticalHeaderWidth {
Copy link
Member

Choose a reason for hiding this comment

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

It looks like verticalHeaderWidth is either the value of colWidths[0] or just 0. So either this conditional doesn't trigger, or it triggers if colWidths[0] < 0, which feels inappropriate.

Copy link
Member Author

Choose a reason for hiding this comment

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

As in my other comment, verticalHeaderWidth is the value before equalization so it can differ from colWidths[0]. I'll add a comment here as well.

@MDrakos MDrakos requested a review from Naatan March 19, 2024 21:37
Copy link
Member

@Naatan Naatan left a comment

Choose a reason for hiding this comment

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

Looks good, but still think we can move this code:

#3139 (comment)

@MDrakos MDrakos requested a review from Naatan March 19, 2024 23:42
@MDrakos MDrakos merged commit d47d8ff into version/0-44-0-RC1 Mar 20, 2024
7 checks passed
@MDrakos MDrakos deleted the DX-2578 branch March 20, 2024 23:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants