console: Offer a more reliable API.
[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 end
23
24 # ANSI/VT100 code to switch character attributes (SGR).
25 #
26 # By default, resets everything to the terminal’s defaults.
27 #
28 # Note:
29 #
30 # The escape sequence inserted at the end of the string by terminal-related
31 # methods of `String` resets all character attributes to the terminal’s
32 # defaults. So, when combining format `a` and `b`, something like
33 # `("foo".a + " bar").b` will not work as expected, but `"foo".a.b + " bar".b`
34 # will. You may also use `TermCharFormat` (this class).
35 #
36 # Usage example:
37 #
38 # print "{(new TermCharFormat).yellow_fg.bold}a{(new TermCharFormat).yellow_fg}b{new TermCharFormat}"
39 class TermCharFormat
40 super TermEscape
41
42 private var attributes: Array[String] = new Array[String]
43
44 # Copies the attributes from the specified format.
45 init from(format: TermCharFormat) do
46 attributes.add_all(format.attributes)
47 end
48
49 redef fun to_s: String do return "{esc}[{attributes.join(";")}m"
50
51 # Apply the specified SGR and return `self`.
52 private fun apply(sgr: String): TermCharFormat do
53 attributes.add(sgr)
54 return self
55 end
56
57 # Apply normal (default) format and return `self`.
58 fun default: TermCharFormat do return apply("0")
59
60 # Apply bold weight and return `self`.
61 fun bold: TermCharFormat do return apply("1")
62
63 # Apply underlining and return `self`.
64 fun underline: TermCharFormat do return apply("4")
65
66 # Apply blinking or bold weight and return `self`.
67 fun blink: TermCharFormat do return apply("5")
68
69 # Apply reverse video and return `self`.
70 fun inverse: TermCharFormat do return apply("7")
71
72 # Apply normal weight and return `self`.
73 fun normalWeight: TermCharFormat do return apply("22")
74
75 # Add the attribute that disable inderlining and return `self`.
76 fun not_underlined: TermCharFormat do return apply("24")
77
78 # Add the attribute that disable blinking and return `self`.
79 fun steady: TermCharFormat do return apply("25")
80
81 # Add the attribute that disable reverse video and return `self`.
82 fun positive: TermCharFormat do return apply("27")
83
84 # Apply a black foreground and return `self`.
85 fun black_fg: TermCharFormat do return apply("30")
86
87 # Apply a red foreground and return `self`.
88 fun red_fg: TermCharFormat do return apply("31")
89
90 # Apply a green foreground and return `self`.
91 fun green_fg: TermCharFormat do return apply("32")
92
93 # Apply a yellow foreground and return `self`.
94 fun yellow_fg: TermCharFormat do return apply("33")
95
96 # Apply a blue foreground and return `self`.
97 fun blue_fg: TermCharFormat do return apply("34")
98
99 # Apply a mangenta foreground and return `self`.
100 fun magenta_fg: TermCharFormat do return apply("35")
101
102 # Apply a cyan foreground and return `self`.
103 fun cyan_fg: TermCharFormat do return apply("36")
104
105 # Apply a white foreground and return `self`.
106 fun white_fg: TermCharFormat do return apply("37")
107
108 # Apply the default foreground and return `self`.
109 fun default_fg: TermCharFormat do return apply("39")
110
111 # Apply a black backgroud and return `self`.
112 fun black_bg: TermCharFormat do return apply("40")
113
114 # Apply a red backgroud and return `self`.
115 fun red_bg: TermCharFormat do return apply("41")
116
117 # Apply a green backgroud and return `self`.
118 fun green_bg: TermCharFormat do return apply("42")
119
120 # Apply a yellow backgroud and return `self`.
121 fun yellow_bg: TermCharFormat do return apply("43")
122
123 # Apply a blue backgroud and return `self`.
124 fun blue_bg: TermCharFormat do return apply("44")
125
126 # Apply a mangenta backgroud and return `self`.
127 fun magenta_bg: TermCharFormat do return apply("45")
128
129 # Apply a cyan backgroud and return `self`.
130 fun cyan_bg: TermCharFormat do return apply("46")
131
132 # Apply a white backgroud and return `self`.
133 fun white_bg: TermCharFormat do return apply("47")
134
135 # Apply the default backgroud and return `self`.
136 fun default_bg: TermCharFormat do return apply("49")
137 end
138
139 # Redefine the `String` class to add functions to color the string.
140 redef class String
141 private fun apply_format(f: TermCharFormat): String do
142 return "{f}{self}{normal}"
143 end
144
145 private fun normal: TermCharFormat do return new TermCharFormat
146
147 # Make the text appear in dark gray (or black) in a ANSI/VT100 terminal.
148 #
149 # WARNING: SEE: `TermCharFormat`
150 fun gray: String do return apply_format(normal.black_fg)
151
152 # Make the text appear in red in a ANSI/VT100 terminal.
153 #
154 # WARNING: SEE: `TermCharFormat`
155 fun red: String do return apply_format(normal.red_fg)
156
157 # Make the text appear in green in a ANSI/VT100 terminal.
158 #
159 # WARNING: SEE: `TermCharFormat`
160 fun green: String do return apply_format(normal.green_fg)
161
162 # Make the text appear in yellow in a ANSI/VT100 terminal.
163 #
164 # WARNING: SEE: `TermCharFormat`
165 fun yellow: String do return apply_format(normal.yellow_fg)
166
167 # Make the text appear in blue in a ANSI/VT100 terminal.
168 #
169 # WARNING: SEE: `TermCharFormat`
170 fun blue: String do return apply_format(normal.blue_fg)
171
172 # Make the text appear in mangenta in a ANSI/VT100 terminal.
173 #
174 # WARNING: SEE: `TermCharFormat`
175 fun purple: String do return apply_format(normal.magenta_fg)
176
177 # Make the text appear in cyan in a ANSI/VT100 terminal.
178 #
179 # WARNING: SEE: `TermCharFormat`
180 fun cyan: String do return apply_format(normal.cyan_fg)
181
182 # Make the text appear in light gray (or white) in a ANSI/VT100 terminal.
183 #
184 # WARNING: SEE: `TermCharFormat`
185 fun light_gray: String do return apply_format(normal.white_fg)
186
187 # Make the text appear in bold in a ANSI/VT100 terminal.
188 #
189 # WARNING: SEE: `TermCharFormat`
190 fun bold: String do return apply_format(normal.bold)
191
192 # Make the text underlined in a ANSI/VT100 terminal.
193 #
194 # WARNING: SEE: `TermCharFormat`
195 fun underline: String do return apply_format(normal.underline)
196 end