Scoop API

scoop:scoop.asyncio:0.1.0

src/tty.scoop

Selected production source · src/tty.scoop

1package scoop.asyncio
3import scoop.platform.memory.NativeByteBuffer
4import scoop.time.Duration
6/**
7 * Terminal dimensions in character cells and optional platform-reported pixel dimensions.
8 */
9public struct TtySize(
10 val columns: Long,
11 val rows: Long,
12 val widthPixels: Long,
13 val heightPixels: Long
16/**
17 * The outcome of a terminal read: bytes, resize notification, end of input, or timeout.
18 */
19public enum TtyRead {
20 /**
21 * The returned count of bytes appended to the input buffer.
22 */
23 Bytes(val count: Long),
24 /**
25 * A new terminal size in columns and rows.
26 */
27 Resize(val columns: Long, val rows: Long),
28 /**
29 * Terminal input has ended.
30 */
31 End,
32 /**
33 * No input or resize was delivered before the requested timeout.
34 */
35 TimedOut
38/**
39 * An owned asynchronous terminal handle with byte output, input/resize events, and raw-mode
40 * control.
41 */
42public interface AsyncTty: AsyncByteSink, AsyncCloseable {
43 /**
44 * Whether terminal cleanup has completed.
45 */
46 public val isClosed: Boolean
47 /**
48 * Waits for input, resize, EOF, or an optional timeout, appending bytes to the writable buffer
49 * region.
50 */
51 public suspend fun receive(
52 buffer: NativeByteBuffer,
53 count: Long,
54 timeout: Duration? = None
55 ): TtyRead
56 /**
57 * Queries current terminal cell and pixel dimensions.
58 */
59 public suspend fun size(): TtySize
60 /**
61 * Reports whether raw input mode is active for this terminal.
62 */
63 public suspend fun isRawMode(): Boolean
64 /**
65 * Enables raw terminal input under the handle's mode-ownership rules.
66 */
67 public suspend fun enableRawMode()
68 /**
69 * Restores the terminal mode saved when raw input was enabled.
70 */
71 public suspend fun disableRawMode()
74internal class TtyEndpoint(internal val owner: OwnedEndpoint): AsyncTty {
75 private val outputTransfer = TtyWrite(owner)
76 init {
77 owner.installTransfers(None, Some(outputTransfer))
78 }
79 internal var pendingResize: TtyRead? = None
80 public override val isClosed: Boolean get() = owner.isClosed
82 public override suspend fun receive(
83 buffer: NativeByteBuffer,
84 count: Long,
85 timeout: Duration?
86 ): TtyRead = receiveTty(this, buffer, count, timeout)
87 public override suspend fun writeFrom(buffer: NativeByteBuffer, count: Long): Long =
88 suspendCoroutine(outputTransfer.prepare(buffer, count, owner.loop.requireJob()))
89 public override suspend fun flush() {
90 owner.loop.requireJob();
91 owner.reserve(true).dispose()
92 }
93 public override suspend fun close() {
94 owner.close()
95 }
97 public override suspend fun size(): TtySize =
98 when (controlTty(owner, TtyControl.Size)) {
99 case IoResult.TtySizeValue(val size) -> size
100 case _ -> throw IllegalStateException(Some("TTY size returned no value"))
101 }
102 public override suspend fun isRawMode(): Boolean =
103 when (controlTty(owner, TtyControl.ReadMode)) {
104 case IoResult.TtyMode(val raw) -> raw
105 case _ -> throw IllegalStateException(Some("TTY mode returned no value"))
106 }
107 public override suspend fun enableRawMode() {
108 controlTty(owner, TtyControl.Enable)
109 }
110 public override suspend fun disableRawMode() {
111 controlTty(owner, TtyControl.Disable)
112 }
115internal suspend fun openTtyOnLoop(loop: LoopOwner): AsyncTty {
116 val job = loop.requireJob()
117 return suspendCoroutine<AsyncTty> {
118 continuation ->
119 val operation =
120 CallbackOperation(
121 loop,
122 loop.nextOperation(),
123 {
124 native,
125 id -> @Unsafe
126 {
127 return@CallbackOperation nativeStatus(id, nativeTtyOpen(native, id))
128 }
129 },
130 { _, _ -> },
131 {
132 event,
133 selected -> finishEndpointCreation<AsyncTty>(
134 loop,
135 EndpointKind.Tty,
136 event.result,
137 selected,
138 continuation,
139 { owner -> TtyEndpoint(owner) }
140 )
141 }
142 )
143 operation.subscribe(job)
144 loop.submit(operation)
145 }