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

View File

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

View File

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

View File

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