scoop:scoop.asyncio:0.1.0
src/pty.scoop
Selected production source · src/pty.scoop
1
package scoop.asyncio3
import scoop.platform.process.Command4
import scoop.platform.process.ExitStatus5
import scoop.platform.process.spawnPtyNative7
/** An owned asynchronous pseudoterminal with a single merged child output stream. */8
public 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(): ExitStatus15
/** Ends the session and reaps its direct child, discarding unread output if necessary. */16
public override suspend fun close()17
}19
internal suspend fun spawnPtyOnLoop(20
loop: LoopOwner,21
command: Command,22
columns: Long,23
rows: Long,24
searchPath: Boolean25
): AsyncPty {26
val job = loop.requireJob()27
val pointer = loop28
.executor29
.blocking {30
@Unsafe31
{32
return@blocking command.spawnPtyNative(columns, rows, searchPath)33
}34
}35
return suspendCoroutine<AsyncPty> { continuation ->36
loop37
.post {38
val pty = PtyEndpoint(loop, pointer)39
loop.retain(pty)40
val failure = try {41
job.ensureActive()42
pty.activate()43
None44
} catch (error: Throwable) {45
Some(error)46
}47
when (failure) {48
case Some(val error) ->49
pty50
.closeOnLoop { cleanup ->51
continuation52
.resumeWithException(53
when (cleanup) {54
case Some(val secondary) ->55
ResourceUseException(error, secondary)56
case None -> error57
}58
)59
}60
case None -> continuation.resume(pty)61
}62
}63
}64
}