-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
383 additions
and
346 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
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,47 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"text/tabwriter" | ||
) | ||
|
||
// TabbedStringBuilder is a wrapper around a *tabwriter.Writer that allows for efficiently building | ||
// tab-aligned strings. | ||
// This exists as *tabwriter.Writer exposes a more complicated interface which returns errors to the | ||
// caller, propagated for the underlying IOWriter. In this case we ensure that the underlying Writer is | ||
// a strings.Builder. This never returns errors therefore we can provide a simpler interface where the caller | ||
// doesn't need to consider error handling. | ||
type TabbedStringBuilder struct { | ||
sb *strings.Builder | ||
writer *tabwriter.Writer | ||
} | ||
|
||
// NewTabbedStringBuilder creates a new TabbedStringBuilder. All parameters are equivalent to those defined in tabwriter.NewWriter | ||
func NewTabbedStringBuilder(minwidth, tabwidth, padding int, padchar byte, flags uint) *TabbedStringBuilder { | ||
sb := &strings.Builder{} | ||
return &TabbedStringBuilder{ | ||
sb: sb, | ||
writer: tabwriter.NewWriter(sb, minwidth, tabwidth, padding, padchar, flags), | ||
} | ||
} | ||
|
||
// Writef formats according to a format specifier and writes to the underlying writer | ||
func (t *TabbedStringBuilder) Writef(format string, a ...any) { | ||
// should be safe ignore the error here as strings.Builder never errors | ||
_, _ = fmt.Fprintf(t.writer, format, a...) | ||
} | ||
|
||
// Write the string to the underlying writer | ||
func (t *TabbedStringBuilder) Write(a ...any) { | ||
// should be safe ignore the error here as strings.Builder never errors | ||
_, _ = fmt.Fprint(t.writer, a...) | ||
} | ||
|
||
// String returns the accumulated string. | ||
// Flush on the underlying writer is automatically called | ||
func (t *TabbedStringBuilder) String() string { | ||
// should be safe ignore the error here as strings.Builder never errors | ||
_ = t.writer.Flush() | ||
return t.sb.String() | ||
} |
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,20 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTabbedStringBuilder_TestSimple(t *testing.T) { | ||
w := NewTabbedStringBuilder(1, 1, 1, ' ', 0) | ||
w.Writef("a:\t%s", "b") | ||
assert.Equal(t, "a: b", w.String()) | ||
} | ||
|
||
func TestTabbedStringBuilderWriter_TestComplex(t *testing.T) { | ||
w := NewTabbedStringBuilder(1, 1, 1, ' ', 0) | ||
w.Writef("a:\t%s\n", "b") | ||
w.Writef("a:\t%.2f\t%d\n", 1.5, 2) | ||
assert.Equal(t, "a: b\na: 1.50 2\n", w.String()) | ||
} |
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
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
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
Oops, something went wrong.