Share constants for ANSI control codes

This commit is contained in:
Jake Wharton
2023-02-22 00:02:43 -05:00
committed by Jake Wharton
parent 3c18954fb0
commit 66eca84c5d
5 changed files with 13 additions and 12 deletions

View File

@ -0,0 +1,4 @@
package com.jakewharton.mosaic
internal const val clearLine = "\u001B[K"
internal const val cursorUp = "\u001B[F"

View File

@ -47,14 +47,14 @@ internal class AnsiRendering : Rendering {
clear() clear()
repeat(lastHeight) { repeat(lastHeight) {
append("\u001B[F") // Cursor up line. append(cursorUp)
} }
val staticLines = statics.flatMap { it.render().split("\n") } val staticLines = statics.flatMap { it.render().split("\n") }
val lines = canvas.render().split("\n") val lines = canvas.render().split("\n")
for (line in staticLines + lines) { for (line in staticLines + lines) {
append(line) append(line)
append("\u001B[K") // Clear rest of line. append(clearLine)
append('\n') append('\n')
} }
@ -64,12 +64,12 @@ internal class AnsiRendering : Rendering {
if (i > 0) { if (i > 0) {
append('\n') append('\n')
} }
append("\u001B[K") // Clear line. append(clearLine)
} }
// Move cursor back up to end of the new output. // Move cursor back up to end of the new output.
repeat(extraLines - 1) { repeat(extraLines - 1) {
append("\u001B[F") // Cursor up line. append(cursorUp)
} }
lastHeight = lines.size lastHeight = lines.size

View File

@ -16,8 +16,8 @@ class AnsiRenderingTest {
// TODO We should not draw trailing whitespace. // TODO We should not draw trailing whitespace.
assertEquals( assertEquals(
""" """
|Hello $esc[K |Hello $clearLine
|World!$esc[K |World!$clearLine
|""".trimMargin(), |""".trimMargin(),
rendering.render(helloCanvas).toString(), rendering.render(helloCanvas).toString(),
) )

View File

@ -13,9 +13,9 @@ class MosaicTest {
} }
} }
assertEquals(""" assertEquals("""
|One $esc[K |One $clearLine
|Two $esc[K |Two $clearLine
|Three$esc[K |Three$clearLine
|""".trimMargin(), actual) |""".trimMargin(), actual)
} }
} }

View File

@ -1,3 +0,0 @@
package com.jakewharton.mosaic
internal const val esc = "\u001B"