1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2010-2018, VU University Amsterdam 7 CWI, Amsterdam 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in 19 the documentation and/or other materials provided with the 20 distribution. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34*/ 35 36:- module(ansi_term, 37 [ ansi_format/3 % +Attr, +Format, +Args 38 ]). 39:- use_module(library(apply)). 40:- use_module(library(error)).
56:- create_prolog_flag(color_term, true, [type(boolean)]). 57 58:- meta_predicate 59 keep_line_pos( , ). 60 61:- multifile 62 user:message_property/2.
current_output
is a
terminal, it adds ANSI escape sequences according to Attributes.
For example, to print a text in bold cyan, do
?- ansi_format([bold,fg(cyan)], 'Hello ~w', [world]).
Attributes is either a single attribute or a list thereof. The attribute names are derived from the ANSI specification. See the source for sgr_code/2 for details. Some commonly used attributes are:
fg(Color)
, bg(Color)
, hfg(Color)
, hbg(Color)
Defined color constants are below. default
can be used to
access the default color of the terminal.
ANSI sequences are sent if and only if
current_output
has the property tty(true)
(see
stream_property/2).color_term
is true
.95ansi_format(Attr, Format, Args) :- 96 ansi_format(current_output, Attr, Format, Args). 97 98ansi_format(Stream, Attr, Format, Args) :- 99 stream_property(Stream, tty(true)), 100 current_prolog_flag(color_term, true), 101 !, 102 ( is_list(Attr) 103 -> maplist(sgr_code_ex, Attr, Codes), 104 atomic_list_concat(Codes, ;, Code) 105 ; sgr_code_ex(Attr, Code) 106 ), 107 format(string(Fmt), '\e[~~wm~w\e[0m', [Format]), 108 format(Stream, Fmt, [Code|Args]), 109 flush_output. 110ansi_format(Stream, _Attr, Format, Args) :- 111 format(Stream, Format, Args). 112 113sgr_code_ex(Attr, Code) :- 114 sgr_code(Attr, Code), 115 !. 116sgr_code_ex(Attr, _) :- 117 domain_error(sgr_code, Attr).
reset | all attributes off |
bold | |
faint | |
italic | |
underline | |
blink(slow) | |
blink(rapid) | |
negative | |
conceal | |
crossed_out | |
font(primary) | |
font(N) | Alternate font (1..8) |
fraktur | |
underline(double) | |
intensity(normal) | |
fg(Name) | Color name |
bg(Name) | Color name |
framed | |
encircled | |
overlined | |
ideogram(underline) | |
right_side_line | |
ideogram(underline(double)) | |
right_side_line(double) | |
ideogram(overlined) | |
left_side_line | |
ideogram(stress_marking) | |
-Off | Switch attributes off |
hfg(Name) | Color name |
hbg(Name) | Color name |
158sgr_code(reset, 0). 159sgr_code(bold, 1). 160sgr_code(faint, 2). 161sgr_code(italic, 3). 162sgr_code(underline, 4). 163sgr_code(blink(slow), 5). 164sgr_code(blink(rapid), 6). 165sgr_code(negative, 7). 166sgr_code(conceal, 8). 167sgr_code(crossed_out, 9). 168sgr_code(font(primary), 10) :- !. 169sgr_code(font(N), C) :- 170 C is 10+N. 171sgr_code(fraktur, 20). 172sgr_code(underline(double), 21). 173sgr_code(intensity(normal), 22). 174sgr_code(fg(Name), C) :- 175 ansi_color(Name, N), 176 C is N+30. 177sgr_code(bg(Name), C) :- 178 !, 179 ansi_color(Name, N), 180 C is N+40. 181sgr_code(framed, 51). 182sgr_code(encircled, 52). 183sgr_code(overlined, 53). 184sgr_code(ideogram(underline), 60). 185sgr_code(right_side_line, 60). 186sgr_code(ideogram(underline(double)), 61). 187sgr_code(right_side_line(double), 61). 188sgr_code(ideogram(overlined), 62). 189sgr_code(left_side_line, 62). 190sgr_code(ideogram(stress_marking), 64). 191sgr_code(-X, Code) :- 192 off_code(X, Code). 193sgr_code(hfg(Name), C) :- 194 ansi_color(Name, N), 195 C is N+90. 196sgr_code(hbg(Name), C) :- 197 !, 198 ansi_color(Name, N), 199 C is N+100. 200 201off_code(italic_and_franktur, 23). 202off_code(underline, 24). 203off_code(blink, 25). 204off_code(negative, 27). 205off_code(conceal, 28). 206off_code(crossed_out, 29). 207off_code(framed, 54). 208off_code(overlined, 55). 209 210 211ansi_color(black, 0). 212ansi_color(red, 1). 213ansi_color(green, 2). 214ansi_color(yellow, 3). 215ansi_color(blue, 4). 216ansi_color(magenta, 5). 217ansi_color(cyan, 6). 218ansi_color(white, 7). 219ansi_color(default, 9). 220 221 222 /******************************* 223 * HOOK * 224 *******************************/
ansi(+Attr, +Fmt, +Args)
in
message specifications.231prolog:message_line_element(S, ansi(Attr, Fmt, Args)) :- 232 ansi_format(S, Attr, Fmt, Args). 233prolog:message_line_element(S, ansi(Attr, Fmt, Args, Ctx)) :- 234 ansi_format(S, Attr, Fmt, Args), 235 ( nonvar(Ctx), 236 Ctx = ansi(_, RI-RA) 237 -> keep_line_pos(S, format(S, RI, RA)) 238 ; true 239 ). 240prolog:message_line_element(S, begin(Level, Ctx)) :- 241 level_attrs(Level, Attr), 242 stream_property(S, tty(true)), 243 current_prolog_flag(color_term, true), 244 !, 245 ( is_list(Attr) 246 -> maplist(sgr_code, Attr, Codes), 247 atomic_list_concat(Codes, ;, Code) 248 ; sgr_code(Attr, Code) 249 ), 250 keep_line_pos(S, format(S, '\e[~wm', [Code])), 251 Ctx = ansi('\e[0m', '\e[0m\e[~wm'-[Code]). 252prolog:message_line_element(S, end(Ctx)) :- 253 nonvar(Ctx), 254 Ctx = ansi(Reset, _), 255 keep_line_pos(S, write(S, Reset)). 256 257level_attrs(Level, Attrs) :- 258 user:message_property(Level, color(Attrs)). 259level_attrs(informational, fg(green)). 260level_attrs(information, fg(green)). 261level_attrs(debug(_), fg(blue)). 262level_attrs(warning, fg(red)). 263level_attrs(error, [fg(red),bold]). 264 265keep_line_pos(S, G) :- 266 stream_property(S, position(Pos)), 267 !, 268 stream_position_data(line_position, Pos, LPos), 269 , 270 set_stream(S, line_position(LPos)). 271keep_line_pos(_, G) :- 272
Print decorated text to ANSI consoles
This library allows for exploiting the color and attribute facilities of most modern terminals using ANSI escape sequences. This library provides the following: