Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(table): fixed column length calculation for Chinese strings #378

Merged
merged 1 commit into from Jul 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions _examples/table/demo/main.go
Expand Up @@ -6,10 +6,10 @@ func main() {
// Create a fork of the default table, fill it with data and print it.
// Data can also be generated and inserted later.
pterm.DefaultTable.WithHasHeader().WithData(pterm.TableData{
{"Firstname", "Lastname", "Email"},
{"Paul", "Dean", "nisi.dictum.augue@velitAliquam.co.uk"},
{"Callie", "Mckay", "egestas.nunc.sed@est.com"},
{"Libby", "Camacho", "aliquet.lobortis@semper.com"},
{"Firstname", "Lastname", "Email", "Note"},
{"Paul", "Dean", "nisi.dictum.augue@velitAliquam.co.uk", ""},
{"Callie", "Mckay", "egestas.nunc.sed@est.com", "这是一个测试, haha!"},
{"Libby", "Camacho", "aliquet.lobortis@semper.com", "just a test, hey!"},
}).Render()

pterm.Println() // Blank line
Expand All @@ -20,5 +20,6 @@ func main() {
{"Paul", "Dean", "nisi.dictum.augue@velitAliquam.co.uk"},
{"Callie", "Mckay", "egestas.nunc.sed@est.com"},
{"Libby", "Camacho", "aliquet.lobortis@semper.com"},
{"张", "小宝", "zhang@example.com"},
}).WithRightAlignment().Render()
}
10 changes: 5 additions & 5 deletions table_printer.go
Expand Up @@ -4,9 +4,9 @@ import (
"encoding/csv"
"io"
"strings"
"unicode/utf8"

"github.com/pterm/pterm/internal"
"github.com/mattn/go-runewidth"
)

// DefaultTable contains standards, which can be used to print a TablePrinter.
Expand Down Expand Up @@ -163,7 +163,7 @@ func (p TablePrinter) Srender() (string, error) {

for _, row := range p.Data {
for ci, column := range row {
columnLength := utf8.RuneCountInString(RemoveColorFromString(column))
columnLength := runewidth.StringWidth(RemoveColorFromString(column))
if columnLength > maxColumnWidth[ci] {
maxColumnWidth[ci] = columnLength
}
Expand All @@ -174,11 +174,11 @@ func (p TablePrinter) Srender() (string, error) {
rowWidth := 0
for ci, column := range row {
columnString := p.createColumnString(column, maxColumnWidth[ci])
rowWidth += utf8.RuneCountInString(RemoveColorFromString(columnString))
rowWidth += runewidth.StringWidth(RemoveColorFromString(columnString))

if ci != len(row) && ci != 0 {
ret += p.Style.Sprint(p.SeparatorStyle.Sprint(p.Separator))
rowWidth += utf8.RuneCountInString(RemoveColorFromString(p.SeparatorStyle.Sprint(p.Separator)))
rowWidth += runewidth.StringWidth(RemoveColorFromString(p.SeparatorStyle.Sprint(p.Separator)))
}

if p.HasHeader && ri == 0 {
Expand Down Expand Up @@ -209,7 +209,7 @@ func (p TablePrinter) Srender() (string, error) {
}

func (p TablePrinter) createColumnString(data string, maxColumnWidth int) string {
columnLength := utf8.RuneCountInString(RemoveColorFromString(data))
columnLength := runewidth.StringWidth(RemoveColorFromString(data))
if p.RightAlignment {
return strings.Repeat(" ", maxColumnWidth-columnLength) + data
}
Expand Down