terminal colors, multi-line strings; bump to 0.8

This commit is contained in:
alex-s168
2024-03-28 22:44:15 +01:00
parent bd60bf5b27
commit e8d4fb16b1
9 changed files with 230 additions and 34 deletions

View File

@@ -0,0 +1,53 @@
package blitz.str
class MutMultiLineString(
var fill: Char
) {
val lines = mutableListOf<MutString>()
// TODO: wrap at \n
/** if out of bounds, extends with @see fill */
operator fun get(row: Int, col: Int): Char {
if (row >= lines.size) {
repeat(row - lines.size + 1) {
lines.add(MutString(fill = fill))
}
}
return lines[row][col]
}
/** if out of bounds, extends with @see fill */
operator fun set(row: Int, col: Int, value: Char) {
if (row >= lines.size) {
repeat(row - lines.size + 1) {
lines.add(MutString(fill = fill))
}
} else {
lines[row].fill = fill
}
lines[row][col] = value
}
/** if out of bounds, extends with @see fill */
operator fun set(row: Int, colStart: Int, value: CharSequence) {
if (row >= lines.size) {
repeat(row - lines.size + 1) {
lines.add(MutString(fill = fill))
}
} else {
lines[row].fill = fill
}
lines[row][colStart] = value
}
/** if out of bounds, extends with @see fill */
operator fun set(rowStart: Int, colStart: Int, value: MutMultiLineString) {
value.lines.forEachIndexed { index, line ->
this[index + rowStart, colStart] = line
}
}
override fun toString(): String =
lines.joinToString(separator = "\n")
}

View File

@@ -0,0 +1,61 @@
package blitz.str
class MutString(
init: String = "",
var fill: Char
): CharSequence, Appendable {
private val builder = StringBuilder(init)
override val length: Int
get() = builder.length
override operator fun get(index: Int): Char {
if (index >= length) {
repeat(index - length + 1) {
builder.append(fill)
}
}
return builder[index]
}
/** if out of bounds, extends with @see fill */
operator fun set(index: Int, value: Char) {
if (index >= length) {
repeat(index - length + 1) {
builder.append(fill)
}
}
builder[index] = value
}
/** if out of bounds, extends with @see fill */
operator fun set(start: Int, str: CharSequence) {
if (start >= length) {
repeat(start - length + 1) {
builder.append(fill)
}
}
builder.insert(start, str)
}
override fun toString(): String =
builder.toString()
override fun append(csq: CharSequence?): Appendable {
builder.append(csq)
return this
}
override fun append(csq: CharSequence?, start: Int, end: Int): Appendable {
builder.append(csq, start, end)
return this
}
override fun append(c: Char): Appendable {
builder.append(c)
return this
}
override fun subSequence(startIndex: Int, endIndex: Int): CharSequence =
builder.subSequence(startIndex, endIndex)
}