Scoop API

scoop:scoop.asyncio:0.1.0

src/pty.scoop

Selected production source · src/pty.scoop

1package scoop.asyncio
3import scoop.platform.process.Command
4import scoop.platform.process.ExitStatus
5import scoop.platform.process.spawnPtyNative
7/** An owned asynchronous pseudoterminal with a single merged child output stream. */
8public interface AsyncPty : AsyncByteStream, AsyncCloseable {
9 /** Changes the terminal cell dimensions. May run while reading or writing. */
10 public suspend fun resize(columns: Long, rows: Long)
12 /** Waits for the direct child. Keep reading concurrently to retain its terminal output. */
13 public suspend fun wait(): ExitStatus
15 /** Ends the session and reaps its direct child, discarding unread output if necessary. */
16 public override suspend fun close()
19internal suspend fun spawnPtyOnLoop(
20 loop: LoopOwner,
21 command: Command,
22 columns: Long,
23 rows: Long,
24 searchPath: Boolean
25): AsyncPty {
26 val job = loop.requireJob()
27 val pointer = loop
28 .executor
29 .blocking {
30 @Unsafe
31 {
32 return@blocking command.spawnPtyNative(columns, rows, searchPath)
33 }
34 }
35 return suspendCoroutine<AsyncPty> { continuation ->
36 loop
37 .post {
38 val pty = PtyEndpoint(loop, pointer)
39 loop.retain(pty)
40 val failure = try {
41 job.ensureActive()
42 pty.activate()
43 None
44 } catch (error: Throwable) {
45 Some(error)
46 }
47 when (failure) {
48 case Some(val error) ->
49 pty
50 .closeOnLoop { cleanup ->
51 continuation
52 .resumeWithException(
53 when (cleanup) {
54 case Some(val secondary) ->
55 ResourceUseException(error, secondary)
56 case None -> error
57 }
58 )
59 }
60 case None -> continuation.resume(pty)
61 }
62 }
63 }