View source with raw comments or as raw
    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-2013, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(ansi_term,
   36          [ ansi_format/3               % +Attr, +Format, +Args
   37          ]).   38:- use_module(library(apply)).   39:- use_module(library(error)).

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:

See also
- http://en.wikipedia.org/wiki/ANSI_escape_code */
   55:- create_prolog_flag(color_term, true, [type(boolean)]).   56
   57:- meta_predicate
   58    keep_line_pos(+, 0).   59
   60:- multifile
   61    user:message_property/2.
 ansi_format(+Attributes, +Format, +Args) is det
Format text with ANSI attributes. This predicate behaves as format/2 using Format and Args, but if the 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:

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

   94ansi_format(Attr, Format, Args) :-
   95    ansi_format(current_output, Attr, Format, Args).
   96
   97ansi_format(Stream, Attr, Format, Args) :-
   98    stream_property(Stream, tty(true)),
   99    current_prolog_flag(color_term, true),
  100    !,
  101    (   is_list(Attr)
  102    ->  maplist(sgr_code_ex, Attr, Codes),
  103        atomic_list_concat(Codes, ;, Code)
  104    ;   sgr_code_ex(Attr, Code)
  105    ),
  106    format(string(Fmt), '\e[~~wm~w\e[0m', [Format]),
  107    format(Stream, Fmt, [Code|Args]),
  108    flush_output.
  109ansi_format(Stream, _Attr, Format, Args) :-
  110    format(Stream, Format, Args).
  111
  112sgr_code_ex(Attr, Code) :-
  113    sgr_code(Attr, Code),
  114    !.
  115sgr_code_ex(Attr, _) :-
  116    domain_error(sgr_code, Attr).
 sgr_code(+Name, -Code)
True when code is the Select Graphic Rendition code for Name. The defined names are given below. Note that most terminals only implement this partially.
resetall 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)
-OffSwitch attributes off
hfg(Name)Color name
hbg(Name)Color name
See also
- http://en.wikipedia.org/wiki/ANSI_escape_code
  157sgr_code(reset, 0).
  158sgr_code(bold,  1).
  159sgr_code(faint, 2).
  160sgr_code(italic, 3).
  161sgr_code(underline, 4).
  162sgr_code(blink(slow), 5).
  163sgr_code(blink(rapid), 6).
  164sgr_code(negative, 7).
  165sgr_code(conceal, 8).
  166sgr_code(crossed_out, 9).
  167sgr_code(font(primary), 10) :- !.
  168sgr_code(font(N), C) :-
  169    C is 10+N.
  170sgr_code(fraktur, 20).
  171sgr_code(underline(double), 21).
  172sgr_code(intensity(normal), 22).
  173sgr_code(fg(Name), C) :-
  174    ansi_color(Name, N),
  175    C is N+30.
  176sgr_code(bg(Name), C) :-
  177    !,
  178    ansi_color(Name, N),
  179    C is N+40.
  180sgr_code(framed, 51).
  181sgr_code(encircled, 52).
  182sgr_code(overlined, 53).
  183sgr_code(ideogram(underline), 60).
  184sgr_code(right_side_line, 60).
  185sgr_code(ideogram(underline(double)), 61).
  186sgr_code(right_side_line(double), 61).
  187sgr_code(ideogram(overlined), 62).
  188sgr_code(left_side_line, 62).
  189sgr_code(ideogram(stress_marking), 64).
  190sgr_code(-X, Code) :-
  191    off_code(X, Code).
  192sgr_code(hfg(Name), C) :-
  193    ansi_color(Name, N),
  194    C is N+90.
  195sgr_code(hbg(Name), C) :-
  196    !,
  197    ansi_color(Name, N),
  198    C is N+100.
  199
  200off_code(italic_and_franktur, 23).
  201off_code(underline, 24).
  202off_code(blink, 25).
  203off_code(negative, 27).
  204off_code(conceal, 28).
  205off_code(crossed_out, 29).
  206off_code(framed, 54).
  207off_code(overlined, 55).
  208
  209
  210ansi_color(black,   0).
  211ansi_color(red,     1).
  212ansi_color(green,   2).
  213ansi_color(yellow,  3).
  214ansi_color(blue,    4).
  215ansi_color(magenta, 5).
  216ansi_color(cyan,    6).
  217ansi_color(white,   7).
  218ansi_color(default, 9).
  219
  220
  221                 /*******************************
  222                 *             HOOK             *
  223                 *******************************/
 prolog:message_line_element(+Stream, +Term) is semidet
Hook implementation that deals with ansi(+Attr, +Fmt, +Args) in message specifications.
  230prolog:message_line_element(S, ansi(Attr, Fmt, Args)) :-
  231    ansi_format(S, Attr, Fmt, Args).
  232prolog:message_line_element(S, begin(Level, Ctx)) :-
  233    level_attrs(Level, Attr),
  234    stream_property(S, tty(true)),
  235    current_prolog_flag(color_term, true),
  236    !,
  237    (   is_list(Attr)
  238    ->  maplist(sgr_code, Attr, Codes),
  239        atomic_list_concat(Codes, ;, Code)
  240    ;   sgr_code(Attr, Code)
  241    ),
  242    keep_line_pos(S, format(S, '\e[~wm', [Code])),
  243    Ctx = ansi('\e[0m').
  244prolog:message_line_element(S, end(Ctx)) :-
  245    nonvar(Ctx),
  246    Ctx = ansi(Reset),
  247    keep_line_pos(S, write(S, Reset)).
  248
  249level_attrs(Level,         Attrs) :-
  250    user:message_property(Level, color(Attrs)).
  251level_attrs(informational, fg(green)).
  252level_attrs(information,   fg(green)).
  253level_attrs(debug(_),      fg(blue)).
  254level_attrs(warning,       fg(red)).
  255level_attrs(error,         [fg(red),bold]).
  256
  257keep_line_pos(S, G) :-
  258    stream_property(S, position(Pos)),
  259    !,
  260    stream_position_data(line_position, Pos, LPos),
  261    G,
  262    set_stream(S, line_position(LPos)).
  263keep_line_pos(_, G) :-
  264    G