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

[#15] Replace newlines with ZPL equivalent #18

Merged
merged 3 commits into from
Sep 20, 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
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
Expand Up @@ -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,
Expand Down
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
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
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
Expand Up @@ -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", "\\&")
}
}
}

/**
Expand Down
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
Expand Up @@ -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
)
}

/**
Expand Down
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
Expand Up @@ -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)
}


Expand Down
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
Expand Up @@ -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
)
}

/**
Expand All @@ -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
)
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
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
Expand Up @@ -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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
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
Expand Up @@ -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)
}

/**
Expand Down
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
Expand Up @@ -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)
}


Expand Down
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
Expand Up @@ -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()) {
Expand All @@ -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
Expand Up @@ -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
Expand Up @@ -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
)
}