Skip to content

Instantly share code, notes, and snippets.

View pencelab's full-sized avatar
☯️

Glenn pencelab

☯️
View GitHub Profile
inline fun Panel.square(init: SquareBuilder.() -> Unit): Square {
return SquareBuilder().apply { init() }.build().also { this.addShape(it) }
}
@ShapeDsl
class SquareBuilder: ShapeBuilder() {
override fun build() = Square(lines, char)
}
inline fun panel(init: (@ShapeDsl Panel).() -> Unit): Panel {
return Panel().apply { init() }
}
inline fun Panel.square(init: (@ShapeDsl SquareBuilder).() -> Unit): Square {
return SquareBuilder().apply { init() }.build().also { this.addShape(it) }
}
@DslMarker
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE)
annotation class ShapeDsl
panel {
square {
lines = 8
char = 'd'
square {
lines = 5
char = 'o'
triangle {
inline fun panel(init: Panel.() -> Unit): Panel {
return Panel().apply { init() }
}
inline fun Panel.square(init: SquareBuilder.() -> Unit): Square {
return SquareBuilder().apply { init() }.build().also { this.addShape(it) }
}
inline fun Panel.triangle(init: TriangleBuilder.() -> Unit): Triangle {
return TriangleBuilder().apply { init() }.build().also { this.addShape(it) }
operator fun Shape.plus(shape: Shape): ComposedShape = this union shape
operator fun Shape.minus(shape: Shape): ComposedShape = this intersection shape
panel {
val square = square {
lines = 8
char = 'd'
}
space()
val triangle = triangle {
lines = 7
char = 's'
}
panel {
...
space()
addShape(square union rhombus)
...
space()
composed { ComposedShape(rhombus, triangle, ComposedShape.Operation.INTERSECTION) }
...
}.print()
fun Panel.composed(init: Panel.() -> ComposedShape): ComposedShape {
return init().also { this.addShape(it) }
}