1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Defines some ANSI Terminal Control Escape Sequences.
18 # A ANSI/VT100 escape sequence.
19 abstract class TermEscape
20 # The US-ASCII ESC character.
21 protected fun esc
: Char do return 27.ascii
23 # The Control Sequence Introducer (CSI).
24 protected fun csi
: String do return "{esc}["
27 # Abstract class of the ANSI/VT100 escape sequences for directional moves.
28 abstract class TermDirectionalMove
31 # The length of the move.
32 var magnitude
: Int = 1 is protected writable
35 if magnitude
== 1 then return "{csi}{code}"
36 return "{csi}{magnitude}{code}"
39 # The code of the command.
40 protected fun code
: String is abstract
43 # ANSI/VT100 code to move the cursor up by `magnitude` rows (CUU).
45 super TermDirectionalMove
49 # Move by the specified number of cells.
50 init by
(magnitude
: Int) do self.magnitude
= magnitude
52 redef fun code
do return "A"
55 # ANSI/VT100 code to move the cursor down by `magnitude` rows (CUD).
57 super TermDirectionalMove
61 # Move by the specified number of cells.
62 init by
(magnitude
: Int) do self.magnitude
= magnitude
64 redef fun code
do return "B"
67 # ANSI/VT100 code to move the cursor foward by `magnitude` columns (CUF).
69 super TermDirectionalMove
73 # Move by the specified number of cells.
74 init by
(magnitude
: Int) do self.magnitude
= magnitude
76 redef fun code
do return "C"
79 # ANSI/VT100 code to move the cursor backward by `magnitude` columns (CUB).
80 class TermMoveBackward
81 super TermDirectionalMove
85 # Move by the specified number of cells.
86 init by
(magnitude
: Int) do self.magnitude
= magnitude
88 redef fun code
do return "D"
91 # ANSI/VT100 code to move the cursor at the specified position (CUP).
100 # Horizontal position.
107 # Move at the specified position.
109 # (1, 1) is the top-left corner of the display.
110 init at
(row
: Int, column
: Int) do
117 if column
== 1 then return "{csi}H"
118 return "{csi};{column}H"
120 if column
== 1 then return "{csi}{row}H"
121 return "{csi}{row};{column}H"
126 # ANSI/VT100 code to clear from the cursor to the end of the screen (ED 0).
127 class TermEraseDisplayDown
129 redef fun to_s
do return "{csi}J"
132 # ANSI/VT100 code to clear from the cursor to the beginning of the screen (ED 1).
133 class TermEraseDisplayUp
135 redef fun to_s
do return "{csi}1J"
138 # ANSI/VT100 code to clear the entire display and move the cursor to the top left of screen (ED 2).
140 # Note: Some terminals always move the cursor when the screen is cleared. So we
141 # force this behaviour to ensure interoperability of the code.
142 class TermClearDisplay
144 redef fun to_s
do return "{csi}2J{csi}H"
147 # ANSI/VT100 code to erase anything after the cursor in the line (EL 0).
148 class TermEraseLineFoward
150 redef fun to_s
do return "{csi}K"
153 # ANSI/VT100 code to erase anything before the cursor in the line (EL 1).
154 class TermEraseLineBackward
156 redef fun to_s
do return "{csi}1K"
159 # ANSI/VT100 code to clear everything in the current line (EL 2).
162 redef fun to_s
do return "{csi}2K"
165 # ANSI/VT100 code to save the current cursor position (SCP).
168 redef fun to_s
do return "{csi}s"
171 # ANSI/VT100 code to restore the current cursor position (RCP).
172 class TermRestoreCursor
174 redef fun to_s
do return "{csi}u"
177 # ANSI/VT100 code to change character look (SGR).
179 # By default, resets everything to the terminal’s defaults.
183 # The escape sequence inserted at the end of the string by terminal-related
184 # methods of `String` resets all character attributes to the terminal’s
185 # defaults. So, when combining format `a` and `b`, something like
186 # `("foo".a + " bar").b` will not work as expected, but `"foo".a.b + " bar".b`
187 # will. You may also use `TermCharFormat` (this class).
191 # print "{(new TermCharFormat).yellow_fg.bold}a{(new TermCharFormat).yellow_fg}b{new TermCharFormat}"
195 private var attributes
: Array[String] = new Array[String]
197 # Copies the attributes from the specified format.
198 init from
(format
: TermCharFormat) do
199 attributes
.add_all
(format
.attributes
)
202 redef fun to_s
: String do return "{csi}{attributes.join(";")}m"
204 # Apply the specified SGR and return `self`.
205 private fun apply
(sgr
: String): TermCharFormat do
210 # Apply normal (default) format and return `self`.
211 fun default
: TermCharFormat do return apply
("0")
213 # Apply bold weight and return `self`.
214 fun bold
: TermCharFormat do return apply
("1")
216 # Apply underlining and return `self`.
217 fun underline
: TermCharFormat do return apply
("4")
219 # Apply blinking or bold weight and return `self`.
220 fun blink
: TermCharFormat do return apply
("5")
222 # Apply reverse video and return `self`.
223 fun inverse
: TermCharFormat do return apply
("7")
225 # Apply normal weight and return `self`.
226 fun normal_weight
: TermCharFormat do return apply
("22")
228 # Add the attribute that disable inderlining and return `self`.
229 fun not_underlined
: TermCharFormat do return apply
("24")
231 # Add the attribute that disable blinking and return `self`.
232 fun steady
: TermCharFormat do return apply
("25")
234 # Add the attribute that disable reverse video and return `self`.
235 fun positive
: TermCharFormat do return apply
("27")
237 # Apply a black foreground and return `self`.
238 fun black_fg
: TermCharFormat do return apply
("30")
240 # Apply a red foreground and return `self`.
241 fun red_fg
: TermCharFormat do return apply
("31")
243 # Apply a green foreground and return `self`.
244 fun green_fg
: TermCharFormat do return apply
("32")
246 # Apply a yellow foreground and return `self`.
247 fun yellow_fg
: TermCharFormat do return apply
("33")
249 # Apply a blue foreground and return `self`.
250 fun blue_fg
: TermCharFormat do return apply
("34")
252 # Apply a mangenta foreground and return `self`.
253 fun magenta_fg
: TermCharFormat do return apply
("35")
255 # Apply a cyan foreground and return `self`.
256 fun cyan_fg
: TermCharFormat do return apply
("36")
258 # Apply a white foreground and return `self`.
259 fun white_fg
: TermCharFormat do return apply
("37")
261 # Apply the default foreground and return `self`.
262 fun default_fg
: TermCharFormat do return apply
("39")
264 # Apply a black backgroud and return `self`.
265 fun black_bg
: TermCharFormat do return apply
("40")
267 # Apply a red backgroud and return `self`.
268 fun red_bg
: TermCharFormat do return apply
("41")
270 # Apply a green backgroud and return `self`.
271 fun green_bg
: TermCharFormat do return apply
("42")
273 # Apply a yellow backgroud and return `self`.
274 fun yellow_bg
: TermCharFormat do return apply
("43")
276 # Apply a blue backgroud and return `self`.
277 fun blue_bg
: TermCharFormat do return apply
("44")
279 # Apply a mangenta backgroud and return `self`.
280 fun magenta_bg
: TermCharFormat do return apply
("45")
282 # Apply a cyan backgroud and return `self`.
283 fun cyan_bg
: TermCharFormat do return apply
("46")
285 # Apply a white backgroud and return `self`.
286 fun white_bg
: TermCharFormat do return apply
("47")
288 # Apply the default backgroud and return `self`.
289 fun default_bg
: TermCharFormat do return apply
("49")
292 # Redefine the `String` class to add functions to color the string.
294 private fun apply_format
(f
: TermCharFormat): String do
295 return "{f}{self}{normal}"
298 private fun normal
: TermCharFormat do return new TermCharFormat
300 # Make the text appear in dark gray (or black) in a ANSI/VT100 terminal.
302 # WARNING: SEE: `TermCharFormat`
303 fun gray
: String do return apply_format
(normal
.black_fg
)
305 # Make the text appear in red in a ANSI/VT100 terminal.
307 # WARNING: SEE: `TermCharFormat`
308 fun red
: String do return apply_format
(normal
.red_fg
)
310 # Make the text appear in green in a ANSI/VT100 terminal.
312 # WARNING: SEE: `TermCharFormat`
313 fun green
: String do return apply_format
(normal
.green_fg
)
315 # Make the text appear in yellow in a ANSI/VT100 terminal.
317 # WARNING: SEE: `TermCharFormat`
318 fun yellow
: String do return apply_format
(normal
.yellow_fg
)
320 # Make the text appear in blue in a ANSI/VT100 terminal.
322 # WARNING: SEE: `TermCharFormat`
323 fun blue
: String do return apply_format
(normal
.blue_fg
)
325 # Make the text appear in mangenta in a ANSI/VT100 terminal.
327 # WARNING: SEE: `TermCharFormat`
328 fun purple
: String do return apply_format
(normal
.magenta_fg
)
330 # Make the text appear in cyan in a ANSI/VT100 terminal.
332 # WARNING: SEE: `TermCharFormat`
333 fun cyan
: String do return apply_format
(normal
.cyan_fg
)
335 # Make the text appear in light gray (or white) in a ANSI/VT100 terminal.
337 # WARNING: SEE: `TermCharFormat`
338 fun light_gray
: String do return apply_format
(normal
.white_fg
)
340 # Make the text appear in bold in a ANSI/VT100 terminal.
342 # WARNING: SEE: `TermCharFormat`
343 fun bold
: String do return apply_format
(normal
.bold
)
345 # Make the text underlined in a ANSI/VT100 terminal.
347 # WARNING: SEE: `TermCharFormat`
348 fun underline
: String do return apply_format
(normal
.underline
)