Skip to content

Commit

Permalink
♻️ 重构优化打印控制台表格内容相关代码
Browse files Browse the repository at this point in the history
替换依赖:github.com/scylladb/termtables => github.com/olekukonko/tablewriter
  • Loading branch information
iTanken committed May 7, 2024
1 parent feb2fb6 commit ce18e88
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
18 changes: 11 additions & 7 deletions dateparse/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/godoes/dateparse"
"github.com/scylladb/termtables"
"github.com/olekukonko/tablewriter"
)

var (
Expand Down Expand Up @@ -53,9 +53,13 @@ func main() {
}
fmt.Printf("\n")

table := termtables.CreateTable()
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(false)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)

table.AddHeaders("method", "Zone Source", "Parsed", "Parsed: t.In(time.UTC)")
table.SetHeader([]string{"method", "Zone Source", "Parsed", "Parsed: t.In(time.UTC)"})

parsers := map[string]parser{
"ParseAny": parseAny,
Expand All @@ -66,16 +70,16 @@ func main() {

for name, parser := range parsers {
time.Local = nil
table.AddRow(name, "time.Local = nil", parser(datestr, nil, false), parser(datestr, nil, true))
table.Append([]string{name, "time.Local = nil", parser(datestr, nil, false), parser(datestr, nil, true)})
if timezone != "" {
time.Local = loc
table.AddRow(name, "time.Local = timezone arg", parser(datestr, loc, false), parser(datestr, loc, true))
table.Append([]string{name, "time.Local = timezone arg", parser(datestr, loc, false), parser(datestr, loc, true)})
}
time.Local = time.UTC
table.AddRow(name, "time.Local = time.UTC", parser(datestr, time.UTC, false), parser(datestr, time.UTC, true))
table.Append([]string{name, "time.Local = time.UTC", parser(datestr, time.UTC, false), parser(datestr, time.UTC, true)})
}

fmt.Println(table.Render())
table.Render()
}

type parser func(datestr string, loc *time.Location, utc bool) string
Expand Down
15 changes: 10 additions & 5 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package main
import (
"flag"
"fmt"
"os"
"time"

"github.com/godoes/dateparse"
"github.com/scylladb/termtables"
"github.com/olekukonko/tablewriter"
)

var examples = []string{
Expand Down Expand Up @@ -150,17 +151,21 @@ func main() {
time.Local = loc
}

table := termtables.CreateTable()
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(false)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)

table.AddHeaders("Input", "Parsed, and Output as %v")
table.SetHeader([]string{"Input", "Parsed, and Output as %v"})
for _, dateExample := range examples {
t, err := dateparse.ParseLocal(dateExample)
if err != nil {
panic(err.Error())
}
table.AddRow(dateExample, fmt.Sprintf("%v", t))
table.Append([]string{dateExample, fmt.Sprintf("%v", t)})
}
fmt.Println(table.Render())
table.Render()
}

/*
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ module github.com/godoes/dateparse
go 1.18

require (
github.com/scylladb/termtables v1.0.0
github.com/olekukonko/tablewriter v0.0.5
github.com/stretchr/testify v1.9.0
)

require (
github.com/apcera/termtables v0.0.0-20170405184538-bcbc5dc54055 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-runewidth v0.0.10 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.1.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
15 changes: 7 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
github.com/apcera/termtables v0.0.0-20170405184538-bcbc5dc54055 h1:IkPAzP+QjchKXXFX6LCcpDKa89b/e/0gPCUbQGWtUUY=
github.com/apcera/termtables v0.0.0-20170405184538-bcbc5dc54055/go.mod h1:8mHYHlOef9UC51cK1/WRvE/iQVM8O8QlYFa8eh8r5I8=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRRpdGg=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
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/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/scylladb/termtables v1.0.0 h1:uUnesUY4V1VPCotpOQLb1LjTXVvzwy7Ramx8K8+w+8U=
github.com/scylladb/termtables v1.0.0/go.mod h1:C1a7PQSMz9NShzorzCiG2fk9+xuCgLkPeCvMHYR2OWg=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
10 changes: 5 additions & 5 deletions parseany.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func ParseFormat(datestr string, opts ...ParserOption) (string, error) {
return string(p.format), nil
}

// ParseStrict parse an unknown date format. IF the date is ambigous
// ParseStrict parse an unknown date format. IF the date is ambiguous
// mm/dd vs dd/mm then return an error. These return errors: 3.3.2014 , 8/8/71 etc
func ParseStrict(datestr string, opts ...ParserOption) (time.Time, error) {
p, err := parseTime(datestr, nil, opts...)
Expand Down Expand Up @@ -562,7 +562,7 @@ iterRunes:
// We have no idea if this is
// yy-mon-dd OR dd-mon-yy
//
// We are going to ASSUME (bad, bad) that it is dd-mon-yy which is a horible assumption
// We are going to ASSUME (bad, bad) that it is dd-mon-yy which is a horrible assumption
p.ambiguousMD = true
p.yearlen = 2
p.set(p.yeari, "06")
Expand Down Expand Up @@ -592,7 +592,7 @@ iterRunes:
// We have no idea if this is
// yy-mon-dd OR dd-mon-yy
//
// We are going to ASSUME (bad, bad) that it is dd-mon-yy which is a horible assumption
// We are going to ASSUME (bad, bad) that it is dd-mon-yy which is a horrible assumption
p.ambiguousMD = true
p.yearlen = 2
p.set(p.yeari, "06")
Expand Down Expand Up @@ -1951,7 +1951,7 @@ iterRunes:
// We have no idea if this is
// yy-mon-dd OR dd-mon-yy
//
// We are going to ASSUME (bad, bad) that it is dd-mon-yy which is a horible assumption
// We are going to ASSUME (bad, bad) that it is dd-mon-yy which is a horrible assumption
p.ambiguousMD = true
p.yearlen = 2
p.set(p.yeari, "06")
Expand All @@ -1978,7 +1978,7 @@ iterRunes:
// We have no idea if this is
// yy-mon-dd OR dd-mon-yy
//
// We are going to ASSUME (bad, bad) that it is dd-mon-yy which is a horible assumption
// We are going to ASSUME (bad, bad) that it is dd-mon-yy which is a horrible assumption
p.ambiguousMD = true
p.yearlen = 2
p.set(p.yeari, "06")
Expand Down
4 changes: 2 additions & 2 deletions parseany_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,12 +841,12 @@ func TestInLocation(t *testing.T) {
// Now some errors
zeroTime := time.Time{}.Unix()
ts, err = ParseIn("INVALID", denverLoc)
assert.Equal(t, zeroTime, ts.Unix())
assert.NotEqual(t, nil, err)
assert.Equal(t, zeroTime, ts.Unix())

ts, err = ParseLocal("INVALID")
assert.Equal(t, zeroTime, ts.Unix())
assert.NotEqual(t, nil, err)
assert.Equal(t, zeroTime, ts.Unix())
}

func TestPreferMonthFirst(t *testing.T) {
Expand Down

0 comments on commit ce18e88

Please sign in to comment.