Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
31 changes: 17 additions & 14 deletions guesswidth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"
"unicode"

"github.com/mattn/go-runewidth"
"github.com/rivo/uniseg"
)

// GuessWidth reads records from printf-like output.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
Expand All @@ -335,7 +338,7 @@ func countBlanks(blanks []int, line string) []int {
}

n++
if runewidth.RuneWidth(r) == 2 {
if uniseg.StringWidth(string(r)) == 2 {
n++
}
}
Expand Down