forked from johnmikee/pterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
panel_printer.go
190 lines (160 loc) · 4.25 KB
/
panel_printer.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package pterm
import (
"io"
"strings"
"github.com/mattn/go-runewidth"
"github.com/gemini/pterm/internal"
)
// Panel contains the data, which should be printed inside a PanelPrinter.
type Panel struct {
Data string
}
// Panels is a two dimensional coordinate system for Panel.
type Panels [][]Panel
// DefaultPanel is the default PanelPrinter.
var DefaultPanel = PanelPrinter{
Padding: 1,
}
// PanelPrinter prints content in boxes.
type PanelPrinter struct {
Panels Panels
Padding int
BottomPadding int
SameColumnWidth bool
BoxPrinter BoxPrinter
Writer io.Writer
}
// WithPanels returns a new PanelPrinter with specific options.
func (p PanelPrinter) WithPanels(panels Panels) *PanelPrinter {
p.Panels = panels
return &p
}
// WithPadding returns a new PanelPrinter with specific options.
func (p PanelPrinter) WithPadding(padding int) *PanelPrinter {
if padding < 0 {
padding = 0
}
p.Padding = padding
return &p
}
// WithBottomPadding returns a new PanelPrinter with specific options.
func (p PanelPrinter) WithBottomPadding(bottomPadding int) *PanelPrinter {
if bottomPadding < 0 {
bottomPadding = 0
}
p.BottomPadding = bottomPadding
return &p
}
// WithSameColumnWidth returns a new PanelPrinter with specific options.
func (p PanelPrinter) WithSameColumnWidth(b ...bool) *PanelPrinter {
b2 := internal.WithBoolean(b)
p.SameColumnWidth = b2
return &p
}
// WithBoxPrinter returns a new PanelPrinter with specific options.
func (p PanelPrinter) WithBoxPrinter(boxPrinter BoxPrinter) *PanelPrinter {
p.BoxPrinter = boxPrinter
return &p
}
// WithWriter sets the custom Writer.
func (p PanelPrinter) WithWriter(writer io.Writer) *PanelPrinter {
p.Writer = writer
return &p
}
func (p PanelPrinter) getRawOutput() string {
var ret string
for _, panel := range p.Panels {
for _, panel2 := range panel {
ret += panel2.Data + "\n\n"
}
ret += "\n"
}
return ret
}
// Srender renders the Template as a string.
func (p PanelPrinter) Srender() (string, error) {
var ret string
if RawOutput {
return p.getRawOutput(), nil
}
for i := range p.Panels {
for i2 := range p.Panels[i] {
p.Panels[i][i2].Data = strings.TrimSuffix(p.Panels[i][i2].Data, "\n")
}
}
if p.BoxPrinter != (BoxPrinter{}) {
for i := range p.Panels {
for i2 := range p.Panels[i] {
p.Panels[i][i2].Data = p.BoxPrinter.Sprint(p.Panels[i][i2].Data)
}
}
}
for i := range p.Panels {
if len(p.Panels)-1 != i {
for i2 := range p.Panels[i] {
p.Panels[i][i2].Data += strings.Repeat("\n", p.BottomPadding)
}
}
}
columnMaxHeightMap := make(map[int]int)
if p.SameColumnWidth {
for _, panel := range p.Panels {
for i, p2 := range panel {
if columnMaxHeightMap[i] < internal.GetStringMaxWidth(p2.Data) {
columnMaxHeightMap[i] = internal.GetStringMaxWidth(p2.Data)
}
}
}
}
for _, boxLine := range p.Panels {
var maxHeight int
var renderedPanels []string
for _, box := range boxLine {
renderedPanels = append(renderedPanels, box.Data)
}
for i, panel := range renderedPanels {
renderedPanels[i] = strings.ReplaceAll(panel, "\n", Reset.Sprint()+"\n")
}
for _, box := range renderedPanels {
height := len(strings.Split(box, "\n"))
if height > maxHeight {
maxHeight = height
}
}
for i := 0; i < maxHeight; i++ {
if maxHeight != i {
for j, letter := range renderedPanels {
var letterLine string
letterLines := strings.Split(letter, "\n")
var maxLetterWidth int
if !p.SameColumnWidth {
maxLetterWidth = internal.GetStringMaxWidth(letter)
}
if len(letterLines) > i {
letterLine = letterLines[i]
}
letterLineLength := runewidth.StringWidth(RemoveColorFromString(letterLine))
if !p.SameColumnWidth {
if letterLineLength < maxLetterWidth {
letterLine += strings.Repeat(" ", maxLetterWidth-letterLineLength)
}
} else {
if letterLineLength < columnMaxHeightMap[j] {
letterLine += strings.Repeat(" ", columnMaxHeightMap[j]-letterLineLength)
}
}
letterLine += strings.Repeat(" ", p.Padding)
ret += letterLine
}
ret += "\n"
}
}
}
return ret, nil
}
// Render prints the Template to the terminal.
func (p PanelPrinter) Render() error {
s, _ := p.Srender()
Println(s)
return nil
}