scoop:scoop.readline:0.1.0
src/render/layout.scoop
Selected production source · src/render/layout.scoop
1
package scoop.readline3
import scoop.charset.*4
import scoop.text.*6
/** A viewport of ANSI-decorated rows with a zero-based cursor position. */7
public struct RenderedLine(val lines: Array<String>, val cursorRow: Long, val cursorColumn: Long)9
/** Computes a terminal viewport without performing I/O. */10
public fun renderLine(11
prompt: String, session: EditingSession, columns: Long, rows: Long,12
final: Boolean = false13
): RenderedLine {14
if (columns <= 0L || rows <= 0L) throw IllegalArgumentException(Some("invalid terminal size"))15
val layout = LineLayout(columns, session.config.tabStop)16
val colors = session.config.colorMode != ColorMode.Disabled17
val highlighter = if (colors) session.highlighter else PlainHighlighter18
val promptText = stripDecoration(prompt)19
layout.append(promptText, if (colors) highlighter.highlightPrompt(prompt) else promptText, -1L)20
val styled = if (final) session.line else highlighter.highlight(session.line, session.cursor)21
layout.append(session.line, styled, if (final) session.line.charLength else session.cursor)22
val hint = if (final) None else session.hint23
if (hint != None) layout.append(hint!!.display, highlighter.highlightHint(hint!!.display), -1L)24
val message = session.message25
if (message != None && message!!.byteLength > 0L) {26
layout.append("\n" + message!!, "\n" + message!!, -1L)27
}28
return layout.finish(rows)29
}31
internal fun clusterWidth(text: String): Long {32
var width = 0L33
var emoji = false34
var regional = 0L35
for (character in text) {36
val code = character.code37
if (code < 32 || code == 127) { width += 2L; continue }38
val scalarWidth = unicodeScalarWidth(character).toLong()39
if (scalarWidth > width) width = scalarWidth40
if (code == 0xfe0f || code == 0x20e3) emoji = true41
if (code >= 0x1f1e6 && code <= 0x1f1ff) regional += 1L42
if (scalarWidth < 0L) width += 1L43
}44
return if (emoji || regional == 2L) 2L else width45
}47
private class LineLayout(private val columns: Long, private val tabStop: Long) {48
private val lines: ArrayList<String> = ArrayList<String>()49
private val row: StringBuilder = StringBuilder()50
private var column: Long = 0L51
private var cursorRow: Long = 0L52
private var cursorColumn: Long = 0L53
private var decoration: String = ""55
fun append(plain: String, styled: String, cursor: Long) {56
val pieces = styledGraphemes(plain, styled)57
var offset = 0L58
for (piece in pieces) {59
val text = piece.plain60
if (offset == cursor) { cursorRow = lines.size; cursorColumn = column }61
if (text == "\n" || text == "\r\n") {62
row.append(piece.decoration)63
decoration += piece.decoration64
newRow()65
} else if (text == "\t") {66
val count = tabStop - column % tabStop67
row.append(piece.decoration)68
decoration += piece.decoration69
var left = count70
while (left > 0L) {71
if (column == columns) newRow()72
row.append(' ')73
column += 1L74
left -= 1L75
}76
} else {77
val width = clusterWidth(text)78
if (column > 0L && width > columns - column) newRow()79
if (offset == cursor) { cursorRow = lines.size; cursorColumn = column }80
row.append(piece.decoration)81
decoration += piece.decoration82
val visible = if (width > columns) "�" else text83
for (character in visible) {84
if (character.code < 32 || character.code == 127) {85
row.append('^')86
row.append((if (character.code == 127) 63 else character.code + 64).toChar())87
} else if (unicodeScalarWidth(character) < 0) row.append('�')88
else row.append(character)89
}90
column += if (width > columns) 1L else width91
}92
offset += text.charLength93
if (column == columns) newRow()94
}95
if (offset == cursor) { cursorRow = lines.size; cursorColumn = column }96
}98
private fun newRow() {99
lines.add(row.toString() + "\u001b[0m")100
row.clear().append(decoration)101
column = 0L102
}103
fun finish(height: Long): RenderedLine {104
lines.add(row.toString() + "\u001b[0m")105
val start = if (cursorRow >= height) cursorRow - height + 1L else 0L106
val count = if (lines.size - start < height) lines.size - start else height107
return RenderedLine(Array<String>(count) { index -> lines[start + index] }, cursorRow - start, cursorColumn)108
}109
}