Scoop API

scoop:scoop.terminal:0.1.0

src/command.scoop

Selected production source · src/command.scoop

1package scoop.terminal
3import scoop.platform.io.ByteSink
4import scoop.platform.io.Utf8Writer
6/**
7 * A terminal command that writes its ANSI representation to a text destination. Serialization
8 * alone does not flush output or query terminal capabilities.
9 */
10public 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)
18/**
19 * Serializes this command into an in-memory string without terminal I/O.
20 */
21public fun Command.toAnsiString(): String {
22 val writer = StringWriter()
23 writeAnsi(writer)
24 return writer.toString()
27/**
28 * Writes the commands in order without flushing the destination.
29 */
30public fun TextWriter.queue(vararg commands: Command) {
31 for (command in commands) command.writeAnsi(this)
34/**
35 * Writes the commands in order without flushing the destination.
36 */
37public fun ByteSink.queue(vararg commands: Command) {
38 val writer = Utf8Writer(this)
39 for (command in commands) command.writeAnsi(writer)
42/**
43 * Writes the commands in order, then flushes the byte sink.
44 */
45public fun ByteSink.execute(vararg commands: Command) {
46 queue(*commands)
47 flush()
50/**
51 * Writes the content's ToString result as supplied, without escaping terminal control sequences.
52 */
53public 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()) }
61internal fun requireTerminalParameter(value: Long, minimum: Long = 0L) {
62 if (value < minimum || value > 65535L) {
63 throw IllegalArgumentException(Some("terminal parameter outside supported range"))
64 }
67internal fun writeCsiParameter(writer: TextWriter, value: Long, suffix: String) {
68 requireTerminalParameter(value)
69 writer.write("\u001b[" + value.toString() + suffix)