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

feat: used go-pretty to improve table formatting on screen width #414

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

Aerex
Copy link
Collaborator

@Aerex Aerex commented Nov 11, 2024

Context

The purpose of this PR is to improve the table formatting on the columns to wrap when the text when it reaches an estimated max column width. The max col width is calculated by excluding the padding and applying the remaining space evenly among the columns. Any columns that are expected to be wider than the others (eg. ID and Description) will given more space than the others

The PR uses the go-pretty library to manage the column text wrapping, and max column width and coloring.

@Aerex Aerex requested a review from steveclay as a code owner November 11, 2024 21:02
@@ -38,7 +38,7 @@ func TestEmptyHeaderTable(t *testing.T) {
testTable.Add("row1", "row2")
testTable.Print()
assert.Contains(t, buf.String(), "row1")
assert.Equal(t, " \nrow1 row2\n", buf.String())
assert.Equal(t, "\nrow1 row2\n", buf.String())
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Callout: Removing the tab space does not change the output visually.

image

@@ -79,7 +79,7 @@ func TestNotEnoughRowEntires(t *testing.T) {
testTable.Add("", "row2")
testTable.Print()
assert.Contains(t, buf.String(), "row1")
assert.Equal(t, "col1 col2\nrow1 \n row2\n", buf.String())
assert.Equal(t, "col1 col2\nrow1\n row2\n", buf.String())
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Callout: Same as above

image

@Aerex Aerex force-pushed the feat/better-formating-table-go-pretty branch from 0fa5825 to b1ff345 Compare November 12, 2024 18:09
@Aerex
Copy link
Collaborator Author

Aerex commented Nov 12, 2024

Ready for review @steveclay

@steveclay steveclay changed the title feat: used go-pretty to imporve table formatting on screen width feat: used go-pretty to improve table formatting on screen width Nov 14, 2024
columnConfig[i].WidthMax = t.maxSizes[i]
remainingSpace -= t.maxSizes[i]
} else {
remainingSpace -= maxColWidth
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wondering if there can be a scenario that remainingSpace becomes 0 or less.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good question. I don't think there is a valid scenario since that would mean the user would need to have a lot of columns. For instance, for terminal width of 80, the user would need to create more than 27 columns (27 * 3 > 80) for there to be negative remainingSpace.

tbl.Render()
}

func (t *PrintableTable) createColumnConfigs() []table.ColumnConfig {
Copy link
Collaborator

@steveclay steveclay Nov 22, 2024

Choose a reason for hiding this comment

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

It would be good to add some more testing to explicitly try out some odd column headings to ensure there's no breaking conditions. I don't have any in mind, but would like to see if we can test some boundary conditions here.

For example, these paths are not unit tested:
image

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added more unit tests for various scenarios.

@Aerex Aerex requested a review from steveclay November 26, 2024 18:09
terminalWidth = 80
}

testTerminalWidth, envSet := os.LookupEnv("TEST_TERMINAL_WIDTH")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Callout: Added test environment variable to test variable terminal width in unit tests

terminalWidth = terminalWidth()
// total amount padding space that a row will take up
totalPaddingSpace = (colCount - 1) * minSpace
remainingSpace = max(0, terminalWidth-totalPaddingSpace)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Callout: It is unlikely for the remainingSpace to be negative but if that does happen we will set it to 0.

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