Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#8] Add method to track current vertical position #25

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 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,25 @@ class ZplBuilder {
private var _zplDpiSetting: ZplDpiSetting = ZplDpiSetting.Unset
private var defaultFont: Font = Font(ZplFont.A, ZplFieldOrientation.NORMAL, 30.dots, 30.dots)

/**
* The current vertical or Y position of the label. This can be used
* as needed to keep track of a label height to be used in combination
* with the [com.sainsburys.k2zpl.command.LabelLength] command, as well as
* any command that requires a Y position value.
*/
var verticalPosition: Int = 0
private set

/**
* Advance the [verticalPosition] value by the amount specified.
* Can also be used in combination with the [cm], [mm], [dots] functions
* to indicate measurement type.
* @param byAmount The amount to increment the [verticalPosition] by.
*/
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()
}
11 changes: 11 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,17 @@ 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
subject.advancePosition(50)
subject.verticalPosition shouldBe 150
}
}
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"
}
}
})