You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cointop/table/align/align.go

35 lines
564 B
Go

package align
import (
"fmt"
"strings"
)
func AlignLeft(s string, n int) string {
if len(s) > n {
return s[:n]
}
return fmt.Sprintf("%s%s", s, strings.Repeat(" ", n-len(s)))
}
func AlignRight(s string, n int) string {
if len(s) > n {
return s[:n]
}
return fmt.Sprintf("%s%s", strings.Repeat(" ", n-len(s)), s)
}
func AlignCenter(s string, n int) string {
if len(s) > n {
return s[:n]
}
pad := (n - len(s)) / 2
lpad := pad
rpad := n - len(s) - lpad
return fmt.Sprintf("%s%s%s", strings.Repeat(" ", lpad), s, strings.Repeat(" ", rpad))
}