diff --git a/src/main/kotlin/com/sainsburys/k2zpl/builder/ZplBuilder.kt b/src/main/kotlin/com/sainsburys/k2zpl/builder/ZplBuilder.kt index ba93d29..c7e2f04 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/builder/ZplBuilder.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/builder/ZplBuilder.kt @@ -20,6 +20,13 @@ class ZplBuilder { private var _zplDpiSetting: ZplDpiSetting = ZplDpiSetting.Unset private var defaultFont: Font = Font(ZplFont.A, ZplFieldOrientation.NORMAL, 30.dots, 30.dots) + var verticalPosition: Int = 0 + private set + + fun advancePosition(byAmount: Int) { + verticalPosition += byAmount + } + var dpiSetting: ZplDpiSetting get() { if (_zplDpiSetting == ZplDpiSetting.Unset) { diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/LabelLength.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/LabelLength.kt index 40d3cf9..9a6edff 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/LabelLength.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/LabelLength.kt @@ -1,6 +1,7 @@ package com.sainsburys.k2zpl.command import com.sainsburys.k2zpl.builder.ZplBuilder +import com.sainsburys.k2zpl.command.options.ZplLabelLength internal data class LabelLength(val length: Int) : ZplCommand { init { @@ -12,20 +13,40 @@ internal data class LabelLength(val length: Int) : ZplCommand { } /** - * Sets the length of the label. + * Sets the length of the label using Int. * @param length The length of the label. */ fun ZplBuilder.labelLength(length: Int) { - command(LabelLength(length)) + labelLength(ZplLabelLength.Exact(length)) +} + +/** + * Sets the length of the label using ZplLabelLength. + * @param length The length of the label: + * - [ZplLabelLength.Exact] the exact value + * - [ZplLabelLength.CurrentPosition] use [ZplBuilder.verticalPosition] + */ +fun ZplBuilder.labelLength(length: ZplLabelLength) { + when (length) { + ZplLabelLength.CurrentPosition -> command { LabelLength(verticalPosition) } + is ZplLabelLength.Exact -> command(LabelLength(length.value)) + } } /** * Sets the length of the label by lambda * This allows for lazy evaluation of the length. * @param length lambda with context of the current [ZplBuilder] + * that returns an Int. */ -fun ZplBuilder.labelLength(length: ZplBuilder.() -> Int) { +fun ZplBuilder.labelLength(length: ZplBuilder.() -> ZplLabelLength) { command { - LabelLength(length.invoke(this)) + when (val result = length.invoke(this)) { + ZplLabelLength.CurrentPosition -> verticalPosition + is ZplLabelLength.Exact -> result.value + }.let { + LabelLength(it) + } } -} \ No newline at end of file +} + diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/options/ZplLabelLength.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/options/ZplLabelLength.kt new file mode 100644 index 0000000..7c6a39b --- /dev/null +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/options/ZplLabelLength.kt @@ -0,0 +1,8 @@ +@file:Suppress("UNUSED") + +package com.sainsburys.k2zpl.command.options + +sealed class ZplLabelLength { + data class Exact(val value: Int) : ZplLabelLength() + data object CurrentPosition : ZplLabelLength() +} diff --git a/src/test/kotlin/com/sainsburys/k2zpl/builder/ZplBuilderTest.kt b/src/test/kotlin/com/sainsburys/k2zpl/builder/ZplBuilderTest.kt index 310139b..21beeff 100644 --- a/src/test/kotlin/com/sainsburys/k2zpl/builder/ZplBuilderTest.kt +++ b/src/test/kotlin/com/sainsburys/k2zpl/builder/ZplBuilderTest.kt @@ -49,6 +49,18 @@ class ZplBuilderTest : DescribeSpec({ subject.dpiSetting shouldBe ZplDpiSetting.DPI_203 } } + describe("advancePosition") { + it("defaults to zero") { + subject.verticalPosition shouldBe 0 + } + it("adds value to the vertical position") { + subject.advancePosition(100) + subject.verticalPosition shouldBe 100 + } + it("adds cm value to the vertical position") { + subject.verticalPosition shouldBe 100 + } + } describe("Int mm extension") { it("throws an appropriate exception when no ZplDpiSetting") { shouldThrow { diff --git a/src/test/kotlin/com/sainsburys/k2zpl/command/LabelLengthTest.kt b/src/test/kotlin/com/sainsburys/k2zpl/command/LabelLengthTest.kt index ec0a51f..a90b8cd 100644 --- a/src/test/kotlin/com/sainsburys/k2zpl/command/LabelLengthTest.kt +++ b/src/test/kotlin/com/sainsburys/k2zpl/command/LabelLengthTest.kt @@ -1,5 +1,6 @@ package com.sainsburys.k2zpl.command +import com.sainsburys.k2zpl.command.options.ZplLabelLength import com.sainsburys.k2zpl.k2zpl import com.sainsburys.k2zpl.testBuildString import io.kotest.assertions.throwables.shouldThrow @@ -39,15 +40,43 @@ class LabelLengthTest : DescribeSpec({ } result shouldBe "^LL1000\n" } - it("outputs correct command when using lambda") { + it("outputs correct command when using Exact") { + val result = k2zpl { + labelLength(ZplLabelLength.Exact(100)) + } + result shouldBe "^LL100\n" + } + it("outputs correct command when using lambda Exact") { var size = 100 val result = k2zpl { labelLength { - size + ZplLabelLength.Exact(size) } size += 100 } result shouldBe "^LL200\n" } + it("outputs correct command when using CurrentPosition") { + val result = k2zpl { + labelLength(ZplLabelLength.CurrentPosition) + advancePosition(200) + } + result shouldBe "^LL200\n" + } + it("outputs correct command when using CurrentPosition with multiple advancePosition") { + val result = k2zpl { + advancePosition(200) + labelLength(ZplLabelLength.CurrentPosition) + advancePosition(200) + } + result shouldBe "^LL400\n" + } + it("outputs correct command when using lambda CurrentPosition") { + val result = k2zpl { + labelLength { ZplLabelLength.CurrentPosition } + advancePosition(200) + } + result shouldBe "^LL200\n" + } } }) \ No newline at end of file