2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-16 21:25:38 +00:00
cointop/pkg/table/align/align.go
Alexis Hildebrandt 5933f22020 Ignore ANSI escape codes when when determining string length
to calculate width and padding for table alignment.
2021-04-20 10:29:09 +02:00

47 lines
858 B
Go

package align
import (
"fmt"
"strings"
"unicode/utf8"
"github.com/acarl005/stripansi"
)
// AlignLeft align left
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", t, strings.Repeat(" ", n-slen))
}
// AlignRight align right
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), t)
}
// AlignCenter align center
func AlignCenter(t string, n int) string {
s := stripansi.Strip(t)
slen := utf8.RuneCountInString(s)
if slen > n {
return s[:n]
}
pad := (n - slen) / 2
lpad := pad
rpad := n - slen - lpad
return fmt.Sprintf("%s%s%s", strings.Repeat(" ", lpad), t, strings.Repeat(" ", rpad))
}