Scoop API

scoop:scoop.terminal:0.1.0

src/style/attributes.scoop

Selected production source · src/style/attributes.scoop

1package scoop.terminal.style
3import scoop.terminal.Command
5/**
6 * An SGR rendition attribute or its corresponding reset instruction. Terminal support varies by
7 * attribute.
8 */
9public enum Attribute {
10 /**
11 * Reset all rendition attributes.
12 */
13 Reset,
14 /**
15 * Enable bold or increased intensity.
16 */
17 Bold,
18 /**
19 * Enable decreased intensity.
20 */
21 Dim,
22 /**
23 * Enable italic presentation.
24 */
25 Italic,
26 /**
27 * Enable a single underline.
28 */
29 Underlined,
30 /**
31 * Enable a double underline.
32 */
33 DoubleUnderlined,
34 /**
35 * Enable a curled underline.
36 */
37 Undercurled,
38 /**
39 * Enable a dotted underline.
40 */
41 Underdotted,
42 /**
43 * Enable a dashed underline.
44 */
45 Underdashed,
46 /**
47 * Enable slow blinking.
48 */
49 SlowBlink,
50 /**
51 * Enable rapid blinking.
52 */
53 RapidBlink,
54 /**
55 * Swap foreground and background rendition.
56 */
57 Reverse,
58 /**
59 * Conceal displayed text.
60 */
61 Hidden,
62 /**
63 * Strike through text.
64 */
65 CrossedOut,
66 /**
67 * Select Fraktur-style presentation.
68 */
69 Fraktur,
70 /**
71 * Disable bold presentation.
72 */
73 NoBold,
74 /**
75 * Restore ordinary intensity, clearing bold/dim.
76 */
77 NormalIntensity,
78 /**
79 * Disable italic presentation.
80 */
81 NoItalic,
82 /**
83 * Disable underlining.
84 */
85 NoUnderline,
86 /**
87 * Disable blinking.
88 */
89 NoBlink,
90 /**
91 * Disable reversed presentation.
92 */
93 NoReverse,
94 /**
95 * Reveal concealed text.
96 */
97 NoHidden,
98 /**
99 * Disable strike-through.
100 */
101 NotCrossedOut,
102 /**
103 * Frame the text.
104 */
105 Framed,
106 /**
107 * Encircle the text.
108 */
109 Encircled,
110 /**
111 * Enable an overline.
112 */
113 OverLined,
114 /**
115 * Disable framing and encircling.
116 */
117 NotFramedOrEncircled,
118 /**
119 * Disable overlining.
120 */
121 NotOverLined
123 internal val index: Int get() = when (this) {
124 case Reset -> 0; case Bold -> 1; case Dim -> 2; case Italic -> 3
125 case Underlined -> 4; case DoubleUnderlined -> 5; case Undercurled -> 6
126 case Underdotted -> 7; case Underdashed -> 8; case SlowBlink -> 9
127 case RapidBlink -> 10; case Reverse -> 11; case Hidden -> 12; case CrossedOut -> 13
128 case Fraktur -> 14; case NoBold -> 15; case NormalIntensity -> 16; case NoItalic -> 17
129 case NoUnderline -> 18; case NoBlink -> 19; case NoReverse -> 20; case NoHidden -> 21
130 case NotCrossedOut -> 22; case Framed -> 23; case Encircled -> 24; case OverLined -> 25
131 case NotFramedOrEncircled -> 26; case NotOverLined -> 27
132 }
134 /**
135 * Returns the SGR parameter text without CSI prefix or final m.
136 */
137 public fun sgr(): String = when (this) {
138 case Reset -> "0"; case Bold -> "1"; case Dim -> "2"; case Italic -> "3"
139 case Underlined -> "4"; case DoubleUnderlined -> "4:2"; case Undercurled -> "4:3"
140 case Underdotted -> "4:4"; case Underdashed -> "4:5"; case SlowBlink -> "5"
141 case RapidBlink -> "6"; case Reverse -> "7"; case Hidden -> "8"; case CrossedOut -> "9"
142 case Fraktur -> "20"; case NoBold -> "21"; case NormalIntensity -> "22"; case NoItalic -> "23"
143 case NoUnderline -> "24"; case NoBlink -> "25"; case NoReverse -> "27"; case NoHidden -> "28"
144 case NotCrossedOut -> "29"; case Framed -> "51"; case Encircled -> "52"; case OverLined -> "53"
145 case NotFramedOrEncircled -> "54"; case NotOverLined -> "55"
146 }
149internal val allAttributes: Array<Attribute> = [
150 Attribute.Reset, Attribute.Bold, Attribute.Dim, Attribute.Italic, Attribute.Underlined,
151 Attribute.DoubleUnderlined, Attribute.Undercurled, Attribute.Underdotted, Attribute.Underdashed,
152 Attribute.SlowBlink, Attribute.RapidBlink, Attribute.Reverse, Attribute.Hidden, Attribute.CrossedOut,
153 Attribute.Fraktur, Attribute.NoBold, Attribute.NormalIntensity, Attribute.NoItalic, Attribute.NoUnderline,
154 Attribute.NoBlink, Attribute.NoReverse, Attribute.NoHidden, Attribute.NotCrossedOut, Attribute.Framed,
155 Attribute.Encircled, Attribute.OverLined, Attribute.NotFramedOrEncircled, Attribute.NotOverLined
158/**
159 * An immutable set of SGR attributes.
160 */
161public struct Attributes(val bits: UInt = 0u) {
162 /**
163 * Whether no bits are set.
164 */
165 public val isEmpty: Boolean get() = bits == 0u
166 /**
167 * Tests whether the selected flag is present.
168 */
169 public fun has(attribute: Attribute): Boolean = (bits and (1u shl attribute.index.toLong())) != 0u
170 /**
171 * Returns a copy with the selected flag included.
172 */
173 public fun with(attribute: Attribute): Attributes = Attributes(bits or (1u shl attribute.index.toLong()))
174 /**
175 * Returns a copy with the selected flag removed.
176 */
177 public fun without(attribute: Attribute): Attributes = Attributes(bits and (1u shl attribute.index.toLong()).inv())
178 /**
179 * Returns a copy with the selected attribute's bit toggled.
180 */
181 public fun toggled(attribute: Attribute): Attributes = Attributes(bits xor (1u shl attribute.index.toLong()))
184/**
185 * Emits one SGR attribute command.
186 */
187public struct SetAttribute(val attribute: Attribute) : Command {
188 /**
189 * Writes this command's ANSI representation to the supplied writer without flushing it.
190 * Parameter validation happens before or during serialization.
191 */
192 public override fun writeAnsi(writer: TextWriter) { writer.write("\u001b[" + attribute.sgr() + "m") }
195/**
196 * Emits the selected SGR attributes in the library's fixed order.
197 */
198public struct SetAttributes(val attributes: Attributes) : Command {
199 /**
200 * Writes this command's ANSI representation to the supplied writer without flushing it.
201 * Parameter validation happens before or during serialization.
202 */
203 public override fun writeAnsi(writer: TextWriter) {
204 if ((attributes.bits and 0xf0000000u) != 0u) {
205 throw IllegalArgumentException(Some("unknown terminal attribute bits"))
206 }
207 for (attribute in allAttributes) {
208 if (attributes.has(attribute)) SetAttribute(attribute).writeAnsi(writer)
209 }
210 }