mirror of
https://github.com/jimeh/undent.git
synced 2026-02-18 19:46:39 +00:00
feat(print) add Print, Printf, Fprint, and Fprintf functions
This commit is contained in:
5
go.mod
5
go.mod
@@ -2,4 +2,7 @@ module github.com/jimeh/undent
|
|||||||
|
|
||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require github.com/stretchr/testify v1.6.1
|
require (
|
||||||
|
github.com/rhysd/go-fakeio v1.0.0
|
||||||
|
github.com/stretchr/testify v1.6.1
|
||||||
|
)
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
|
|||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/rhysd/go-fakeio v1.0.0 h1:+TjiKCOs32dONY7DaoVz/VPOdvRkPfBkEyUDIpM8FQY=
|
||||||
|
github.com/rhysd/go-fakeio v1.0.0/go.mod h1:joYxF906trVwp2JLrE4jlN7A0z6wrz8O6o1UjarbFzE=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
|||||||
36
undent.go
36
undent.go
@@ -4,6 +4,7 @@ package undent
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -60,3 +61,38 @@ func String(s string) string {
|
|||||||
func Stringf(format string, a ...interface{}) string {
|
func Stringf(format string, a ...interface{}) string {
|
||||||
return fmt.Sprintf(String(format), a...)
|
return fmt.Sprintf(String(format), a...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Print will undent any strings arguments before passing them to fmt.Print.
|
||||||
|
func Print(a ...interface{}) (n int, err error) {
|
||||||
|
return fmt.Print(undentInterfaces(a)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Printf will undent the given format string before passing it and all
|
||||||
|
// arguments to fmt.Printf.
|
||||||
|
func Printf(format string, a ...interface{}) (n int, err error) {
|
||||||
|
return fmt.Printf(String(format), a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fprint will undent any string arguments before passing them to fmt.Fprint.
|
||||||
|
func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
|
||||||
|
return fmt.Fprint(w, undentInterfaces(a)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fprintf will undent the given format string before passing it and all
|
||||||
|
// arguments to fmt.Fprintf.
|
||||||
|
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
|
||||||
|
return fmt.Fprintf(w, String(format), a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func undentInterfaces(a []interface{}) []interface{} {
|
||||||
|
var r []interface{}
|
||||||
|
|
||||||
|
for _, v := range a {
|
||||||
|
if s, ok := v.(string); ok {
|
||||||
|
v = String(s)
|
||||||
|
}
|
||||||
|
r = append(r, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package undent_test
|
package undent_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/jimeh/undent"
|
"github.com/jimeh/undent"
|
||||||
@@ -60,3 +61,57 @@ func ExampleStringf() {
|
|||||||
// "hello": "world"
|
// "hello": "world"
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExamplePrint() {
|
||||||
|
undent.Print(`
|
||||||
|
{
|
||||||
|
"hello": "world"
|
||||||
|
}`,
|
||||||
|
)
|
||||||
|
// Output:
|
||||||
|
// {
|
||||||
|
// "hello": "world"
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExamplePrintf() {
|
||||||
|
undent.Printf(`
|
||||||
|
{
|
||||||
|
"hello": "%s"
|
||||||
|
}`,
|
||||||
|
"world",
|
||||||
|
)
|
||||||
|
// Output:
|
||||||
|
// {
|
||||||
|
// "hello": "world"
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleFprint() {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
undent.Fprint(&buf, `
|
||||||
|
{
|
||||||
|
"hello": "world"
|
||||||
|
}`,
|
||||||
|
)
|
||||||
|
fmt.Println(buf.String())
|
||||||
|
// Output:
|
||||||
|
// {
|
||||||
|
// "hello": "world"
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleFprintf() {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
undent.Fprintf(&buf, `
|
||||||
|
{
|
||||||
|
"hello": "%s"
|
||||||
|
}`,
|
||||||
|
"world",
|
||||||
|
)
|
||||||
|
fmt.Println(buf.String())
|
||||||
|
// Output:
|
||||||
|
// {
|
||||||
|
// "hello": "world"
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package undent
|
package undent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/rhysd/go-fakeio"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
var stringTestCases = []struct {
|
var stringTestCases = []struct {
|
||||||
@@ -443,6 +446,62 @@ func TestStringf(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPrint(t *testing.T) {
|
||||||
|
for _, tt := range stringTestCases {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := fakeio.Stdout().Do(func() {
|
||||||
|
Print(tt.s, 5, tt.s)
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.IsType(t, "", got)
|
||||||
|
assert.Equal(t, tt.want+"5"+tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrintf(t *testing.T) {
|
||||||
|
for _, tt := range stringfTestCases {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := fakeio.Stdout().Do(func() {
|
||||||
|
Printf(tt.s, tt.a...)
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.IsType(t, "", got)
|
||||||
|
assert.Equal(t, tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFprint(t *testing.T) {
|
||||||
|
for _, tt := range stringTestCases {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
Fprint(&buf, tt.s, 5, tt.s)
|
||||||
|
got := buf.String()
|
||||||
|
|
||||||
|
assert.IsType(t, "", got)
|
||||||
|
assert.Equal(t, tt.want+"5"+tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFprintf(t *testing.T) {
|
||||||
|
for _, tt := range stringfTestCases {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
Fprintf(&buf, tt.s, tt.a...)
|
||||||
|
got := buf.String()
|
||||||
|
|
||||||
|
assert.IsType(t, "", got)
|
||||||
|
assert.Equal(t, tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkBytes(b *testing.B) {
|
func BenchmarkBytes(b *testing.B) {
|
||||||
for _, tt := range stringTestCases {
|
for _, tt := range stringTestCases {
|
||||||
b.Run(tt.name, func(b *testing.B) {
|
b.Run(tt.name, func(b *testing.B) {
|
||||||
|
|||||||
Reference in New Issue
Block a user