-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (82 loc) · 2.12 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
func replaceTabToSpace(lines []string) []string {
var newLines []string
for _, line := range lines {
line = strings.Replace(line, "\t", " ", -1)
newLines = append(newLines, line)
}
return newLines
}
func calculateMaxWidth(lines []string) int {
var width = 0
for _, line := range lines {
var lineLength = len(line)
if lineLength > width {
width = lineLength
}
}
return width
}
func normalizelineWidth(lines []string, maxWidth int) []string {
var newLines []string
for _, line := range lines {
newLines = append(newLines, string(line)+strings.Repeat(" ", maxWidth-len(line)))
}
return newLines
}
func buildBalloon(lines []string, maxWidth int) string {
var borders = []string{"/", "\\", "\\", "/", "|", "<", ">", "-", "_"}
var borderTop = " " + strings.Repeat(borders[8], maxWidth+2)
var borderBottom = " " + strings.Repeat(borders[7], maxWidth+2)
var balloon []string
var count = len(lines)
balloon = append(balloon, borderTop)
if count == 1 {
s := fmt.Sprintf("%s %s %s", borders[5], lines[0], borders[6])
balloon = append(balloon, s)
} else {
s := fmt.Sprintf("%s %s %s", borders[0], lines[0], borders[1])
balloon = append(balloon, s)
for i := 1; i < count-1; i++ {
s = fmt.Sprintf("%s %s %s", borders[4], lines[i], borders[4])
balloon = append(balloon, s)
}
s = fmt.Sprintf("%s %s %s", borders[2], lines[count-1], borders[3])
balloon = append(balloon, s)
}
balloon = append(balloon, borderBottom)
return strings.Join(balloon, "\n")
}
func main() {
//INFO: ASCII Koala
const koala = ` \
\ ||
Ϲ(◕𐊶◕)Ͻ
(__ ⊃
||`
//INFO: Get data from Stdin
reader := bufio.NewReader(os.Stdin)
var lines []string
for {
var line, _, err = reader.ReadLine()
if err != nil && err == io.EOF {
break
}
lines = append(lines, string(line))
}
//INFO: Display final Result
lines = replaceTabToSpace(lines)
maxWidth := calculateMaxWidth(lines)
messages := normalizelineWidth(lines, maxWidth)
balloon := buildBalloon(messages, maxWidth)
fmt.Println(balloon)
fmt.Println(koala)
fmt.Println()
}