Skip to content

Commit

Permalink
[#8] Add method to track current vertical position
Browse files Browse the repository at this point in the history
- Adds `verticalPosition` in `ZplBuilder` to allow tracking of position
- Add `advancePosition` to allow control of `currentPosition`
- Update use of `labelLength` to take either `Exact` or `CurrentPosition` values.
  • Loading branch information
itsmattking committed Oct 26, 2024
1 parent 2b94151 commit 6e986fe
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/main/kotlin/com/sainsburys/k2zpl/builder/ZplBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
31 changes: 26 additions & 5 deletions src/main/kotlin/com/sainsburys/k2zpl/command/LabelLength.kt
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
}
}
}
}

Original file line number Diff line number Diff line change
@@ -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()
}
12 changes: 12 additions & 0 deletions src/test/kotlin/com/sainsburys/k2zpl/builder/ZplBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<IllegalStateException> {
Expand Down
33 changes: 31 additions & 2 deletions src/test/kotlin/com/sainsburys/k2zpl/command/LabelLengthTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
}
}
})

0 comments on commit 6e986fe

Please sign in to comment.