scoop:scoop.terminal.asyncio:0.1.0
src/output.scoop
Selected production source · src/output.scoop
1
package scoop.terminal.asyncio3
import scoop.asyncio.*4
import scoop.platform.memory.NativeByteBuffer5
import scoop.terminal.*7
/**8
* Writes each terminal command in order to the asynchronous byte sink without flushing.9
* Cancellation is checked before output begins.10
*/11
public suspend fun AsyncByteSink.queue(vararg commands: Command) {12
ensureActive()13
for (command in commands) writeText(command.toAnsiString())14
}16
/**17
* Writes each terminal command in order and then flushes the asynchronous byte sink.18
*/19
public suspend fun AsyncByteSink.execute(vararg commands: Command) {20
queue(*commands)21
flush()22
}24
internal suspend fun AsyncByteSink.writeText(text: String) {25
val bytes = text.toByteArray()26
NativeByteBuffer.allocate(bytes.size).use suspend { output ->27
output.writeBytes(bytes)28
writeFully(output)29
}30
}