X Tutup
Skip to content

Extract generic row printer that adjusts itself for receiving terminal#82

Merged
mislav merged 5 commits intomasterfrom
table-output
Nov 21, 2019
Merged

Extract generic row printer that adjusts itself for receiving terminal#82
mislav merged 5 commits intomasterfrom
table-output

Conversation

@mislav
Copy link
Copy Markdown
Contributor

@mislav mislav commented Nov 15, 2019

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.

Screen Shot 2019-11-15 at 7 40 46 PM

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.
Copy link
Copy Markdown
Contributor

@probablycorey probablycorey left a comment

Choose a reason for hiding this comment

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

This is great and surprisingly simple! It looks like a test is failing, but once that is fixed I'm good to merge this!

if t.IsTTY {
truncVal := truncate(t.colWidths[col], val)
if col != lastCol {
truncVal = fmt.Sprintf("%-*s", t.colWidths[col], truncVal)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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()))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@probablycorey Good idea! I've now redesigned the printer API to be basically:

  1. AddField;
  2. EndRow;
  3. Render.

@probablycorey
Copy link
Copy Markdown
Contributor

Love it, want it, need it! 🚢

@mislav mislav merged commit bad21ea into master Nov 21, 2019
@mislav mislav deleted the table-output branch November 21, 2019 13:15
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

X Tutup