Merge branch 'afh-table-alignment'

pull/121/head
Miguel Mota 3 years ago
commit 15471504e4
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9

@ -4,30 +4,35 @@ import (
"fmt"
"strings"
"unicode/utf8"
"github.com/acarl005/stripansi"
)
// AlignLeft align left
func AlignLeft(s string, n int) string {
func AlignLeft(t string, n int) string {
s := stripansi.Strip(t)
slen := utf8.RuneCountInString(s)
if slen > n {
return s[:n]
}
return fmt.Sprintf("%s%s", s, strings.Repeat(" ", n-slen))
return fmt.Sprintf("%s%s", t, strings.Repeat(" ", n-slen))
}
// AlignRight align right
func AlignRight(s string, n int) string {
func AlignRight(t string, n int) string {
s := stripansi.Strip(t)
slen := utf8.RuneCountInString(s)
if slen > n {
return s[:n]
}
return fmt.Sprintf("%s%s", strings.Repeat(" ", n-slen), s)
return fmt.Sprintf("%s%s", strings.Repeat(" ", n-slen), t)
}
// AlignCenter align center
func AlignCenter(s string, n int) string {
func AlignCenter(t string, n int) string {
s := stripansi.Strip(t)
slen := utf8.RuneCountInString(s)
if slen > n {
return s[:n]
@ -37,5 +42,5 @@ func AlignCenter(s string, n int) string {
lpad := pad
rpad := n - slen - lpad
return fmt.Sprintf("%s%s%s", strings.Repeat(" ", lpad), s, strings.Repeat(" ", rpad))
return fmt.Sprintf("%s%s%s", strings.Repeat(" ", lpad), t, strings.Repeat(" ", rpad))
}

@ -7,6 +7,7 @@ import (
"strings"
"unicode/utf8"
"github.com/acarl005/stripansi"
"github.com/miguelmota/cointop/pkg/pad"
"github.com/miguelmota/cointop/pkg/table/align"
)
@ -130,7 +131,8 @@ func (t *Table) normalizeColWidthPerc() {
// Format format table
func (t *Table) Format() *Table {
for _, c := range t.cols {
c.width = utf8.RuneCountInString(c.name) + 1
s := stripansi.Strip(c.name)
c.width = utf8.RuneCountInString(s) + 1
if c.minWidth > c.width {
c.width = c.minWidth
}
@ -152,7 +154,8 @@ func (t *Table) Format() *Table {
r.strValues[j] = fmt.Sprintf("%v", v)
}
runeCount := utf8.RuneCountInString(r.strValues[j])
s := stripansi.Strip(r.strValues[j])
runeCount := utf8.RuneCountInString(s)
if runeCount > t.cols[j].width {
t.cols[j].width = runeCount
}

Loading…
Cancel
Save