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
24 # ANSI/VT100 code to switch character attributes (SGR).
26 # By default, resets everything to the terminal’s defaults.
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).
38 # print "{(new TermCharFormat).yellow_fg.bold}a{(new TermCharFormat).yellow_fg}b{new TermCharFormat}"
42 private var attributes
: Array[String] = new Array[String]
44 # Copies the attributes from the specified format.
45 init from
(format
: TermCharFormat) do
46 attributes
.add_all
(format
.attributes
)
49 redef fun to_s
: String do return "{esc}[{attributes.join(";")}m"
51 # Apply the specified SGR and return `self`.
52 private fun apply
(sgr
: String): TermCharFormat do
57 # Apply normal (default) format and return `self`.
58 fun default
: TermCharFormat do return apply
("0")
60 # Apply bold weight and return `self`.
61 fun bold
: TermCharFormat do return apply
("1")
63 # Apply underlining and return `self`.
64 fun underline
: TermCharFormat do return apply
("4")
66 # Apply blinking or bold weight and return `self`.
67 fun blink
: TermCharFormat do return apply
("5")
69 # Apply reverse video and return `self`.
70 fun inverse
: TermCharFormat do return apply
("7")
72 # Apply normal weight and return `self`.
73 fun normalWeight
: TermCharFormat do return apply
("22")
75 # Add the attribute that disable inderlining and return `self`.
76 fun not_underlined
: TermCharFormat do return apply
("24")
78 # Add the attribute that disable blinking and return `self`.
79 fun steady
: TermCharFormat do return apply
("25")
81 # Add the attribute that disable reverse video and return `self`.
82 fun positive
: TermCharFormat do return apply
("27")
84 # Apply a black foreground and return `self`.
85 fun black_fg
: TermCharFormat do return apply
("30")
87 # Apply a red foreground and return `self`.
88 fun red_fg
: TermCharFormat do return apply
("31")
90 # Apply a green foreground and return `self`.
91 fun green_fg
: TermCharFormat do return apply
("32")
93 # Apply a yellow foreground and return `self`.
94 fun yellow_fg
: TermCharFormat do return apply
("33")
96 # Apply a blue foreground and return `self`.
97 fun blue_fg
: TermCharFormat do return apply
("34")
99 # Apply a mangenta foreground and return `self`.
100 fun magenta_fg
: TermCharFormat do return apply
("35")
102 # Apply a cyan foreground and return `self`.
103 fun cyan_fg
: TermCharFormat do return apply
("36")
105 # Apply a white foreground and return `self`.
106 fun white_fg
: TermCharFormat do return apply
("37")
108 # Apply the default foreground and return `self`.
109 fun default_fg
: TermCharFormat do return apply
("39")
111 # Apply a black backgroud and return `self`.
112 fun black_bg
: TermCharFormat do return apply
("40")
114 # Apply a red backgroud and return `self`.
115 fun red_bg
: TermCharFormat do return apply
("41")
117 # Apply a green backgroud and return `self`.
118 fun green_bg
: TermCharFormat do return apply
("42")
120 # Apply a yellow backgroud and return `self`.
121 fun yellow_bg
: TermCharFormat do return apply
("43")
123 # Apply a blue backgroud and return `self`.
124 fun blue_bg
: TermCharFormat do return apply
("44")
126 # Apply a mangenta backgroud and return `self`.
127 fun magenta_bg
: TermCharFormat do return apply
("45")
129 # Apply a cyan backgroud and return `self`.
130 fun cyan_bg
: TermCharFormat do return apply
("46")
132 # Apply a white backgroud and return `self`.
133 fun white_bg
: TermCharFormat do return apply
("47")
135 # Apply the default backgroud and return `self`.
136 fun default_bg
: TermCharFormat do return apply
("49")
139 # Redefine the `String` class to add functions to color the string.
141 private fun apply_format
(f
: TermCharFormat): String do
142 return "{f}{self}{normal}"
145 private fun normal
: TermCharFormat do return new TermCharFormat
147 # Make the text appear in dark gray (or black) in a ANSI/VT100 terminal.
149 # WARNING: SEE: `TermCharFormat`
150 fun gray
: String do return apply_format
(normal
.black_fg
)
152 # Make the text appear in red in a ANSI/VT100 terminal.
154 # WARNING: SEE: `TermCharFormat`
155 fun red
: String do return apply_format
(normal
.red_fg
)
157 # Make the text appear in green in a ANSI/VT100 terminal.
159 # WARNING: SEE: `TermCharFormat`
160 fun green
: String do return apply_format
(normal
.green_fg
)
162 # Make the text appear in yellow in a ANSI/VT100 terminal.
164 # WARNING: SEE: `TermCharFormat`
165 fun yellow
: String do return apply_format
(normal
.yellow_fg
)
167 # Make the text appear in blue in a ANSI/VT100 terminal.
169 # WARNING: SEE: `TermCharFormat`
170 fun blue
: String do return apply_format
(normal
.blue_fg
)
172 # Make the text appear in mangenta in a ANSI/VT100 terminal.
174 # WARNING: SEE: `TermCharFormat`
175 fun purple
: String do return apply_format
(normal
.magenta_fg
)
177 # Make the text appear in cyan in a ANSI/VT100 terminal.
179 # WARNING: SEE: `TermCharFormat`
180 fun cyan
: String do return apply_format
(normal
.cyan_fg
)
182 # Make the text appear in light gray (or white) in a ANSI/VT100 terminal.
184 # WARNING: SEE: `TermCharFormat`
185 fun light_gray
: String do return apply_format
(normal
.white_fg
)
187 # Make the text appear in bold in a ANSI/VT100 terminal.
189 # WARNING: SEE: `TermCharFormat`
190 fun bold
: String do return apply_format
(normal
.bold
)
192 # Make the text underlined in a ANSI/VT100 terminal.
194 # WARNING: SEE: `TermCharFormat`
195 fun underline
: String do return apply_format
(normal
.underline
)