scoop:scoop.terminal:0.1.0
src/style/color_commands.scoop
Selected production source · src/style/color_commands.scoop
1
package scoop.terminal.style3
import scoop.terminal.Command5
/**6
* Sets the foreground color subject to the library color-output policy.7
*/8
public struct SetForegroundColor(val color: Color) : Command {9
/**10
* Writes this command's ANSI representation to the supplied writer without flushing it.11
* Parameter validation happens before or during serialization.12
*/13
public override fun writeAnsi(writer: TextWriter) { writeColor(writer, Colored.ForegroundColor(color)) }14
}16
/**17
* Sets the background color subject to the library color-output policy.18
*/19
public struct SetBackgroundColor(val color: Color) : Command {20
/**21
* Writes this command's ANSI representation to the supplied writer without flushing it.22
* Parameter validation happens before or during serialization.23
*/24
public override fun writeAnsi(writer: TextWriter) { writeColor(writer, Colored.BackgroundColor(color)) }25
}27
/**28
* Sets the underline color subject to the library color-output policy.29
*/30
public struct SetUnderlineColor(val color: Color) : Command {31
/**32
* Writes this command's ANSI representation to the supplied writer without flushing it.33
* Parameter validation happens before or during serialization.34
*/35
public override fun writeAnsi(writer: TextWriter) { writeColor(writer, Colored.UnderlineColor(color)) }36
}38
private fun writeColor(writer: TextWriter, color: Colored) {39
val parameters = color.toAnsiString()40
if (isColorOutputEnabled()) writer.write("\u001b[" + parameters + "m")41
}43
/**44
* Sets the present foreground/background selections, preserving absent channels.45
*/46
public struct SetColors(val colors: Colors) : Command {47
/**48
* Writes this command's ANSI representation to the supplied writer without flushing it.49
* Parameter validation happens before or during serialization.50
*/51
public override fun writeAnsi(writer: TextWriter) {52
val foreground = when (colors.foreground) {53
case Some(val color) -> Colored.ForegroundColor(color).toAnsiString()54
case None -> ""55
}56
val background = when (colors.background) {57
case Some(val color) -> Colored.BackgroundColor(color).toAnsiString()58
case None -> ""59
}60
if (!isColorOutputEnabled()) return61
val parameters = if (foreground == "") background62
else if (background == "") foreground else foreground + ";" + background63
if (parameters != "") writer.write("\u001b[" + parameters + "m")64
}65
}67
/**68
* Emits the full SGR reset, restoring rendition defaults.69
*/70
public object ResetColor : Command {71
/**72
* Writes this command's ANSI representation to the supplied writer without flushing it.73
* Parameter validation happens before or during serialization.74
*/75
public override fun writeAnsi(writer: TextWriter) { writer.write("\u001b[0m") }76
}