Extract generic row printer that adjusts itself for receiving terminal#82
Extract generic row printer that adjusts itself for receiving terminal#82
Conversation
This makes the approach from `pr list` reusable across other commands that may benefit from table-based output, e.g. `issue list` or `pr status` The idea is: instantiate a printer, connect it to stdout, feed it some data, and it does the rest: colored, truncated column output that fits into a terminal, or tab-delimited output (no color, no truncation) for scripts.
probablycorey
left a comment
There was a problem hiding this comment.
This is great and surprisingly simple! It looks like a test is failing, but once that is fixed I'm good to merge this!
utils/table_printer.go
Outdated
| if t.IsTTY { | ||
| truncVal := truncate(t.colWidths[col], val) | ||
| if col != lastCol { | ||
| truncVal = fmt.Sprintf("%-*s", t.colWidths[col], truncVal) |
There was a problem hiding this comment.
I always find the printf patterns cryptic, a comment explaining it would helpful!
command/pr.go
Outdated
| } | ||
| table.SetContentWidth(0, len(strconv.Itoa(pr.Number))+1) | ||
| table.SetContentWidth(1, len(pr.Title)) | ||
| table.SetContentWidth(2, len(pr.HeadLabel())) |
There was a problem hiding this comment.
I've been thinking about the "set width, fit columns, write row" pattern and wondering if a simplified interface of "create row, write table" pattern might be better? This way you just think about creating the rows and leave everything else up to the table struct.
I initially wasn't going to post this because it's not a big deal, but I wanted to at least mention it.
There was a problem hiding this comment.
@probablycorey Good idea! I've now redesigned the printer API to be basically:
- AddField;
- EndRow;
- Render.
|
Love it, want it, need it! 🚢 |
This makes the approach from
pr listreusable across other commands that may benefit from table-based output, e.g.issue listorpr statusThe idea is: instantiate a printer, connect it to stdout, feed it some data, and it does the rest: colored, truncated column output that fits into a terminal, or tab-delimited output (no color, no truncation) for scripts.