Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves cli#3488
- Loading branch information
Showing
4 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,49 @@ | ||
package export | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
"text/tabwriter" | ||
) | ||
|
||
type tableFormatter struct { | ||
current *table | ||
writer io.Writer | ||
} | ||
|
||
type table struct { | ||
tabwriter *tabwriter.Writer | ||
} | ||
|
||
// Initialize a new tabwriter. | ||
func (f *tableFormatter) Table() string { | ||
f.current = &table{ | ||
// Write directly to the same writer as template execution | ||
// since writing to a buffer isn't getting flushed properly. | ||
tabwriter: tabwriter.NewWriter(f.writer, 0, 1, 1, ' ', 0), | ||
} | ||
return "" | ||
} | ||
|
||
// Write a row of string fields. Formatting must be performed before passing to Row. | ||
func (f *tableFormatter) Row(fields ...string) (string, error) { | ||
if f.current == nil { | ||
return "", fmt.Errorf("no current table. use '{{table}}' to start a table") | ||
} | ||
row := strings.Join(fields, "\t") | ||
if _, err := f.current.tabwriter.Write([]byte(row + "\n")); err != nil { | ||
return "", fmt.Errorf("failed to write row: %v", err) | ||
} | ||
return "", nil | ||
} | ||
|
||
// End the table and flush rows, adjusting for columns' widths using elastic tabstops. | ||
func (f *tableFormatter) EndTable() (string, error) { | ||
if f.current == nil { | ||
return "", fmt.Errorf("no current table. use '{{table}}' to start a table") | ||
} | ||
f.current.tabwriter.Flush() | ||
f.current = nil | ||
return "", nil | ||
} |
This file contains 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,26 @@ | ||
package export | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func Test_Row_withoutTable(t *testing.T) { | ||
formatter := &tableFormatter{ | ||
current: nil, | ||
writer: &strings.Builder{}, | ||
} | ||
if _, err := formatter.Row("col"); err == nil { | ||
t.Errorf("expected error, got nil") | ||
} | ||
} | ||
|
||
func Test_EndTable_withoutTable(t *testing.T) { | ||
formatter := &tableFormatter{ | ||
current: nil, | ||
writer: &strings.Builder{}, | ||
} | ||
if _, err := formatter.EndTable(); err == nil { | ||
t.Errorf("expected error, got nil") | ||
} | ||
} |
This file contains 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 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