Compare commits

..

3 Commits

Author SHA1 Message Date
Timon Ringwald
5be0ec3905 unicode support added 2022-08-25 22:47:11 +02:00
Timon Ringwald
7f027b08ed removed useless newline 2022-08-25 17:07:37 +02:00
Timon Ringwald
5949bf193f removed debug print 2022-08-25 17:06:18 +02:00
4 changed files with 19 additions and 5 deletions

2
go.mod
View File

@ -6,11 +6,13 @@ require (
git.milar.in/milarin/gmath v0.0.2 git.milar.in/milarin/gmath v0.0.2
git.milar.in/milarin/slices v0.0.3 git.milar.in/milarin/slices v0.0.3
github.com/fatih/color v1.13.0 github.com/fatih/color v1.13.0
github.com/mattn/go-runewidth v0.0.13
) )
require ( require (
git.milar.in/milarin/channel v0.0.9 // indirect git.milar.in/milarin/channel v0.0.9 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
github.com/rivo/uniseg v0.3.4 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
) )

5
go.sum
View File

@ -11,6 +11,11 @@ github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.3.4 h1:3Z3Eu6FGHZWSfNKJTOUiPatWwfc7DzJRU04jFUqJODw=
github.com/rivo/uniseg v0.3.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=

View File

@ -31,5 +31,7 @@ func FormatHeaderTable(header string, table *Table) string {
b.WriteString(s) b.WriteString(s)
b.WriteRune('\n') b.WriteRune('\n')
}) })
return b.String()
ret := b.String()
return ret[:len(ret)-1]
} }

View File

@ -1,9 +1,10 @@
package tprint package tprint
import ( import (
"fmt"
"strings" "strings"
"unicode" "unicode"
"github.com/mattn/go-runewidth"
) )
func maxLengthStr(a, b string) string { func maxLengthStr(a, b string) string {
@ -26,9 +27,10 @@ func strLen(str string) int {
runes := []rune(str) runes := []rune(str)
for i := 0; i < len(runes); i++ { for i := 0; i < len(runes); i++ {
rn := runes[i]
// skip control sequences // skip control sequences
if unicode.IsControl(runes[i]) { if unicode.IsControl(rn) {
for j := i; j < len(runes)-1 && runes[j] != 'm'; j++ { for j := i; j < len(runes)-1 && runes[j] != 'm'; j++ {
i = j i = j
} }
@ -36,7 +38,11 @@ func strLen(str string) int {
continue continue
} }
if rn <= 0xFF || rn >= '─' && rn <= '╿' {
l++ l++
} else {
l += runewidth.RuneWidth(rn)
}
} }
return l return l
@ -54,6 +60,5 @@ func padStringLeft(str string, pad rune, length int) string {
func padStringCenter(str string, pad rune, length int) string { func padStringCenter(str string, pad rune, length int) string {
l := strLen(str) l := strLen(str)
fmt.Println(l, length)
return padStringLeft(padStringRight(str, pad, (length-l)/2+l), pad, length) return padStringLeft(padStringRight(str, pad, (length-l)/2+l), pad, length)
} }