From ce18e882d0cf0937eac5a75c15b544b05a6a4fc6 Mon Sep 17 00:00:00 2001 From: liutianqi Date: Tue, 7 May 2024 15:01:38 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=89=93=E5=8D=B0=E6=8E=A7=E5=88=B6=E5=8F=B0?= =?UTF-8?q?=E8=A1=A8=E6=A0=BC=E5=86=85=E5=AE=B9=E7=9B=B8=E5=85=B3=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 替换依赖:github.com/scylladb/termtables => github.com/olekukonko/tablewriter --- dateparse/main.go | 18 +++++++++++------- example/main.go | 15 ++++++++++----- go.mod | 7 +++---- go.sum | 15 +++++++-------- parseany.go | 10 +++++----- parseany_test.go | 4 ++-- 6 files changed, 38 insertions(+), 31 deletions(-) diff --git a/dateparse/main.go b/dateparse/main.go index b8b4e32..0a64223 100644 --- a/dateparse/main.go +++ b/dateparse/main.go @@ -7,7 +7,7 @@ import ( "time" "github.com/godoes/dateparse" - "github.com/scylladb/termtables" + "github.com/olekukonko/tablewriter" ) var ( @@ -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, @@ -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 diff --git a/example/main.go b/example/main.go index aeb44b6..0e906c5 100644 --- a/example/main.go +++ b/example/main.go @@ -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{ @@ -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() } /* diff --git a/go.mod b/go.mod index ab457f1..5e76045 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 4a69723..08dbd70 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/parseany.go b/parseany.go index 2cc4005..da6d761 100644 --- a/parseany.go +++ b/parseany.go @@ -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...) @@ -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") @@ -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") @@ -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") @@ -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") diff --git a/parseany_test.go b/parseany_test.go index 8e1d98b..738159d 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -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) {