Skip to content

Commit

Permalink
Merge pull request #18 from sainsburys-tech/issue/15
Browse files Browse the repository at this point in the history
[#15] Replace newlines with ZPL equivalent
itsmattking authored Sep 20, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents a9df360 + 99e83b5 commit 500eaa9
Showing 14 changed files with 87 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/sainsburys/k2zpl/command/BarCode.kt
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ internal data class BarCode(
}

override val command: CharSequence = "^B1"
override val parameters: LinkedHashMap<CharSequence, Any?> = linkedMapOf(
override val parameters: Map<CharSequence, Any?> = addParameters(
"o" to orientation.code,
"c" to checkDigit.toString(),
"h" to height,
4 changes: 2 additions & 2 deletions src/main/kotlin/com/sainsburys/k2zpl/command/FieldBlock.kt
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ internal data class FieldBlock(
}

override val command: CharSequence = "^FB"
override val parameters: LinkedHashMap<CharSequence, Any?> = linkedMapOf(
override val parameters: Map<CharSequence, Any?> = addParameters(
"w" to width,
"l" to maxLines,
"s" to lineSpacing,
@@ -58,7 +58,7 @@ fun ZplBuilder.fieldBlock(
* @param x horizontal position of fieldBlock
* @param y vertical position of fieldBlock
* @param width The width of the text block.
* @param data content of block
* @param data content of block. Any newlines are substituted with \&
* @param maxLines The number of lines in the text block.
* @param lineSpacing The space between lines.
* @param alignment The text alignment within the block.
17 changes: 16 additions & 1 deletion src/main/kotlin/com/sainsburys/k2zpl/command/FieldData.kt
Original file line number Diff line number Diff line change
@@ -4,7 +4,22 @@ import com.sainsburys.k2zpl.builder.ZplBuilder

internal data class FieldData(val data: String) : ZplCommand {
override val command: CharSequence = "^FD"
override val parameters: LinkedHashMap<CharSequence, Any?> = linkedMapOf("d" to data)
override val parameters: Map<CharSequence, Any?> = addParameters("d" to data)

override fun build(stringBuilder: StringBuilder): StringBuilder {
return stringBuilder
.append(command)
.append(
(parameters["d"] as? CharSequence).convertNewlines()
)
}

private fun CharSequence?.convertNewlines(): String {
return when {
this == null -> ""
else -> toString().replace("\n", "\\&")
}
}
}

/**
10 changes: 5 additions & 5 deletions src/main/kotlin/com/sainsburys/k2zpl/command/FieldOrigin.kt
Original file line number Diff line number Diff line change
@@ -14,11 +14,11 @@ internal data class FieldOrigin(
}

override val command: CharSequence = "^FO"
override val parameters: LinkedHashMap<CharSequence, Any?> = buildLinkedMap {
put("x", x)
put("y", y)
put("j", justification)
}
override val parameters: Map<CharSequence, Any?> = addParameters(
"x" to x,
"y" to y,
"j" to justification
)
}

/**
4 changes: 2 additions & 2 deletions src/main/kotlin/com/sainsburys/k2zpl/command/Font.kt
Original file line number Diff line number Diff line change
@@ -17,8 +17,8 @@ internal data class Font(
}

override val command: CharSequence = "^A${font}"
override val parameters: LinkedHashMap<CharSequence, Any?> =
linkedMapOf("o" to orientation, "h" to height, "w" to width)
override val parameters: Map<CharSequence, Any?> =
addParameters("o" to orientation, "h" to height, "w" to width)
}


29 changes: 16 additions & 13 deletions src/main/kotlin/com/sainsburys/k2zpl/command/GraphicBox.kt
Original file line number Diff line number Diff line change
@@ -18,12 +18,13 @@ internal data class GraphicBox(
}

override val command: CharSequence = "^GB"
override val parameters: LinkedHashMap<CharSequence, Any?> =
buildLinkedMap {
putAll(
mapOf("w" to width, "h" to height, "t" to thickness, "c" to color.code, "r" to rounding)
)
}
override val parameters: Map<CharSequence, Any?> =
addParameters(
"w" to width,
"h" to height,
"t" to thickness, "c" to color.code,
"r" to rounding
)
}

/**
@@ -41,13 +42,15 @@ fun ZplBuilder.graphicBox(
color: ZplLineColor = ZplLineColor.BLACK,
rounding: Int = 0
) {
command(GraphicBox(
width = width,
height = height,
thickness = thickness,
color = color,
rounding = rounding
))
command(
GraphicBox(
width = width,
height = height,
thickness = thickness,
color = color,
rounding = rounding
)
)
}

/**
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ internal data class GraphicField(
}

override val command: CharSequence = "^GF"
override val parameters: LinkedHashMap<CharSequence, Any?> = linkedMapOf(
override val parameters: Map<CharSequence, Any?> = addParameters(
"f" to format,
"db" to dataBytes,
"tb" to totalBytes,
2 changes: 1 addition & 1 deletion src/main/kotlin/com/sainsburys/k2zpl/command/LabelHome.kt
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import com.sainsburys.k2zpl.builder.ZplBuilder

internal data class LabelHome(val x: Int, val y: Int) : ZplCommand {
override val command: CharSequence = "^LH"
override val parameters: LinkedHashMap<CharSequence, Any?> = linkedMapOf("x" to x, "y" to y)
override val parameters: Map<CharSequence, Any?> = addParameters("x" to x, "y" to y)
}

/**
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ internal data class LabelLength(val length: Int) : ZplCommand {
}

override val command: CharSequence = "^LL"
override val parameters: LinkedHashMap<CharSequence, Any?> = linkedMapOf("l" to length)
override val parameters: Map<CharSequence, Any?> = addParameters("l" to length)
}

/**
4 changes: 2 additions & 2 deletions src/main/kotlin/com/sainsburys/k2zpl/command/MediaMode.kt
Original file line number Diff line number Diff line change
@@ -10,8 +10,8 @@ internal data class MediaMode(
val prePeelSelect: ZplYesNo
) : ZplCommand {
override val command: CharSequence = "^MM"
override val parameters: LinkedHashMap<CharSequence, Any?> =
linkedMapOf("m" to mediaMode, "p" to prePeelSelect)
override val parameters: Map<CharSequence, Any?> =
addParameters("m" to mediaMode, "p" to prePeelSelect)
}

/**
2 changes: 1 addition & 1 deletion src/main/kotlin/com/sainsburys/k2zpl/command/PrintWidth.kt
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ internal data class PrintWidth(val width: Int) : ZplCommand {
}

override val command: CharSequence = "^PW"
override val parameters: LinkedHashMap<CharSequence, Any?> = linkedMapOf("w" to width)
override val parameters: Map<CharSequence, Any?> = addParameters("w" to width)
}


9 changes: 6 additions & 3 deletions src/main/kotlin/com/sainsburys/k2zpl/command/ZplCommand.kt
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ package com.sainsburys.k2zpl.command

interface ZplCommand {
val command: CharSequence
val parameters: LinkedHashMap<CharSequence, Any?> get() = linkedMapOf()
val parameters: Map<CharSequence, Any?> get() = addParameters()
fun build(stringBuilder: StringBuilder) = stringBuilder.apply {
append(command)
with(parameters.values.iterator()) {
@@ -25,5 +25,8 @@ private fun <T> Iterator<T>.nextNotNull(block: (T) -> Unit) {
next()?.let { block(it) }
}

internal fun <K, V> buildLinkedMap(block: LinkedHashMap<K, V>.() -> Unit) =
linkedMapOf<K, V>().apply(block)
/**
* A shortcut to adding parameters that helps to enforce use of [LinkedHashMap]
* so that entry order is preserved.
*/
internal fun <K, V> ZplCommand.addParameters(vararg pairs: Pair<K, V>) = linkedMapOf(*pairs)
18 changes: 18 additions & 0 deletions src/test/kotlin/com/sainsburys/k2zpl/command/FieldDataTest.kt
Original file line number Diff line number Diff line change
@@ -23,5 +23,23 @@ class FieldDataTest : DescribeSpec({
}
result shouldBe "^FDsome-other-data\n"
}
it("outputs correct command using raw string") {
val result = k2zpl {
val data =
"""
some data
with line breaks
""".trimIndent()
fieldData(data = data)
}
result shouldBe "^FDsome data\\&with line breaks\\&\n"
}
it("outputs correct command using string") {
val result = k2zpl {
fieldData(data = "some data\nwith line breaks\n")
}
result shouldBe "^FDsome data\\&with line breaks\\&\n"
}
}
})
27 changes: 15 additions & 12 deletions src/test/kotlin/com/sainsburys/k2zpl/command/ZplCommandTest.kt
Original file line number Diff line number Diff line change
@@ -33,28 +33,31 @@ class ZplCommandWithoutParameters : ZplCommand {

class ZplCommandWithOneParameter : ZplCommand {
override val command = "^ZCP"
override val parameters: LinkedHashMap<CharSequence, Any?> = buildLinkedMap {
putAll(mapOf("param-one" to "value-one"))
}
override val parameters: Map<CharSequence, Any?> = addParameters(
"param-one" to "value-one"
)
}

class ZplCommandWithMultipleParameters : ZplCommand {
override val command = "^ZCPS"
override val parameters: LinkedHashMap<CharSequence, Any?> = buildLinkedMap {
putAll(mapOf("param-one" to "value-one", "param-two" to "value-two"))
}
override val parameters: Map<CharSequence, Any?> = addParameters(
"param-one" to "value-one",
"param-two" to "value-two"
)
}

class ZplCommandWitNullFirstParameter : ZplCommand {
override val command = "^ZCPN"
override val parameters: LinkedHashMap<CharSequence, Any?> = buildLinkedMap {
putAll(mapOf("param-one" to null, "param-two" to "value-two"))
}
override val parameters: Map<CharSequence, Any?> = addParameters(
"param-one" to null,
"param-two" to "value-two"
)
}

class ZplCommandWitNullSecondParameter : ZplCommand {
override val command = "^ZCPNS"
override val parameters: LinkedHashMap<CharSequence, Any?> = buildLinkedMap {
putAll(mapOf("param-one" to "value-one", "param-two" to null))
}
override val parameters: Map<CharSequence, Any?> = addParameters(
"param-one" to "value-one",
"param-two" to null
)
}

0 comments on commit 500eaa9

Please sign in to comment.