Skip to content

Change the handling of out-of-bounds canvas #255

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

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import de.cketti.codepoints.appendCodePoint

private val blankPixel = TextPixel(' ')

/**
* Returns always a new blank [TextPixel].
*/
private inline fun newBlankPixel(): TextPixel = TextPixel(' ')

public interface TextCanvas {
public val height: Int
public val width: Int
Expand All @@ -35,19 +40,32 @@ internal class TextSurface(

private val cells = Array(width * height) { TextPixel(' ') }

/**
* It is used in places where the [TextPixel] state is not important
* and it can change.
*/
private val reusableDirtyPixel: TextPixel = newBlankPixel()

/**
* It is used in places where it is important that the [TextPixel]
* has its original state and **will not change**.
*/
private val reusableBlankPixel: TextPixel = newBlankPixel()

operator fun get(row: Int, column: Int): TextPixel {
val x = translationX + column
val y = row + translationY
check(x in 0 until width)
check(y in 0 until height)
if (x >= width || y >= height || x < 0 || y < 0) {
return reusableDirtyPixel
}
return cells[y * width + x]
}

override fun appendRowTo(appendable: Appendable, row: Int, ansiLevel: AnsiLevel) {
// Reused heap allocation for building ANSI attributes inside the loop.
val attributes = mutableIntListOf()

var lastPixel = blankPixel
var lastPixel = reusableBlankPixel

val rowStart = row * width
val rowStop = rowStart + width
Expand Down
Loading