-
-
Notifications
You must be signed in to change notification settings - Fork 550
/
arrangement.go
196 lines (160 loc) · 4.35 KB
/
arrangement.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
191
192
193
194
195
196
package config
import (
ui "github.com/gizak/termui/v3"
"github.com/sqshq/sampler/console"
"image"
)
func (c *Config) setDefaultArrangement() {
components := getComponents(c)
if allHaveNoPosition(components) {
setSingleComponentPosition(components[0])
}
for _, component := range components {
if component.Position == nil || len(component.Position) == 0 {
lc := getLargestComponent(components)
le := getLargestEmptySpaceRectangle(components)
if getSquare(lc) > le.Dx()*le.Dy()*2 {
arrangeIntoLargestComponent(component, lc)
} else {
arrangeIntoLargestEmptySpace(component, le)
}
}
}
}
func arrangeIntoLargestComponent(component *ComponentConfig, lc *ComponentConfig) {
lr := lc.GetRectangle()
var w, h, ws, hs int
if lr.Dx()/2 > lr.Dy() {
w = lr.Dx() / 2
h = lr.Dy()
ws = w
hs = 0
} else {
w = lr.Dx()
h = lr.Dy() / 2
ws = 0
hs = h
}
component.Position = [][]int{
{lr.Min.X + ws, lr.Min.Y + hs},
{w, h},
}
lc.Position = [][]int{
{lr.Min.X, lr.Min.Y},
{w, h},
}
}
func arrangeIntoLargestEmptySpace(component *ComponentConfig, le image.Rectangle) {
component.Position = [][]int{
{le.Min.X, le.Min.Y},
{le.Dx(), le.Dy()},
}
}
func allHaveNoPosition(components []*ComponentConfig) bool {
for _, component := range components {
if component.Position != nil {
return false
}
}
return true
}
func getLargestComponent(components []*ComponentConfig) *ComponentConfig {
largestComponent := components[0]
for _, component := range components {
if getSquare(component) > getSquare(largestComponent) {
largestComponent = component
}
}
return largestComponent
}
func getSquare(c *ComponentConfig) int {
r := c.GetRectangle()
return r.Dx() * r.Dy()
}
func getComponents(c *Config) []*ComponentConfig {
var components []*ComponentConfig
for i := range c.RunCharts {
components = append(components, &c.RunCharts[i].ComponentConfig)
}
for i := range c.BarCharts {
components = append(components, &c.BarCharts[i].ComponentConfig)
}
for i := range c.Gauges {
components = append(components, &c.Gauges[i].ComponentConfig)
}
for i := range c.SparkLines {
components = append(components, &c.SparkLines[i].ComponentConfig)
}
for i := range c.AsciiBoxes {
components = append(components, &c.AsciiBoxes[i].ComponentConfig)
}
for i := range c.TextBoxes {
components = append(components, &c.TextBoxes[i].ComponentConfig)
}
return components
}
func setSingleComponentPosition(c *ComponentConfig) {
w := int(console.ColumnsCount)
h := int(console.RowsCount * 0.6)
c.Position = [][]int{
{(console.ColumnsCount - w) / 2, (console.RowsCount - h) / 2},
{w, h},
}
}
func getLargestEmptySpaceRectangle(components []*ComponentConfig) image.Rectangle {
grid := [console.RowsCount][console.ColumnsCount]int{}
for _, component := range components {
rect := component.GetRectangle()
for r := ui.MinInt(console.RowsCount, rect.Min.Y); r < ui.MinInt(console.RowsCount, rect.Max.Y); r++ {
for c := ui.MinInt(console.ColumnsCount, rect.Min.X); c < ui.MinInt(console.ColumnsCount, rect.Max.X); c++ {
grid[r][c] = 1
}
}
}
mr := image.ZR
for row := 0; row < console.RowsCount; row++ {
histogram := createHistogram(grid, row)
r := calcMaxRectangle(histogram, row)
if r.Dx()*r.Dy() > mr.Dx()*mr.Dy() {
mr = r
}
}
return mr
}
func calcMaxRectangle(histogram [console.ColumnsCount]int, row int) image.Rectangle {
maxRectangle := image.ZR
maxArea := 0
for i := 0; i < len(histogram); i++ {
height := histogram[i]
if height > maxArea {
maxArea = height
maxRectangle = image.Rect(i, row, i, row+height)
}
for j := i - 1; j >= 0; j-- {
width := i - j + 1
height = ui.MinInt(height, histogram[j])
if width*height > maxArea {
maxArea = width * height
maxRectangle = image.Rect(j, row, j+width, row+height)
}
}
}
return maxRectangle
}
func createHistogram(grid [console.RowsCount][console.ColumnsCount]int, row int) [console.ColumnsCount]int {
histogram := [console.ColumnsCount]int{}
for column := 0; column < console.ColumnsCount; column++ {
histogram[column] = countEmptyCellsBelow(grid, row, column)
}
return histogram
}
func countEmptyCellsBelow(grid [console.RowsCount][console.ColumnsCount]int, row int, column int) int {
count := 0
for r := row; r < console.RowsCount; r++ {
if grid[r][column] == 1 {
return count
}
count++
}
return count
}