Skip to content

Commit

Permalink
Fixed wide width support
Browse files Browse the repository at this point in the history
  • Loading branch information
noborus committed Mar 15, 2023
1 parent 8f1e0e2 commit 353df7f
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions guesswidth.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,14 @@ func separatorPosition(lr []rune, p int, pos []int, n int) int {

f := p
fp := 0
for ; !unicode.IsSpace(lr[f]); f++ {

for ; f < len(lr) && !unicode.IsSpace(lr[f]); f++ {
fp++
}

b := p
bp := 0
for ; !unicode.IsSpace(lr[b]); b-- {
for ; b > 0 && !unicode.IsSpace(lr[b]); b-- {
bp++
}

Expand All @@ -182,24 +183,27 @@ func split(line string, pos []int, trimSpace bool) []string {
start := 0
columns := make([]string, len(pos)+1)
lr := []rune(line)
w := 0
for p := 0; p < len(lr); p++ {
if n > len(pos)-1 {
start = p
break
}
if pos[n] == p {
p = separatorPosition(lr, p, pos, n)
if pos[n] <= w {
end := separatorPosition(lr, p, pos, n)
if start > end {
break
}
col := string(lr[start:end])
if trimSpace {
columns[n] = strings.TrimSpace(string(lr[start:p]))
columns[n] = strings.TrimSpace(col)
} else {
columns[n] = string(lr[start:p])
columns[n] = string(col)
}
n++
start = p
}
if runewidth.RuneWidth(lr[p]) == 2 {
p++
start = end
}
w += runewidth.RuneWidth(lr[p])
}
columns[len(columns)-1] = strings.TrimSpace(string(lr[start:]))
return columns
Expand Down

0 comments on commit 353df7f

Please sign in to comment.