-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDSL.swift
162 lines (128 loc) · 4.49 KB
/
DSL.swift
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
//
// DSL.swift
// Wassup
//
// Created by Josh Holtz on 4/1/22.
//
import Foundation
protocol ContentBuilder {
}
class Dashboards {
static var all: [Dashboard] = []
}
struct Dashboard {
var name: String
@PaneArrayBuilder var panes: () -> [Pane]
init(_ name: String, @PaneArrayBuilder panes: @escaping () -> [Pane]) {
self.name = name
self.panes = panes
Dashboards.all.append(self)
}
}
struct Pane {
var name: String
var alert: Output.Pane.CountAlert
@ContentArrayBuilder var contents: () -> [ContentItem]
var x: Int?
var y: Int?
var width: Int
var height: Int
init(_ name: String, alert: Output.Pane.CountAlert = .none, x: Int? = nil, y: Int? = nil, width: Int = 1, height: Int = 1, @ContentArrayBuilder contents: @escaping () -> [ContentItem]) {
self.name = name
self.alert = alert
self.x = x
self.y = y
self.width = width
self.height = height
self.contents = contents
}
}
@resultBuilder
enum PaneArrayBuilder {
static func buildEither(first component: [Pane]) -> [Pane] {
return component
}
static func buildEither(second component: [Pane]) -> [Pane] {
return component
}
static func buildBlock(_ components: [Pane]...) -> [Pane] {
return components.flatMap { $0 }
}
static func buildOptional(_ component: [Pane]?) -> [Pane] {
return component ?? []
}
static func buildExpression(_ expression: Pane) -> [Pane] {
return [expression]
}
static func buildExpression(_ expression: Void) -> [Pane] {
return []
}
}
@resultBuilder
enum ContentArrayBuilder {
static func buildBlock(_ components: [ContentItem]...) -> [ContentItem] {
return components.flatMap { $0 }
}
static func buildExpression(_ expression: ContentItem) -> [ContentItem] {
return [expression]
}
}
extension Pane {
func frame(x: Int, y: Int, width: Int = 1, height: Int = 1) -> Self {
return Pane(self.name, alert: self.alert, x: x, y: y, width: width, height: height, contents: self.contents)
}
}
extension Dashboard {
func toOutput() -> Output.Dashboard {
let panes: [Output.Pane] = positionedPanes.map { (pane, x, y) -> Output.Pane in
let outputItems: [Output.Item] = pane.contents().map { contentItem -> Output.Item in
let title = contentItem.title
let subtitle = contentItem.subtitle
let extras = contentItem.extras
let meta = contentItem.toString()
let actions = contentItem.actions
return Output.Item(title: title, subtitle: subtitle, extras: extras, meta: meta, actions: actions)
}
let outputPane = Output.Pane(name: pane.name,
alert: pane.alert,
x: x,
y: y,
width: pane.width,
height: pane.height,
items: outputItems)
return outputPane
}
let output = Output.Dashboard(name: name, panes: panes)
return output
}
var positionedPanes: [(Pane, Int, Int)] {
var maxWidth: Int = 0
var maxHeight: Int = 0
var positioned: [(Pane, Int, Int)] = []
var toBePlaced: [Pane] = []
for pane in self.panes() {
if let x = pane.x, (x + pane.width) > maxWidth {
maxWidth = x + pane.width
}
if let y = pane.y, (y + pane.height) > maxHeight {
maxHeight = y + pane.height
}
if let x = pane.x, let y = pane.y {
positioned.append((pane, x, y))
} else {
toBePlaced.append(pane)
}
}
if maxWidth == 0 && maxHeight == 0 {
maxWidth = Int(ceil(sqrt(Double(toBePlaced.count))))
maxHeight = maxWidth
let chunks = toBePlaced.chunked(into: maxHeight)
for (row, panes) in chunks.enumerated() {
for (col, pane) in panes.enumerated() {
positioned.append((pane, col, row))
}
}
}
return positioned
}
}