misc/vim: inform the user when no results are found
[nit.git] / lib / console.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Defines some ANSI Terminal Control Escape Sequences.
16 module console
17
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
22
23 # The Control Sequence Introducer (CSI).
24 protected fun csi: String do return "{esc}["
25 end
26
27 # Abstract class of the ANSI/VT100 escape sequences for directional moves.
28 abstract class TermDirectionalMove
29 super TermEscape
30
31 # The length of the move.
32 var magnitude: Int = 1 is protected writable
33
34 redef fun to_s do
35 if magnitude == 1 then return "{csi}{code}"
36 return "{csi}{magnitude}{code}"
37 end
38
39 # The code of the command.
40 protected fun code: String is abstract
41 end
42
43 # ANSI/VT100 code to move the cursor up by `magnitude` rows (CUU).
44 class TermMoveUp
45 super TermDirectionalMove
46
47 # Move by the specified number of cells.
48 init by(magnitude: Int) do self.magnitude = magnitude
49
50 redef fun code do return "A"
51 end
52
53 # ANSI/VT100 code to move the cursor down by `magnitude` rows (CUD).
54 class TermMoveDown
55 super TermDirectionalMove
56
57 # Move by the specified number of cells.
58 init by(magnitude: Int) do self.magnitude = magnitude
59
60 redef fun code do return "B"
61 end
62
63 # ANSI/VT100 code to move the cursor foward by `magnitude` columns (CUF).
64 class TermMoveFoward
65 super TermDirectionalMove
66
67 # Move by the specified number of cells.
68 init by(magnitude: Int) do self.magnitude = magnitude
69
70 redef fun code do return "C"
71 end
72
73 # ANSI/VT100 code to move the cursor backward by `magnitude` columns (CUB).
74 class TermMoveBackward
75 super TermDirectionalMove
76
77 # Move by the specified number of cells.
78 init by(magnitude: Int) do self.magnitude = magnitude
79
80 redef fun code do return "D"
81 end
82
83 # ANSI/VT100 code to move the cursor at the specified position (CUP).
84 class TermMove
85 super TermEscape
86
87 # Vertical position.
88 #
89 # 1 is the top.
90 var row: Int = 1
91
92 # Horizontal position.
93 #
94 # 1 is the left.
95 var column: Int = 1
96
97 # Move at the specified position.
98 #
99 # (1, 1) is the top-left corner of the display.
100 init at(row: Int, column: Int) do
101 self.row = row
102 self.column = column
103 end
104
105 redef fun to_s do
106 if row == 1 then
107 if column == 1 then return "{csi}H"
108 return "{csi};{column}H"
109 else
110 if column == 1 then return "{csi}{row}H"
111 return "{csi}{row};{column}H"
112 end
113 end
114 end
115
116 # ANSI/VT100 code to clear from the cursor to the end of the screen (ED 0).
117 class TermEraseDisplayDown
118 super TermEscape
119 redef fun to_s do return "{csi}J"
120 end
121
122 # ANSI/VT100 code to clear from the cursor to the beginning of the screen (ED 1).
123 class TermEraseDisplayUp
124 super TermEscape
125 redef fun to_s do return "{csi}1J"
126 end
127
128 # ANSI/VT100 code to clear the entire display and move the cursor to the top left of screen (ED 2).
129 #
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
133 super TermEscape
134 redef fun to_s do return "{csi}2J{csi}H"
135 end
136
137 # ANSI/VT100 code to erase anything after the cursor in the line (EL 0).
138 class TermEraseLineFoward
139 super TermEscape
140 redef fun to_s do return "{csi}K"
141 end
142
143 # ANSI/VT100 code to erase anything before the cursor in the line (EL 1).
144 class TermEraseLineBackward
145 super TermEscape
146 redef fun to_s do return "{csi}1K"
147 end
148
149 # ANSI/VT100 code to clear everything in the current line (EL 2).
150 class TermClearLine
151 super TermEscape
152 redef fun to_s do return "{csi}2K"
153 end
154
155 # ANSI/VT100 code to save the current cursor position (SCP).
156 class TermSaveCursor
157 super TermEscape
158 redef fun to_s do return "{csi}s"
159 end
160
161 # ANSI/VT100 code to restore the current cursor position (RCP).
162 class TermRestoreCursor
163 super TermEscape
164 redef fun to_s do return "{csi}u"
165 end
166
167 # ANSI/VT100 code to change character look (SGR).
168 #
169 # By default, resets everything to the terminal’s defaults.
170 #
171 # Note:
172 #
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).
178 #
179 # Usage example:
180 #
181 # print "{(new TermCharFormat).yellow_fg.bold}a{(new TermCharFormat).yellow_fg}b{new TermCharFormat}"
182 class TermCharFormat
183 super TermEscape
184
185 private var attributes: Array[String] = new Array[String]
186
187 # Copies the attributes from the specified format.
188 init from(format: TermCharFormat) do
189 attributes.add_all(format.attributes)
190 end
191
192 redef fun to_s: String do return "{csi}{attributes.join(";")}m"
193
194 # Apply the specified SGR and return `self`.
195 private fun apply(sgr: String): TermCharFormat do
196 attributes.add(sgr)
197 return self
198 end
199
200 # Apply normal (default) format and return `self`.
201 fun default: TermCharFormat do return apply("0")
202
203 # Apply bold weight and return `self`.
204 fun bold: TermCharFormat do return apply("1")
205
206 # Apply underlining and return `self`.
207 fun underline: TermCharFormat do return apply("4")
208
209 # Apply blinking or bold weight and return `self`.
210 fun blink: TermCharFormat do return apply("5")
211
212 # Apply reverse video and return `self`.
213 fun inverse: TermCharFormat do return apply("7")
214
215 # Apply normal weight and return `self`.
216 fun normal_weight: TermCharFormat do return apply("22")
217
218 # Add the attribute that disable inderlining and return `self`.
219 fun not_underlined: TermCharFormat do return apply("24")
220
221 # Add the attribute that disable blinking and return `self`.
222 fun steady: TermCharFormat do return apply("25")
223
224 # Add the attribute that disable reverse video and return `self`.
225 fun positive: TermCharFormat do return apply("27")
226
227 # Apply a black foreground and return `self`.
228 fun black_fg: TermCharFormat do return apply("30")
229
230 # Apply a red foreground and return `self`.
231 fun red_fg: TermCharFormat do return apply("31")
232
233 # Apply a green foreground and return `self`.
234 fun green_fg: TermCharFormat do return apply("32")
235
236 # Apply a yellow foreground and return `self`.
237 fun yellow_fg: TermCharFormat do return apply("33")
238
239 # Apply a blue foreground and return `self`.
240 fun blue_fg: TermCharFormat do return apply("34")
241
242 # Apply a mangenta foreground and return `self`.
243 fun magenta_fg: TermCharFormat do return apply("35")
244
245 # Apply a cyan foreground and return `self`.
246 fun cyan_fg: TermCharFormat do return apply("36")
247
248 # Apply a white foreground and return `self`.
249 fun white_fg: TermCharFormat do return apply("37")
250
251 # Apply the default foreground and return `self`.
252 fun default_fg: TermCharFormat do return apply("39")
253
254 # Apply a black backgroud and return `self`.
255 fun black_bg: TermCharFormat do return apply("40")
256
257 # Apply a red backgroud and return `self`.
258 fun red_bg: TermCharFormat do return apply("41")
259
260 # Apply a green backgroud and return `self`.
261 fun green_bg: TermCharFormat do return apply("42")
262
263 # Apply a yellow backgroud and return `self`.
264 fun yellow_bg: TermCharFormat do return apply("43")
265
266 # Apply a blue backgroud and return `self`.
267 fun blue_bg: TermCharFormat do return apply("44")
268
269 # Apply a mangenta backgroud and return `self`.
270 fun magenta_bg: TermCharFormat do return apply("45")
271
272 # Apply a cyan backgroud and return `self`.
273 fun cyan_bg: TermCharFormat do return apply("46")
274
275 # Apply a white backgroud and return `self`.
276 fun white_bg: TermCharFormat do return apply("47")
277
278 # Apply the default backgroud and return `self`.
279 fun default_bg: TermCharFormat do return apply("49")
280 end
281
282 # Redefine the `String` class to add functions to color the string.
283 redef class String
284 private fun apply_format(f: TermCharFormat): String do
285 return "{f}{self}{normal}"
286 end
287
288 private fun normal: TermCharFormat do return new TermCharFormat
289
290 # Make the text appear in dark gray (or black) in a ANSI/VT100 terminal.
291 #
292 # WARNING: SEE: `TermCharFormat`
293 fun gray: String do return apply_format(normal.black_fg)
294
295 # Make the text appear in red in a ANSI/VT100 terminal.
296 #
297 # WARNING: SEE: `TermCharFormat`
298 fun red: String do return apply_format(normal.red_fg)
299
300 # Make the text appear in green in a ANSI/VT100 terminal.
301 #
302 # WARNING: SEE: `TermCharFormat`
303 fun green: String do return apply_format(normal.green_fg)
304
305 # Make the text appear in yellow in a ANSI/VT100 terminal.
306 #
307 # WARNING: SEE: `TermCharFormat`
308 fun yellow: String do return apply_format(normal.yellow_fg)
309
310 # Make the text appear in blue in a ANSI/VT100 terminal.
311 #
312 # WARNING: SEE: `TermCharFormat`
313 fun blue: String do return apply_format(normal.blue_fg)
314
315 # Make the text appear in mangenta in a ANSI/VT100 terminal.
316 #
317 # WARNING: SEE: `TermCharFormat`
318 fun purple: String do return apply_format(normal.magenta_fg)
319
320 # Make the text appear in cyan in a ANSI/VT100 terminal.
321 #
322 # WARNING: SEE: `TermCharFormat`
323 fun cyan: String do return apply_format(normal.cyan_fg)
324
325 # Make the text appear in light gray (or white) in a ANSI/VT100 terminal.
326 #
327 # WARNING: SEE: `TermCharFormat`
328 fun light_gray: String do return apply_format(normal.white_fg)
329
330 # Make the text appear in bold in a ANSI/VT100 terminal.
331 #
332 # WARNING: SEE: `TermCharFormat`
333 fun bold: String do return apply_format(normal.bold)
334
335 # Make the text underlined in a ANSI/VT100 terminal.
336 #
337 # WARNING: SEE: `TermCharFormat`
338 fun underline: String do return apply_format(normal.underline)
339 end