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
47 # Move by the specified number of cells.
48 init by
(magnitude
: Int) do self.magnitude
= magnitude
50 redef fun code
do return "A"
53 # ANSI/VT100 code to move the cursor down by `magnitude` rows (CUD).
55 super TermDirectionalMove
57 # Move by the specified number of cells.
58 init by
(magnitude
: Int) do self.magnitude
= magnitude
60 redef fun code
do return "B"
63 # ANSI/VT100 code to move the cursor foward by `magnitude` columns (CUF).
65 super TermDirectionalMove
67 # Move by the specified number of cells.
68 init by
(magnitude
: Int) do self.magnitude
= magnitude
70 redef fun code
do return "C"
73 # ANSI/VT100 code to move the cursor backward by `magnitude` columns (CUB).
74 class TermMoveBackward
75 super TermDirectionalMove
77 # Move by the specified number of cells.
78 init by
(magnitude
: Int) do self.magnitude
= magnitude
80 redef fun code
do return "D"
83 # ANSI/VT100 code to move the cursor at the specified position (CUP).
92 # Horizontal position.
97 # Move at the specified position.
99 # (1, 1) is the top-left corner of the display.
100 init at
(row
: Int, column
: Int) do
107 if column
== 1 then return "{csi}H"
108 return "{csi};{column}H"
110 if column
== 1 then return "{csi}{row}H"
111 return "{csi}{row};{column}H"
116 # ANSI/VT100 code to clear from the cursor to the end of the screen (ED 0).
117 class TermEraseDisplayDown
119 redef fun to_s
do return "{csi}J"
122 # ANSI/VT100 code to clear from the cursor to the beginning of the screen (ED 1).
123 class TermEraseDisplayUp
125 redef fun to_s
do return "{csi}1J"
128 # ANSI/VT100 code to clear the entire display and move the cursor to the top left of screen (ED 2).
130 # Note: Some terminals always move the cursor when the screen is cleared. So we
131 # force this behaviour to ensure interoperability of the code.
132 class TermClearDisplay
134 redef fun to_s
do return "{csi}2J{csi}H"
137 # ANSI/VT100 code to erase anything after the cursor in the line (EL 0).
138 class TermEraseLineFoward
140 redef fun to_s
do return "{csi}K"
143 # ANSI/VT100 code to erase anything before the cursor in the line (EL 1).
144 class TermEraseLineBackward
146 redef fun to_s
do return "{csi}1K"
149 # ANSI/VT100 code to clear everything in the current line (EL 2).
152 redef fun to_s
do return "{csi}2K"
155 # ANSI/VT100 code to save the current cursor position (SCP).
158 redef fun to_s
do return "{csi}s"
161 # ANSI/VT100 code to restore the current cursor position (RCP).
162 class TermRestoreCursor
164 redef fun to_s
do return "{csi}u"
167 # ANSI/VT100 code to change character look (SGR).
169 # By default, resets everything to the terminal’s defaults.
173 # The escape sequence inserted at the end of the string by terminal-related
174 # methods of `String` resets all character attributes to the terminal’s
175 # defaults. So, when combining format `a` and `b`, something like
176 # `("foo".a + " bar").b` will not work as expected, but `"foo".a.b + " bar".b`
177 # will. You may also use `TermCharFormat` (this class).
181 # print "{(new TermCharFormat).yellow_fg.bold}a{(new TermCharFormat).yellow_fg}b{new TermCharFormat}"
185 private var attributes
: Array[String] = new Array[String]
187 # Copies the attributes from the specified format.
188 init from
(format
: TermCharFormat) do
189 attributes
.add_all
(format
.attributes
)
192 redef fun to_s
: String do return "{csi}{attributes.join(";")}m"
194 # Apply the specified SGR and return `self`.
195 private fun apply
(sgr
: String): TermCharFormat do
200 # Apply normal (default) format and return `self`.
201 fun default
: TermCharFormat do return apply
("0")
203 # Apply bold weight and return `self`.
204 fun bold
: TermCharFormat do return apply
("1")
206 # Apply underlining and return `self`.
207 fun underline
: TermCharFormat do return apply
("4")
209 # Apply blinking or bold weight and return `self`.
210 fun blink
: TermCharFormat do return apply
("5")
212 # Apply reverse video and return `self`.
213 fun inverse
: TermCharFormat do return apply
("7")
215 # Apply normal weight and return `self`.
216 fun normal_weight
: TermCharFormat do return apply
("22")
218 # Add the attribute that disable inderlining and return `self`.
219 fun not_underlined
: TermCharFormat do return apply
("24")
221 # Add the attribute that disable blinking and return `self`.
222 fun steady
: TermCharFormat do return apply
("25")
224 # Add the attribute that disable reverse video and return `self`.
225 fun positive
: TermCharFormat do return apply
("27")
227 # Apply a black foreground and return `self`.
228 fun black_fg
: TermCharFormat do return apply
("30")
230 # Apply a red foreground and return `self`.
231 fun red_fg
: TermCharFormat do return apply
("31")
233 # Apply a green foreground and return `self`.
234 fun green_fg
: TermCharFormat do return apply
("32")
236 # Apply a yellow foreground and return `self`.
237 fun yellow_fg
: TermCharFormat do return apply
("33")
239 # Apply a blue foreground and return `self`.
240 fun blue_fg
: TermCharFormat do return apply
("34")
242 # Apply a mangenta foreground and return `self`.
243 fun magenta_fg
: TermCharFormat do return apply
("35")
245 # Apply a cyan foreground and return `self`.
246 fun cyan_fg
: TermCharFormat do return apply
("36")
248 # Apply a white foreground and return `self`.
249 fun white_fg
: TermCharFormat do return apply
("37")
251 # Apply the default foreground and return `self`.
252 fun default_fg
: TermCharFormat do return apply
("39")
254 # Apply a black backgroud and return `self`.
255 fun black_bg
: TermCharFormat do return apply
("40")
257 # Apply a red backgroud and return `self`.
258 fun red_bg
: TermCharFormat do return apply
("41")
260 # Apply a green backgroud and return `self`.
261 fun green_bg
: TermCharFormat do return apply
("42")
263 # Apply a yellow backgroud and return `self`.
264 fun yellow_bg
: TermCharFormat do return apply
("43")
266 # Apply a blue backgroud and return `self`.
267 fun blue_bg
: TermCharFormat do return apply
("44")
269 # Apply a mangenta backgroud and return `self`.
270 fun magenta_bg
: TermCharFormat do return apply
("45")
272 # Apply a cyan backgroud and return `self`.
273 fun cyan_bg
: TermCharFormat do return apply
("46")
275 # Apply a white backgroud and return `self`.
276 fun white_bg
: TermCharFormat do return apply
("47")
278 # Apply the default backgroud and return `self`.
279 fun default_bg
: TermCharFormat do return apply
("49")
282 # Redefine the `String` class to add functions to color the string.
284 private fun apply_format
(f
: TermCharFormat): String do
285 return "{f}{self}{normal}"
288 private fun normal
: TermCharFormat do return new TermCharFormat
290 # Make the text appear in dark gray (or black) in a ANSI/VT100 terminal.
292 # WARNING: SEE: `TermCharFormat`
293 fun gray
: String do return apply_format
(normal
.black_fg
)
295 # Make the text appear in red in a ANSI/VT100 terminal.
297 # WARNING: SEE: `TermCharFormat`
298 fun red
: String do return apply_format
(normal
.red_fg
)
300 # Make the text appear in green in a ANSI/VT100 terminal.
302 # WARNING: SEE: `TermCharFormat`
303 fun green
: String do return apply_format
(normal
.green_fg
)
305 # Make the text appear in yellow in a ANSI/VT100 terminal.
307 # WARNING: SEE: `TermCharFormat`
308 fun yellow
: String do return apply_format
(normal
.yellow_fg
)
310 # Make the text appear in blue in a ANSI/VT100 terminal.
312 # WARNING: SEE: `TermCharFormat`
313 fun blue
: String do return apply_format
(normal
.blue_fg
)
315 # Make the text appear in mangenta in a ANSI/VT100 terminal.
317 # WARNING: SEE: `TermCharFormat`
318 fun purple
: String do return apply_format
(normal
.magenta_fg
)
320 # Make the text appear in cyan in a ANSI/VT100 terminal.
322 # WARNING: SEE: `TermCharFormat`
323 fun cyan
: String do return apply_format
(normal
.cyan_fg
)
325 # Make the text appear in light gray (or white) in a ANSI/VT100 terminal.
327 # WARNING: SEE: `TermCharFormat`
328 fun light_gray
: String do return apply_format
(normal
.white_fg
)
330 # Make the text appear in bold in a ANSI/VT100 terminal.
332 # WARNING: SEE: `TermCharFormat`
333 fun bold
: String do return apply_format
(normal
.bold
)
335 # Make the text underlined in a ANSI/VT100 terminal.
337 # WARNING: SEE: `TermCharFormat`
338 fun underline
: String do return apply_format
(normal
.underline
)