Skip to content

Commit

Permalink
✨ feat: byteutil - refactoring the byteutil.Buffer rename some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 19, 2023
1 parent 303903c commit 47bdeb4
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 94 deletions.
97 changes: 68 additions & 29 deletions byteutil/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,99 @@ package byteutil
import (
"bytes"
"fmt"
"strings"
)

// Buffer wrap and extends the bytes.Buffer
// Buffer wrap and extends the bytes.Buffer, add some useful methods
type Buffer struct {
bytes.Buffer
// custom error for testing
CloseErr error
FlushErr error
}

// NewBuffer instance
func NewBuffer() *Buffer {
return &Buffer{}
}

// WriteAny type value to buffer
func (b *Buffer) WriteAny(vs ...any) {
for _, v := range vs {
_, _ = b.Buffer.WriteString(fmt.Sprint(v))
// PrintByte to buffer, ignore error. alias of WriteByte()
func (b *Buffer) PrintByte(c byte) {
_ = b.WriteByte(c)
}

// WriteStr1 quiet write one string to buffer
func (b *Buffer) WriteStr1(s string) {
b.writeStringNl(s, false)
}

// WriteStr1Nl quiet write one string and end with newline
func (b *Buffer) WriteStr1Nl(s string) {
b.writeStringNl(s, true)
}

// writeStringNl quiet write one string and end with newline
func (b *Buffer) writeStringNl(s string, nl bool) {
_, _ = b.Buffer.WriteString(s)
if nl {
_ = b.WriteByte('\n')
}
}

// QuietWriteByte to buffer
func (b *Buffer) QuietWriteByte(c byte) {
_ = b.WriteByte(c)
// WriteStr quiet write strings to buffer
func (b *Buffer) WriteStr(ss ...string) {
b.writeStringsNl(ss, false)
}

// QuietWritef write message to buffer
func (b *Buffer) QuietWritef(tpl string, vs ...any) {
_, _ = b.WriteString(fmt.Sprintf(tpl, vs...))
// WriteStrings to buffer, ignore error.
func (b *Buffer) WriteStrings(ss []string) {
b.writeStringsNl(ss, false)
}

// Writeln write message to buffer with newline
func (b *Buffer) Writeln(ss ...string) {
b.QuietWriteln(ss...)
// WriteStringNl write message to buffer and end with newline
func (b *Buffer) WriteStringNl(ss ...string) {
b.writeStringsNl(ss, true)
}

// QuietWriteln write message to buffer with newline
func (b *Buffer) QuietWriteln(ss ...string) {
_, _ = b.WriteString(strings.Join(ss, ""))
_ = b.WriteByte('\n')
// writeStringsNl to buffer, ignore error.
func (b *Buffer) writeStringsNl(ss []string, nl bool) {
for _, s := range ss {
_, _ = b.Buffer.WriteString(s)
}
if nl {
_ = b.WriteByte('\n')
}
}

// WriteAny type value to buffer
func (b *Buffer) WriteAny(vs ...any) {
b.writeAnysWithNl(vs, false)
}

// Writeln write values to buffer and end with newline
func (b *Buffer) Writeln(vs ...any) {
b.writeAnysWithNl(vs, true)
}

// QuietWriteString to buffer
func (b *Buffer) QuietWriteString(ss ...string) {
_, _ = b.WriteString(strings.Join(ss, ""))
// WriteAnyNl type value to buffer and end with newline
func (b *Buffer) WriteAnyNl(vs ...any) {
b.writeAnysWithNl(vs, true)
}

// MustWriteString to buffer
func (b *Buffer) MustWriteString(ss ...string) {
_, err := b.WriteString(strings.Join(ss, ""))
if err != nil {
panic(err)
// WriteAnyLn type value to buffer and end with newline
func (b *Buffer) writeAnysWithNl(vs []any, nl bool) {
for _, v := range vs {
_, _ = b.Buffer.WriteString(fmt.Sprint(v))
}
if nl {
_ = b.WriteByte('\n')
}
}

// Printf quiet write message to buffer, ignore error.
func (b *Buffer) Printf(tpl string, vs ...any) {
_, _ = b.WriteString(fmt.Sprintf(tpl, vs...))
}

// ResetGet buffer string. alias of ResetAndGet()
func (b *Buffer) ResetGet() string {
return b.ResetAndGet()
Expand All @@ -71,10 +110,10 @@ func (b *Buffer) ResetAndGet() string {

// Close buffer
func (b *Buffer) Close() error {
return nil
return b.CloseErr
}

// Flush buffer
func (b *Buffer) Flush() error {
return nil
return b.FlushErr
}
8 changes: 4 additions & 4 deletions byteutil/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
func TestBuffer_WriteAny(t *testing.T) {
buf := byteutil.NewBuffer()

buf.QuietWritef("ab-%s", "c")
buf.QuietWriteByte('d')
buf.Printf("ab-%s", "c")
buf.PrintByte('d')
assert.Eq(t, "ab-cd", buf.ResetAndGet())

buf.QuietWriteString("ab", "-", "cd")
buf.MustWriteString("-ef")
buf.WriteStr("ab", "-", "cd")
buf.WriteStr1("-ef")
assert.Eq(t, "ab-cd-ef", buf.ResetAndGet())

buf.WriteAny(23, "abc")
Expand Down
18 changes: 9 additions & 9 deletions cflag/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,28 +129,28 @@ func (a *App) findCmd(name string) (*Cmd, bool) {
func (a *App) showHelp() error {
bin := a.Name
buf := strutil.NewBuffer()
buf.QuietWritef("<cyan>%s</> - %s", bin, a.Desc)
buf.Printf("<cyan>%s</> - %s", bin, a.Desc)

if a.Version != "" {
buf.QuietWritef("(Version: <cyan>%s</>)", a.Version)
buf.Printf("(Version: <cyan>%s</>)", a.Version)
}

buf.QuietWritef("\n\n<comment>Usage:</> %s <green>COMMAND</> [--Options...] [...Arguments]\n", bin)
buf.Printf("\n\n<comment>Usage:</> %s <green>COMMAND</> [--Options...] [...Arguments]\n", bin)

buf.QuietWriteln("<comment>Options:</>")
buf.QuietWriteln(" <green>-h, --help</> Display application help")
buf.QuietWriteln("\n<comment>Commands:</>")
buf.WriteStr1Nl("<comment>Options:</>")
buf.WriteStr1Nl(" <green>-h, --help</> Display application help")
buf.WriteStr1Nl("\n<comment>Commands:</>")

sort.Strings(a.names)
for _, name := range a.names {
c := a.cmds[name]
name := strutil.PadRight(name, " ", a.NameWidth)
buf.QuietWritef(" <green>%s</> %s\n", name, strutil.UpperFirst(c.Desc))
buf.Printf(" <green>%s</> %s\n", name, strutil.UpperFirst(c.Desc))
}

name := strutil.PadRight("help", " ", a.NameWidth)
buf.QuietWritef(" <green>%s</> Display application help\n", name)
buf.QuietWritef("\nUse \"<cyan>%s COMMAND --help</>\" for about a command\n", bin)
buf.Printf(" <green>%s</> Display application help\n", name)
buf.Printf("\nUse \"<cyan>%s COMMAND --help</>\" for about a command\n", bin)

if a.AfterHelpBuild != nil {
a.AfterHelpBuild(buf)
Expand Down
22 changes: 11 additions & 11 deletions cflag/cflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,21 +436,21 @@ func (c *CFlags) showHelp(err error) {
buf := new(strutil.Buffer)

if err != nil {
buf.QuietWritef("<error>ERROR:</> %s\n", err.Error())
buf.Printf("<error>ERROR:</> %s\n", err.Error())
} else {
buf.QuietWritef("<cyan>%s</>\n\n", c.helpDesc())
buf.Printf("<cyan>%s</>\n\n", c.helpDesc())
}

buf.QuietWritef("<comment>Usage:</> %s [--Options...] [...CliArgs]\n", binName)
buf.QuietWriteString("<comment>Options:</>\n")
buf.Printf("<comment>Usage:</> %s [--Options...] [...CliArgs]\n", binName)
buf.WriteStr("<comment>Options:</>\n")

// render options help
c.renderOptionsHelp(buf)

if len(c.bindArgs) > 0 {
buf.QuietWriteString("\n<comment>CliArgs:</>\n")
buf.WriteStr1("\n<comment>CliArgs:</>\n")
for _, arg := range c.bindArgs {
buf.QuietWritef(
buf.Printf(
" <green>%s</> %s\n",
strutil.PadRight(arg.Name, " ", c.argWidth),
arg.HelpDesc(),
Expand All @@ -459,13 +459,13 @@ func (c *CFlags) showHelp(err error) {
}

if c.LongHelp != "" {
buf.QuietWriteln("\n<comment>Help:</>")
buf.QuietWriteln(strings.Trim(c.LongHelp, "\n"))
buf.WriteStr1Nl("\n<comment>Help:</>")
buf.WriteStr1Nl(strings.Trim(c.LongHelp, "\n"))
}

if c.Example != "" {
buf.QuietWriteln("\n<comment>Examples:</>")
buf.QuietWriteString(strings.Trim(c.Example, "\n"))
buf.WriteStr1Nl("\n<comment>Examples:</>")
buf.WriteStr1(strings.Trim(c.Example, "\n"))
}

color.Println(strutil.Replaces(buf.String(), helpVars))
Expand Down Expand Up @@ -509,6 +509,6 @@ func (c *CFlags) renderOptionsHelp(buf *strutil.Buffer) {
}
}

buf.QuietWriteln(b.String())
buf.WriteStr1(b.String())
})
}
10 changes: 5 additions & 5 deletions strutil/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
func TestBuffer_WriteAny(t *testing.T) {
buf := strutil.NewBuffer()

buf.QuietWritef("ab-%s", "c")
buf.QuietWriteByte('d')
buf.Printf("ab-%s", "c")
buf.PrintByte('d')
assert.Eq(t, "ab-cd", buf.ResetAndGet())

buf.QuietWriteString("ab", "-", "cd")
buf.MustWriteString("-ef")
assert.Eq(t, "ab-cd-ef", buf.ResetAndGet())
buf.WriteStr("ab", "-", "cd")
buf.WriteStr1("-ef")
assert.Eq(t, "ab-cd-ef", buf.ResetGet())

buf.WriteAny(23, "abc")
assert.Eq(t, "23abc", buf.ResetAndGet())
Expand Down
38 changes: 4 additions & 34 deletions testutil/buffer.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,13 @@
package testutil

import (
"bytes"
"fmt"
"github.com/gookit/goutil/byteutil"
)

// Buffer wrap and extends the bytes.Buffer
type Buffer struct {
bytes.Buffer
}
type Buffer = byteutil.Buffer

// NewBuffer instance
func NewBuffer() *Buffer {
return &Buffer{}
}

// WriteString rewrite
func (b *Buffer) WriteString(ss ...string) {
for _, s := range ss {
_, _ = b.Buffer.WriteString(s)
}
}

// WriteAny method
func (b *Buffer) WriteAny(vs ...any) {
for _, v := range vs {
_, _ = b.Buffer.WriteString(fmt.Sprint(v))
}
}

// Writeln method
func (b *Buffer) Writeln(s string) {
_, _ = b.Buffer.WriteString(s)
_ = b.Buffer.WriteByte('\n')
}

// ResetAndGet buffer string.
func (b *Buffer) ResetAndGet() string {
s := b.String()
b.Reset()
return s
func NewBuffer() *byteutil.Buffer {
return byteutil.NewBuffer()
}
4 changes: 2 additions & 2 deletions testutil/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
func TestNewBuffer(t *testing.T) {
buf := testutil.NewBuffer()

buf.WriteString("ab", "-", "cd")
assert.Eq(t, "ab-cd", buf.ResetAndGet())
buf.WriteStr("ab", "-", "cd")
assert.Eq(t, "ab-cd", buf.ResetGet())

buf.WriteAny(23, "abc")
assert.Eq(t, "23abc", buf.ResetAndGet())
Expand Down

0 comments on commit 47bdeb4

Please sign in to comment.