-
Notifications
You must be signed in to change notification settings - Fork 0
Generic table output #13
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d04a229
Add table printer implementation with dynamic width calculation and t…
nordlundj ec2cadf
Refactor instance listing to use table package for improved formatting
nordlundj 378011c
Refactor node listing to use table package for improved output format…
nordlundj 3cb3656
Refactor plugin listing to use table package for improved output form…
nordlundj a710daa
Refactor plans command to display available plans in a formatted table
nordlundj f0c75fa
Refactor team listing to use table package for improved output format…
nordlundj f510605
Refactor VPC listing to use table package for improved output formatting
nordlundj eab6433
Fix separator width calculation in table printer
nordlundj 86691ee
Merge branch 'main' into generic-table-output
dentarg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package table | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
| ) | ||
|
|
||
| // Column represents a column in the table | ||
| type Column struct { | ||
| Header string | ||
| Width int | ||
| } | ||
|
|
||
| // Printer handles dynamic table printing with automatic width calculation | ||
| type Printer struct { | ||
| columns []Column | ||
| rows [][]string | ||
| writer io.Writer | ||
| } | ||
|
|
||
| // New creates a new table printer | ||
| func New(writer io.Writer, headers ...string) *Printer { | ||
| columns := make([]Column, len(headers)) | ||
| for i, header := range headers { | ||
| columns[i] = Column{ | ||
| Header: header, | ||
| Width: len(header), | ||
| } | ||
| } | ||
| return &Printer{ | ||
| columns: columns, | ||
| rows: make([][]string, 0), | ||
| writer: writer, | ||
| } | ||
| } | ||
|
|
||
| // AddRow adds a row of data to the table | ||
| func (p *Printer) AddRow(values ...string) error { | ||
| if len(values) != len(p.columns) { | ||
| return fmt.Errorf("expected %d columns, got %d", len(p.columns), len(values)) | ||
| } | ||
|
|
||
| // Update column widths based on this row's values | ||
| for i, value := range values { | ||
| if len(value) > p.columns[i].Width { | ||
| p.columns[i].Width = len(value) | ||
| } | ||
| } | ||
|
|
||
| p.rows = append(p.rows, values) | ||
| return nil | ||
| } | ||
|
|
||
| // Print outputs the table with calculated column widths | ||
| func (p *Printer) Print() { | ||
| // Add padding to widths | ||
| for i := range p.columns { | ||
| p.columns[i].Width += 2 | ||
| } | ||
|
|
||
| // Build format string | ||
| formatParts := make([]string, len(p.columns)) | ||
| for i, col := range p.columns { | ||
| formatParts[i] = fmt.Sprintf("%%-%ds", col.Width) | ||
| } | ||
| format := strings.Join(formatParts, " ") + "\n" | ||
|
|
||
| // Print header | ||
| headers := make([]interface{}, len(p.columns)) | ||
| separators := make([]interface{}, len(p.columns)) | ||
| for i, col := range p.columns { | ||
| headers[i] = col.Header | ||
| separators[i] = strings.Repeat("-", col.Width) | ||
| } | ||
| fmt.Fprintf(p.writer, format, headers...) | ||
| fmt.Fprintf(p.writer, format, separators...) | ||
|
|
||
| // Print rows | ||
| for _, row := range p.rows { | ||
| rowInterface := make([]interface{}, len(row)) | ||
| for i, v := range row { | ||
| rowInterface[i] = v | ||
| } | ||
| fmt.Fprintf(p.writer, format, rowInterface...) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.