View source with formatted 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)).   40
   41/** <module> Print decorated text to ANSI consoles
   42
   43This library allows for exploiting the color and attribute facilities of
   44most modern terminals using ANSI escape sequences. This library provides
   45the following:
   46
   47  - ansi_format/3 allows writing messages to the terminal with ansi
   48    attributes.
   49  - It defines the hook prolog:message_line_element/2, which provides
   50    ansi attributes for print_message/2.
   51
   52@see    http://en.wikipedia.org/wiki/ANSI_escape_code
   53*/
   54
   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.   62
   63%!  ansi_format(+Attributes, +Format, +Args) is det.
   64%
   65%   Format text with ANSI  attributes.   This  predicate  behaves as
   66%   format/2 using Format and Args, but if the =current_output= is a
   67%   terminal, it adds ANSI escape sequences according to Attributes.
   68%   For example, to print a text in bold cyan, do
   69%
   70%     ==
   71%     ?- ansi_format([bold,fg(cyan)], 'Hello ~w', [world]).
   72%     ==
   73%
   74%   Attributes is either a single attribute   or a list thereof. The
   75%   attribute names are derived from the ANSI specification. See the
   76%   source for sgr_code/2 for details. Some commonly used attributes
   77%   are:
   78%
   79%     - bold
   80%     - underline
   81%     - fg(Color), bg(Color), hfg(Color), hbg(Color)
   82%
   83%   Defined color constants are below.  =default=   can  be  used to
   84%   access the default color of the terminal.
   85%
   86%     - black, red, green, yellow, blue, magenta, cyan, white
   87%
   88%   ANSI sequences are sent if and only if
   89%
   90%     - The =current_output= has the property tty(true) (see
   91%       stream_property/2).
   92%     - The Prolog flag =color_term= is =true=.
   93
   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).
  117
  118%!  sgr_code(+Name, -Code)
  119%
  120%   True when code is the Select   Graphic  Rendition code for Name.
  121%   The defined names are given below. Note that most terminals only
  122%   implement this partially.
  123%
  124%     | reset                       | all attributes off    |
  125%     | bold                        |                       |
  126%     | faint                       |       |
  127%     | italic                      |       |
  128%     | underline                   |       |
  129%     | blink(slow)                 |       |
  130%     | blink(rapid)                |       |
  131%     | negative                    |       |
  132%     | conceal                     |       |
  133%     | crossed_out                 |       |
  134%     | font(primary)               |       |
  135%     | font(N)                     | Alternate font (1..8) |
  136%     | fraktur                     |       |
  137%     | underline(double)           |       |
  138%     | intensity(normal)           |       |
  139%     | fg(Name)                    | Color name    |
  140%     | bg(Name)                    | Color name    |
  141%     | framed                      |       |
  142%     | encircled                   |       |
  143%     | overlined                   |       |
  144%     | ideogram(underline)         |       |
  145%     | right_side_line             |       |
  146%     | ideogram(underline(double)) |       |
  147%     | right_side_line(double)     |       |
  148%     | ideogram(overlined)         |       |
  149%     | left_side_line              |       |
  150%     | ideogram(stress_marking)    |       |
  151%     | -Off                        | Switch attributes off |
  152%     | hfg(Name)                   | Color name    |
  153%     | hbg(Name)                   | Color name    |
  154%
  155%   @see http://en.wikipedia.org/wiki/ANSI_escape_code
  156
  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                 *******************************/
  224
  225%!  prolog:message_line_element(+Stream, +Term) is semidet.
  226%
  227%   Hook implementation that deals with  ansi(+Attr, +Fmt, +Args) in
  228%   message specifications.
  229
  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