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.
17 # The color methods (e.g. `Text::green`) format the text to appear colored
18 # in a ANSI/VT100 terminal. By default, this coloring is skipped if stdout
19 # is not a TTY, but it can be forced by setting `force_console_colors = true`.
22 # A ANSI/VT100 escape sequence.
23 abstract class TermEscape
24 # The US-ASCII ESC character.
25 protected fun esc
: Char do return 27.code_point
27 # The Control Sequence Introducer (CSI).
28 protected fun csi
: String do return "{esc}["
31 # Abstract class of the ANSI/VT100 escape sequences for directional moves.
32 abstract class TermDirectionalMove
35 # The length of the move.
36 var magnitude
: Int = 1 is protected writable
39 if magnitude
== 1 then return "{csi}{code}"
40 return "{csi}{magnitude}{code}"
43 # The code of the command.
44 protected fun code
: String is abstract
47 # ANSI/VT100 code to move the cursor up by `magnitude` rows (CUU).
49 super TermDirectionalMove
51 # Move by the specified number of cells.
52 init by
(magnitude
: Int) do self.magnitude
= magnitude
54 redef fun code
do return "A"
57 # ANSI/VT100 code to move the cursor down by `magnitude` rows (CUD).
59 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 forward by `magnitude` columns (CUF).
69 super TermDirectionalMove
71 # Move by the specified number of cells.
72 init by
(magnitude
: Int) do self.magnitude
= magnitude
74 redef fun code
do return "C"
77 # ANSI/VT100 code to move the cursor backward by `magnitude` columns (CUB).
78 class TermMoveBackward
79 super TermDirectionalMove
81 # Move by the specified number of cells.
82 init by
(magnitude
: Int) do self.magnitude
= magnitude
84 redef fun code
do return "D"
87 # ANSI/VT100 code to move the cursor at the specified position (CUP).
96 # Horizontal position.
101 # Move at the specified position.
103 # (1, 1) is the top-left corner of the display.
104 init at
(row
: Int, column
: Int) do
111 if column
== 1 then return "{csi}H"
112 return "{csi};{column}H"
114 if column
== 1 then return "{csi}{row}H"
115 return "{csi}{row};{column}H"
120 # ANSI/VT100 code to clear from the cursor to the end of the screen (ED 0).
121 class TermEraseDisplayDown
123 redef fun to_s
do return "{csi}J"
126 # ANSI/VT100 code to clear from the cursor to the beginning of the screen (ED 1).
127 class TermEraseDisplayUp
129 redef fun to_s
do return "{csi}1J"
132 # ANSI/VT100 code to clear the entire display and move the cursor to the top left of screen (ED 2).
134 # Note: Some terminals always move the cursor when the screen is cleared. So we
135 # force this behaviour to ensure interoperability of the code.
136 class TermClearDisplay
138 redef fun to_s
do return "{csi}2J{csi}H"
141 # ANSI/VT100 code to erase anything after the cursor in the line (EL 0).
142 class TermEraseLineFoward
144 redef fun to_s
do return "{csi}K"
147 # ANSI/VT100 code to erase anything before the cursor in the line (EL 1).
148 class TermEraseLineBackward
150 redef fun to_s
do return "{csi}1K"
153 # ANSI/VT100 code to clear everything in the current line (EL 2).
156 redef fun to_s
do return "{csi}2K"
159 # ANSI/VT100 code to save the current cursor position (SCP).
162 redef fun to_s
do return "{csi}s"
165 # ANSI/VT100 code to restore the current cursor position (RCP).
166 class TermRestoreCursor
168 redef fun to_s
do return "{csi}u"
171 # ANSI/VT100 code to change character look (SGR).
173 # By default, resets everything to the terminal’s defaults.
177 # The escape sequence inserted at the end of the string by terminal-related
178 # methods of `String` resets all character attributes to the terminal’s
179 # defaults. So, when combining format `a` and `b`, something like
180 # `("foo".a + " bar").b` will not work as expected, but `"foo".a.b + " bar".b`
181 # will. You may also use `TermCharFormat` (this class).
185 # print "{(new TermCharFormat).yellow_fg.bold}a{(new TermCharFormat).yellow_fg}b{new TermCharFormat}"
189 private var attributes
: Array[String] = new Array[String]
191 # Copies the attributes from the specified format.
192 init from
(format
: TermCharFormat) do
193 attributes
.add_all
(format
.attributes
)
196 redef fun to_s
do return "{csi}{attributes.join(";")}m"
198 # Apply the specified SGR and return `self`.
199 private fun apply
(sgr
: String): TermCharFormat do
204 # Apply normal (default) format and return `self`.
205 fun default
: TermCharFormat do return apply
("0")
207 # Apply bold weight and return `self`.
208 fun bold
: TermCharFormat do return apply
("1")
210 # Apply underlining and return `self`.
211 fun underline
: TermCharFormat do return apply
("4")
213 # Apply blinking or bold weight and return `self`.
214 fun blink
: TermCharFormat do return apply
("5")
216 # Apply reverse video and return `self`.
217 fun inverse
: TermCharFormat do return apply
("7")
219 # Apply normal weight and return `self`.
220 fun normal_weight
: TermCharFormat do return apply
("22")
222 # Add the attribute that disable underlining and return `self`.
223 fun not_underlined
: TermCharFormat do return apply
("24")
225 # Add the attribute that disable blinking and return `self`.
226 fun steady
: TermCharFormat do return apply
("25")
228 # Add the attribute that disable reverse video and return `self`.
229 fun positive
: TermCharFormat do return apply
("27")
231 # Apply a black foreground and return `self`.
232 fun black_fg
: TermCharFormat do return apply
("30")
234 # Apply a red foreground and return `self`.
235 fun red_fg
: TermCharFormat do return apply
("31")
237 # Apply a green foreground and return `self`.
238 fun green_fg
: TermCharFormat do return apply
("32")
240 # Apply a yellow foreground and return `self`.
241 fun yellow_fg
: TermCharFormat do return apply
("33")
243 # Apply a blue foreground and return `self`.
244 fun blue_fg
: TermCharFormat do return apply
("34")
246 # Apply a magenta foreground and return `self`.
247 fun magenta_fg
: TermCharFormat do return apply
("35")
249 # Apply a cyan foreground and return `self`.
250 fun cyan_fg
: TermCharFormat do return apply
("36")
252 # Apply a white foreground and return `self`.
253 fun white_fg
: TermCharFormat do return apply
("37")
255 # Apply the default foreground and return `self`.
256 fun default_fg
: TermCharFormat do return apply
("39")
258 # Apply a black background and return `self`.
259 fun black_bg
: TermCharFormat do return apply
("40")
261 # Apply a red background and return `self`.
262 fun red_bg
: TermCharFormat do return apply
("41")
264 # Apply a green background and return `self`.
265 fun green_bg
: TermCharFormat do return apply
("42")
267 # Apply a yellow background and return `self`.
268 fun yellow_bg
: TermCharFormat do return apply
("43")
270 # Apply a blue background and return `self`.
271 fun blue_bg
: TermCharFormat do return apply
("44")
273 # Apply a magenta background and return `self`.
274 fun magenta_bg
: TermCharFormat do return apply
("45")
276 # Apply a cyan background and return `self`.
277 fun cyan_bg
: TermCharFormat do return apply
("46")
279 # Apply a white background and return `self`.
280 fun white_bg
: TermCharFormat do return apply
("47")
282 # Apply the default background and return `self`.
283 fun default_bg
: TermCharFormat do return apply
("49")
286 # Services to color terminal output
288 private fun apply_format
(f
: TermCharFormat): String do
289 if stdout_isatty
or force_console_colors
then
290 return "{f}{self}{normal}"
294 private fun normal
: TermCharFormat do return new TermCharFormat
296 # Make the text appear in dark gray (or black) in a ANSI/VT100 terminal.
298 # SEE: `TermCharFormat`
299 fun gray
: String do return apply_format
(normal
.black_fg
)
301 # Make the text appear in red in a ANSI/VT100 terminal.
303 # SEE: `TermCharFormat`
304 fun red
: String do return apply_format
(normal
.red_fg
)
306 # Make the text appear in green in a ANSI/VT100 terminal.
308 # SEE: `TermCharFormat`
309 fun green
: String do return apply_format
(normal
.green_fg
)
311 # Make the text appear in yellow in a ANSI/VT100 terminal.
313 # SEE: `TermCharFormat`
314 fun yellow
: String do return apply_format
(normal
.yellow_fg
)
316 # Make the text appear in blue in a ANSI/VT100 terminal.
318 # SEE: `TermCharFormat`
319 fun blue
: String do return apply_format
(normal
.blue_fg
)
321 # Make the text appear in magenta in a ANSI/VT100 terminal.
323 # SEE: `TermCharFormat`
324 fun purple
: String do return apply_format
(normal
.magenta_fg
)
326 # Make the text appear in cyan in a ANSI/VT100 terminal.
328 # SEE: `TermCharFormat`
329 fun cyan
: String do return apply_format
(normal
.cyan_fg
)
331 # Make the text appear in light gray (or white) in a ANSI/VT100 terminal.
333 # SEE: `TermCharFormat`
334 fun light_gray
: String do return apply_format
(normal
.white_fg
)
336 # Make the text appear in bold in a ANSI/VT100 terminal.
338 # SEE: `TermCharFormat`
339 fun bold
: String do return apply_format
(normal
.bold
)
341 # Make the text underlined in a ANSI/VT100 terminal.
343 # SEE: `TermCharFormat`
344 fun underline
: String do return apply_format
(normal
.underline
)
347 # A dynamic progress bar displayable in console.
353 # var pb = new TermProgress(max, current)
356 # for i in [current + 1 .. max] do
364 # Progress bar can accept metadata to display a small amount of data.
366 # Example with metadata:
368 # var pb = new TermProgress(10, 0)
369 # for i in [0..10] do
370 # pb.update(i, "Step {i}")
375 # Max value of the progress bar (business value).
378 # Current value of the progress bar (business value).
379 var current_value
: Int
381 # Number of columns used to display the progress bar.
382 var max_columns
= 70 is writable
384 # Get the current percent value.
385 fun current_percentage
: Int do
386 return current_value
* 100 / max_value
389 # Display the progress bar.
391 # `metadata` can be used to pass a small amount of data to display after
393 fun display
(metadata
: nullable String) do
394 var percent
= current_percentage
395 var p
= current_value
* max_columns
/ max_value
396 printn
"\r{percent}% ["
397 for i
in [1..max_columns
] do
407 if metadata
!= null then printn
" ({metadata})"
410 # Update and display the progress bar.
413 fun update
(new_current
: Int, metadata
: nullable String) do
414 current_value
= new_current
420 private var stdout_isatty
: Bool = 1.isatty
is lazy
422 # Force coloring terminal output, even if stdout is not a TTY?
424 # Defaults to `false`.
425 var force_console_colors
= false is writable