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

Allow row painting by index #345

Closed
wants to merge 7 commits into from
Closed

Conversation

charliedrewitt
Copy link

@charliedrewitt charliedrewitt commented Dec 6, 2024

Proposed Changes

  • Add a new 'indexed row painter' which allows for painting the rows by index as well as value
  • Expose the 'sorted indices' on the table writer so that this can be used by a consumer to determine the final order of the table after sorting

Background

I am using this project to build a cli, one of the operations involves selecting a value from the table using arrow keys. Allowing painting of the rows by index makes this far easier, especially when sorting is involved.

@charliedrewitt
Copy link
Author

Will fix tests before re-opening

@charliedrewitt charliedrewitt reopened this Dec 6, 2024
Copy link

sonarqubecloud bot commented Dec 6, 2024

Please retry analysis of this Pull-Request directly on SonarQube Cloud

@coveralls
Copy link

coveralls commented Dec 6, 2024

Pull Request Test Coverage Report for Build 12203343058

Details

  • 10 of 13 (76.92%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.08%) to 99.92%

Changes Missing Coverage Covered Lines Changed/Added Lines %
table/sort.go 2 5 40.0%
Totals Coverage Status
Change from base Build 12101145541: -0.08%
Covered Lines: 3770
Relevant Lines: 3773

💛 - Coveralls

Copy link

sonarqubecloud bot commented Dec 6, 2024

Comment on lines +58 to +61
func (t *Table) SortedIndices() []int {
return t.sortedRowIndices
}

Copy link
Owner

Choose a reason for hiding this comment

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

This should not be done as the value will not be available until something gets rendered. And another render might wipe it out as well.

Comment on lines 26 to +32
// text.Colors{} to use on the entire row
type RowPainter func(row Row) text.Colors

// IndexedRowPainter is a custom function that takes a Row index as input and returns the
// text.Colors{} to use on the entire row
type IndexedRowPainter func(idx int) text.Colors

Copy link
Owner

Choose a reason for hiding this comment

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

I want to work on a generic RowPainter which does both this and more instead of introducing multiple variants like this. Give me a day please.

Copy link
Author

Choose a reason for hiding this comment

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

Sure. I implemented as a separate variant so as to not introduce a breaking change. If you don't have time, I can implement a generic version.

jedib0t added a commit that referenced this pull request Dec 9, 2024
@jedib0t
Copy link
Owner

jedib0t commented Dec 9, 2024

@charliedrewitt I've changed SetRowPainter to accept a new signature func(row table.Row, attr table.RowAttributes) text.Colors in addition to the original signature func(row table.Row) text.Colors. If you set the row painter using this new signature, you should get the real row number and the sorted row number as part of the call. Sample usage:

		tw.SetRowPainter(func(row Row, attr RowAttributes) text.Colors {
			if attr.NumberSorted == 1 {
				return text.Colors{text.Bold, text.BgHiCyan, text.FgBlack}
			} else if salary, ok := row[3].(int); ok {
				if salary > 3000 {
					return text.Colors{text.BgYellow, text.FgBlack}
				} else if salary < 2000 {
					return text.Colors{text.BgRed, text.FgBlack}
				}
			}
			return text.Colors{}
		})

Can you try the code in branch table-row-painter and let me know if that works for you?

@charliedrewitt
Copy link
Author

@jedib0t the version in table-row-painter works, thanks!

Any reason why you chose to use 1-indexed slices?

@jedib0t
Copy link
Owner

jedib0t commented Dec 9, 2024

Any reason why you chose to use 1-indexed slices?

I don't, but the "Number" terminology works better with 1-index. If I'd named it index or similar, I'd have made it 0-indexed.

@jedib0t
Copy link
Owner

jedib0t commented Dec 9, 2024

@jedib0t jedib0t closed this Dec 9, 2024
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.

3 participants