scoop:scoop.terminal:0.1.0
src/internal/input/items.scoop
Selected production source · src/internal/input/items.scoop
1
package scoop.terminal.internal.input3
import scoop.terminal.protocol.*4
import scoop.terminal.event.*6
internal fun InputItem?.hasItem(): Boolean = when (this) { case None -> false; case Some(_) -> true }8
internal enum InputMode { Ground, Escape, Csi, Ss3, LinuxFunction, Mouse, Utf8, Paste }10
internal class InputDecodeException internal constructor(public val detail: String) : Exception(Some(detail))12
internal fun invalidInput(detail: String): Nothing { throw InputDecodeException(detail) }14
internal fun decimal(text: String, maximum: Long): Long {15
if (text == "") invalidInput("missing terminal protocol number")16
var value = 0L17
for (character in text) {18
val digit = (character.code - 48).toLong()19
if (digit < 0L || digit > 9L || value > (maximum - digit) / 10L || digit > maximum) {20
invalidInput("invalid or overflowing terminal protocol number")21
}22
value = value * 10L + digit23
}24
return value25
}27
internal fun scalar(text: String): Char {28
val value = decimal(text, 1114111L).toInt()29
if (value >= 55296 && value <= 57343) invalidInput("terminal key is not a Unicode scalar")30
return value.toChar()31
}33
internal fun userKey(code: KeyCode, modifiers: KeyModifiers = KeyModifiers(),34
kind: KeyEventKind = KeyEventKind.Press, state: KeyEventState = KeyEventState()): InputItem =35
InputItem.User(Event.Key(KeyEvent(code, modifiers, kind, state)))