scoop:scoop.terminal:0.1.0
src/command.scoop
Selected production source · src/command.scoop
1
package scoop.terminal3
import scoop.platform.io.ByteSink4
import scoop.platform.io.Utf8Writer6
/**7
* A terminal command that writes its ANSI representation to a text destination. Serialization8
* alone does not flush output or query terminal capabilities.9
*/10
public interface Command {11
/**12
* Writes this command's ANSI representation to the supplied writer without flushing it.13
* Parameter validation happens before or during serialization.14
*/15
public fun writeAnsi(writer: TextWriter)16
}18
/**19
* Serializes this command into an in-memory string without terminal I/O.20
*/21
public fun Command.toAnsiString(): String {22
val writer = StringWriter()23
writeAnsi(writer)24
return writer.toString()25
}27
/**28
* Writes the commands in order without flushing the destination.29
*/30
public fun TextWriter.queue(vararg commands: Command) {31
for (command in commands) command.writeAnsi(this)32
}34
/**35
* Writes the commands in order without flushing the destination.36
*/37
public fun ByteSink.queue(vararg commands: Command) {38
val writer = Utf8Writer(this)39
for (command in commands) command.writeAnsi(writer)40
}42
/**43
* Writes the commands in order, then flushes the byte sink.44
*/45
public fun ByteSink.execute(vararg commands: Command) {46
queue(*commands)47
flush()48
}50
/**51
* Writes the content's ToString result as supplied, without escaping terminal control sequences.52
*/53
public struct Print<T : ToString>(val content: T) : Command {54
/**55
* Writes this command's ANSI representation to the supplied writer without flushing it.56
* Parameter validation happens before or during serialization.57
*/58
public override fun writeAnsi(writer: TextWriter) { writer.write(content.toString()) }59
}61
internal fun requireTerminalParameter(value: Long, minimum: Long = 0L) {62
if (value < minimum || value > 65535L) {63
throw IllegalArgumentException(Some("terminal parameter outside supported range"))64
}65
}67
internal fun writeCsiParameter(writer: TextWriter, value: Long, suffix: String) {68
requireTerminalParameter(value)69
writer.write("\u001b[" + value.toString() + suffix)70
}