Scoop API

scoop:scoop.terminal:0.1.0

src/style/color_commands.scoop

Selected production source · src/style/color_commands.scoop

1package scoop.terminal.style
3import scoop.terminal.Command
5/**
6 * Sets the foreground color subject to the library color-output policy.
7 */
8public 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)) }
16/**
17 * Sets the background color subject to the library color-output policy.
18 */
19public 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)) }
27/**
28 * Sets the underline color subject to the library color-output policy.
29 */
30public 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)) }
38private fun writeColor(writer: TextWriter, color: Colored) {
39 val parameters = color.toAnsiString()
40 if (isColorOutputEnabled()) writer.write("\u001b[" + parameters + "m")
43/**
44 * Sets the present foreground/background selections, preserving absent channels.
45 */
46public 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()) return
61 val parameters = if (foreground == "") background
62 else if (background == "") foreground else foreground + ";" + background
63 if (parameters != "") writer.write("\u001b[" + parameters + "m")
64 }
67/**
68 * Emits the full SGR reset, restoring rendition defaults.
69 */
70public 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") }