Scoop API

scoop:scoop.readline:0.1.0

src/render/layout.scoop

Selected production source · src/render/layout.scoop

1package scoop.readline
3import scoop.charset.*
4import scoop.text.*
6/** A viewport of ANSI-decorated rows with a zero-based cursor position. */
7public struct RenderedLine(val lines: Array<String>, val cursorRow: Long, val cursorColumn: Long)
9/** Computes a terminal viewport without performing I/O. */
10public fun renderLine(
11 prompt: String, session: EditingSession, columns: Long, rows: Long,
12 final: Boolean = false
13): 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.Disabled
17 val highlighter = if (colors) session.highlighter else PlainHighlighter
18 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.hint
23 if (hint != None) layout.append(hint!!.display, highlighter.highlightHint(hint!!.display), -1L)
24 val message = session.message
25 if (message != None && message!!.byteLength > 0L) {
26 layout.append("\n" + message!!, "\n" + message!!, -1L)
27 }
28 return layout.finish(rows)
31internal fun clusterWidth(text: String): Long {
32 var width = 0L
33 var emoji = false
34 var regional = 0L
35 for (character in text) {
36 val code = character.code
37 if (code < 32 || code == 127) { width += 2L; continue }
38 val scalarWidth = unicodeScalarWidth(character).toLong()
39 if (scalarWidth > width) width = scalarWidth
40 if (code == 0xfe0f || code == 0x20e3) emoji = true
41 if (code >= 0x1f1e6 && code <= 0x1f1ff) regional += 1L
42 if (scalarWidth < 0L) width += 1L
43 }
44 return if (emoji || regional == 2L) 2L else width
47private 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 = 0L
51 private var cursorRow: Long = 0L
52 private var cursorColumn: Long = 0L
53 private var decoration: String = ""
55 fun append(plain: String, styled: String, cursor: Long) {
56 val pieces = styledGraphemes(plain, styled)
57 var offset = 0L
58 for (piece in pieces) {
59 val text = piece.plain
60 if (offset == cursor) { cursorRow = lines.size; cursorColumn = column }
61 if (text == "\n" || text == "\r\n") {
62 row.append(piece.decoration)
63 decoration += piece.decoration
64 newRow()
65 } else if (text == "\t") {
66 val count = tabStop - column % tabStop
67 row.append(piece.decoration)
68 decoration += piece.decoration
69 var left = count
70 while (left > 0L) {
71 if (column == columns) newRow()
72 row.append(' ')
73 column += 1L
74 left -= 1L
75 }
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.decoration
82 val visible = if (width > columns) "�" else text
83 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 width
91 }
92 offset += text.charLength
93 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 = 0L
102 }
103 fun finish(height: Long): RenderedLine {
104 lines.add(row.toString() + "\u001b[0m")
105 val start = if (cursorRow >= height) cursorRow - height + 1L else 0L
106 val count = if (lines.size - start < height) lines.size - start else height
107 return RenderedLine(Array<String>(count) { index -> lines[start + index] }, cursorRow - start, cursorColumn)
108 }