diff --git a/go.mod b/go.mod index cfa0fa0..1daf710 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,11 @@ module github.com/noborus/guesswidth go 1.18 require ( - github.com/mattn/go-runewidth v0.0.16 + github.com/rivo/uniseg v0.4.7 github.com/spf13/cobra v1.8.1 ) require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/rivo/uniseg v0.4.7 // indirect github.com/spf13/pflag v1.0.5 // indirect ) diff --git a/go.sum b/go.sum index 001620a..239efdc 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= -github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/guesswidth.go b/guesswidth.go index d04ea47..ad6015f 100644 --- a/guesswidth.go +++ b/guesswidth.go @@ -13,7 +13,7 @@ import ( "strings" "unicode" - "github.com/mattn/go-runewidth" + "github.com/rivo/uniseg" ) // GuessWidth reads records from printf-like output. @@ -99,7 +99,7 @@ func (g *GuessWidth) UpdateMaxWidth(columns []string) []Cols { lastCol := len(g.Widths) - 1 for n, col := range columns { - width := runewidth.StringWidth(strings.TrimSpace(col)) + width := uniseg.StringWidth(strings.TrimSpace(col)) if width > g.Widths[n].Width { g.Widths[n].Width = width } @@ -127,11 +127,12 @@ func isRightAlign(str string) bool { if str == "" { return false } + r := len(str) - 1 for n := 0; n < len(str); n++ { if str[n] != ' ' { return false } - if str[len(str)-n-1] != ' ' { + if str[r-n] != ' ' { return true } } @@ -276,20 +277,22 @@ func split(line string, pos []int, trimSpace bool) []string { n++ start = end } - w += runewidth.RuneWidth(lr[p]) + w += uniseg.StringWidth(string(lr[p])) } - if n < len(columns) { - col := string(lr[start:]) - if trimSpace { - columns[n] = strings.TrimSpace(col) - } else { - columns[n] = col - } + if start >= len(lr) { + return columns + } + + // Handle the remaining part of the line after the last separator. + col := string(lr[start:]) + if trimSpace { + col = strings.TrimSpace(col) } + columns[n] = col return columns } -// roRows returns rows separated by columns. +// toRows returns rows separated by columns. func toRows(lines []string, pos []int, trimSpace bool) [][]string { rows := make([][]string, 0, len(lines)) for _, line := range lines { @@ -316,7 +319,7 @@ func lookupBlanks(line string) []int { first = false blanks = append(blanks, 0) - if runewidth.RuneWidth(v) == 2 { + if uniseg.StringWidth(string(v)) == 2 { blanks = append(blanks, 0) } } @@ -335,7 +338,7 @@ func countBlanks(blanks []int, line string) []int { } n++ - if runewidth.RuneWidth(r) == 2 { + if uniseg.StringWidth(string(r)) == 2 { n++ } }