terminal colors, multi-line strings; bump to 0.8
This commit is contained in:
53
src/main/kotlin/blitz/str/MutMultiLineString.kt
Normal file
53
src/main/kotlin/blitz/str/MutMultiLineString.kt
Normal 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")
|
||||
}
|
61
src/main/kotlin/blitz/str/MutString.kt
Normal file
61
src/main/kotlin/blitz/str/MutString.kt
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user