Scoop API

scoop:scoop.terminal:0.1.0

src/event/keyboard.scoop

Selected production source · src/event/keyboard.scoop

1package scoop.terminal.event
3import scoop.terminal.Command
5/**
6 * A keyboard-protocol capability that can be requested from supporting terminals.
7 */
8public enum KeyboardEnhancement {
9 /**
10 * Requests unambiguous escape-encoded key reporting.
11 */
12 DisambiguateEscapeCodes,
13 /**
14 * Requests press, repeat, and release event distinctions.
15 */
16 ReportEventTypes,
17 /**
18 * Requests alternate-key information in enhanced reports.
19 */
20 ReportAlternateKeys,
21 /**
22 * Requests escape-code reporting for all keys.
23 */
24 ReportAllKeysAsEscapeCodes
26 internal val mask: UInt get() = when (this) {
27 case DisambiguateEscapeCodes -> 1u; case ReportEventTypes -> 2u
28 case ReportAlternateKeys -> 4u; case ReportAllKeysAsEscapeCodes -> 8u
29 }
32/**
33 * An immutable bit set of requested or reported keyboard enhancements.
34 */
35public struct KeyboardEnhancementFlags(val bits: UInt = 0u) {
36 /**
37 * Whether no bits are set.
38 */
39 public val isEmpty: Boolean get() = bits == 0u
40 /**
41 * Tests whether the selected flag is present.
42 */
43 public fun has(flag: KeyboardEnhancement): Boolean = (bits and flag.mask) != 0u
44 /**
45 * Returns a copy with the selected flag included.
46 */
47 public fun with(flag: KeyboardEnhancement): KeyboardEnhancementFlags = KeyboardEnhancementFlags(bits or flag.mask)
48 /**
49 * Returns a copy with the selected flag removed.
50 */
51 public fun without(flag: KeyboardEnhancement): KeyboardEnhancementFlags =
52 KeyboardEnhancementFlags(bits and flag.mask.inv())
55/**
56 * Pushes the selected enhanced-keyboard flags onto the terminal's protocol stack.
57 */
58public struct PushKeyboardEnhancementFlags(val flags: KeyboardEnhancementFlags) : Command {
59 /**
60 * Writes this command's ANSI representation to the supplied writer without flushing it.
61 * Parameter validation happens before or during serialization.
62 */
63 public override fun writeAnsi(writer: TextWriter) {
64 if ((flags.bits and 15u.inv()) != 0u) throw IllegalArgumentException(Some("unknown keyboard enhancement flag"))
65 writer.write("\u001b[>" + flags.bits.toString() + "u")
66 }
69/**
70 * Restores the previous enhanced-keyboard flags from the terminal's stack.
71 */
72public object PopKeyboardEnhancementFlags : Command {
73 /**
74 * Writes this command's ANSI representation to the supplied writer without flushing it.
75 * Parameter validation happens before or during serialization.
76 */
77 public override fun writeAnsi(writer: TextWriter) { writer.write("\u001b[<1u") }