-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.go
47 lines (43 loc) · 1.07 KB
/
box.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// boxed just prints your strings in an ascii box
package box
import (
"fmt"
"slices"
"strings"
"unicode/utf8"
)
// Box returns a string in a box
func Box(msg string) string {
inner := fmt.Sprintf("| %s |", msg)
seperator := strings.Repeat("-", utf8.RuneCountInString(inner))
return fmt.Sprintf("%s \n%s \n%s", seperator, inner, seperator)
}
// BoxMany returns many strings in a box
func BoxMany(msgs ...string) string {
var tmp []string
lgst := lengthiest(msgs)
for _, msg := range msgs {
tl := len(lgst) - len(msg)
padding := strings.Repeat(" ", tl)
tmp = append(tmp, fmt.Sprintf("| %s %s|\n", msg, padding))
}
lgstPad := lengthiest(tmp)
seperator := strings.Repeat("-", utf8.RuneCountInString(lgstPad)-1)
s1 := slices.Insert(tmp, 0, fmt.Sprintf("%s\n", seperator))
s1 = append(s1, seperator)
return strings.Join(s1[:], "")
}
// lengthiest returns an int representing the length of the longest
// string in a []string
func lengthiest(ws []string) string {
l := 0
var v string
for _, w := range ws {
ln := len(w)
if ln > l {
l = ln
v = w
}
}
return v
}