Browse Source

add a lot

master v0.0.42
RealXLFD 3 weeks ago
parent
commit
c010d9f75d
  1. 20
      cli/logger/index.go
  2. 4
      go.mod
  3. 9
      go.sum
  4. 10
      utils/str/index.go
  5. 7
      utils/str/str_test.go
  6. 8
      utils/str/template.go

20
cli/logger/index.go

@ -55,10 +55,10 @@ func New(opts ...Option) *Logger {
return logger
}
func (l Logger) Info(msgs ...string) {
func (l Logger) Info(msgs ...string) *Logger {
msg := str.Join(msgs...)
if l.Level < LevelInfo {
return
return &l
}
timeStamp := ""
if !l.HideTime {
@ -68,12 +68,13 @@ func (l Logger) Info(msgs ...string) {
msg = str.TrimEnd(msg, l.LengthLimit)
}
OutputFunc(fmt.Sprint(str.Join(timeStamp, argInfo, " "), msg))
return &l
}
func (l Logger) Debug(msgs ...string) {
func (l Logger) Debug(msgs ...string) *Logger {
msg := str.Join(msgs...)
if l.Level < LevelDebug {
return
return &l
}
timeStamp := ""
if !l.HideTime {
@ -83,12 +84,13 @@ func (l Logger) Debug(msgs ...string) {
msg = str.TrimEnd(msg, l.LengthLimit)
}
OutputFunc(fmt.Sprint(str.Join(timeStamp, argDebug, " "), msg))
return &l
}
func (l Logger) Warn(msgs ...string) {
func (l Logger) Warn(msgs ...string) *Logger {
msg := str.Join(msgs...)
if l.Level < LevelWarn {
return
return &l
}
timeStamp := ""
if !l.HideTime {
@ -98,12 +100,13 @@ func (l Logger) Warn(msgs ...string) {
msg = str.TrimEnd(msg, l.LengthLimit)
}
OutputFunc(fmt.Sprint(str.Join(timeStamp, argWarn, " "), msg))
return &l
}
func (l Logger) Error(msgs ...string) {
func (l Logger) Error(msgs ...string) *Logger {
msg := str.Join(msgs...)
if l.Level < LevelError {
return
return &l
}
timeStamp := ""
if !l.HideTime {
@ -113,4 +116,5 @@ func (l Logger) Error(msgs ...string) {
msg = str.TrimEnd(msg, l.LengthLimit)
}
OutputFunc(fmt.Sprint(str.Join(timeStamp, argError, " "), msg))
return &l
}

4
go.mod

@ -7,6 +7,7 @@ require (
github.com/dustin/go-humanize v1.0.1
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203
github.com/gookit/color v1.5.4
github.com/k0kubun/pp/v3 v3.2.0
github.com/parnurzeal/gorequest v0.3.0
github.com/rs/xid v1.5.0
github.com/stretchr/testify v1.8.4
@ -20,6 +21,8 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/elazarl/goproxy v0.0.0-20231117061959-7cc037d33fb5 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/moul/http2curl v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
@ -28,5 +31,6 @@ require (
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

9
go.sum

@ -54,6 +54,12 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/k0kubun/pp/v3 v3.2.0 h1:h33hNTZ9nVFNP3u2Fsgz8JXiF5JINoZfFq4SvKJwNcs=
github.com/k0kubun/pp/v3 v3.2.0/go.mod h1:ODtJQbQcIRfAD3N+theGCV1m/CBxweERz2dapdz1EwA=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/moul/http2curl v1.0.0 h1:dRMWoAtb+ePxMlLkrCbAqh4TlPHXvoGUSQ323/9Zahs=
github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ=
github.com/parnurzeal/gorequest v0.3.0 h1:SoFyqCDC9COr1xuS6VA8fC8RU7XyrJZN2ona1kEX7FI=
@ -134,6 +140,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
@ -149,6 +156,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

10
utils/str/index.go

@ -32,7 +32,7 @@ func Join(ss ...string) string {
return builder.String()
}
func Each(
func ForEach(
ss []string, f func(index int, s string) (string, bool),
) []string {
var result []string
@ -43,3 +43,11 @@ func Each(
}
return result
}
func For(ss []string, f func(string) string) []string {
result := make([]string, len(ss))
for index := range ss {
result[index] = f(ss[index])
}
return result
}

7
utils/str/str_test.go

@ -67,19 +67,19 @@ func TestT(t *testing.T) {
}{
{
name: "String substitution",
template: "Hello, {}!",
template: "Hello, {ee}!",
args: []any{"world"},
want: "Hello, world!",
},
{
name: "Integer substitution",
template: "I have {} apples",
template: "I have {aa} apples",
args: []any{5},
want: "I have 5 apples",
},
{
name: "Error substitution",
template: "Error: {}",
template: "Error: {f}",
args: []any{fmt.Errorf("something went wrong")},
want: "Error: something went wrong",
},
@ -111,6 +111,7 @@ func BenchmarkT(b *testing.B) {
T(template, args...)
}
}
func BenchmarkSprintf(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = fmt.Sprintf("Hello, %s! You have %d new messages.", "John", 42)

8
utils/str/template.go

@ -2,6 +2,7 @@ package str
import (
"bytes"
"fmt"
"strconv"
)
@ -18,6 +19,9 @@ func T(template string, args ...any) string {
if template[i] == '{' {
buf.WriteString(template[head:i])
i++
for template[i] != '}' {
i++
}
head = i + 1
switch arg := args[argIndex].(type) {
case string:
@ -39,3 +43,7 @@ func T(template string, args ...any) string {
buf.WriteString(template[head:])
return buf.String()
}
func F(templateF string, args ...any) string {
return fmt.Sprintf(templateF, args...)
}

Loading…
Cancel
Save