Skip to content

Commit

Permalink
Merge pull request #378 from opsnull/master
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Jul 17, 2022
2 parents 1109943 + f8f2d26 commit f873e45
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
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

0 comments on commit f873e45

Please sign in to comment.