package main
import (
"bytes"
"fmt"
"strings"
)
func getDie(n int) []string {
return []string{
" ------",
"| |",
"| |",
fmt.Sprintf("|%6d|", n),
" ------",
}
}
func joinLines(between int, items ...[]string) []string {
if len(items) == 0 {
return nil
}
if len(items) == 1 {
return items[0]
}
lineCount := 0
maxSizes := make([]int, len(items))
for i, item := range items {
for j, line := range item {
if maxSizes[i] < len(line) {
maxSizes[i] = len(line)
}
if j+1 > lineCount {
lineCount = j + 1
}
}
}
lines := make([]string, lineCount)
for i := 0; i < lineCount; i++ {
var buff bytes.Buffer
for j, item := range items {
diff := 0
if j+1 < len(items) {
diff += maxSizes[j] + between
}
if i < len(item) {
line := item[i]
buff.WriteString(line)
diff -= len(line)
}
if diff > 0 {
buff.WriteString(strings.Repeat(" ", diff))
}
}
lines[i] = buff.String()
}
return lines
}
func main() {
a, b, c, d := getDie(2), getDie(3), []string{"", "", "="}, getDie(5)
all := joinLines(3, a, b, c, d)
for _, line := range all {
fmt.Println(line)
}
}
https://play.golang.org/p/NNrTUDdfyn