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-2019, 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            ansi_get_color/2            % +Which, -rgb(R,G,B)
   39          ]).   40:- autoload(library(apply),[maplist/3]).   41:- autoload(library(error),[domain_error/2,must_be/2]).   42:- autoload(library(lists),[flatten/2,append/2,append/3]).   43:- if(exists_source(library(time))).   44:- autoload(library(time),[call_with_time_limit/2]).   45:- endif.

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 */
   62:- multifile
   63    prolog:console_color/2,                     % +Term, -AnsiAttrs
   64    supports_get_color/0.   65
   66
   67color_term_flag_default(true) :-
   68    stream_property(user_input, tty(true)),
   69    stream_property(user_error, tty(true)),
   70    stream_property(user_output, tty(true)),
   71    \+ getenv('TERM', dumb),
   72    !.
   73color_term_flag_default(false).
   74
   75init_color_term_flag :-
   76    color_term_flag_default(Default),
   77    create_prolog_flag(color_term, Default,
   78                       [ type(boolean),
   79                         keep(true)
   80                       ]).
   81
   82:- init_color_term_flag.   83
   84
   85:- meta_predicate
   86    keep_line_pos(+, 0).   87
   88:- multifile
   89    user:message_property/2.
 ansi_format(+ClassOrAttributes, +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, a list thereof or a term that is mapped to concrete attributes based on the current theme (see prolog:console_color/2). The attribute names are derived from the ANSI specification. See the source for sgr_code/2 for details. Some commonly used attributes are:

bold
underline
fg(Color),bg(Color),hfg(Color),hbg(Color)
For fg(Color) and bg(Color), the colour name can be '#RGB' or '#RRGGBB'
fg8(Spec),bg8(Spec)
8-bit color specification. Spec is a colour name, h(Color) or an integer 0..255.
fg(R, G, B),bg(R, G, B)
24-bit (direct color) specification. The components are integers in the range 0..255.

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

  131ansi_format(Attr, Format, Args) :-
  132    ansi_format(current_output, Attr, Format, Args).
  133
  134ansi_format(Stream, Class, Format, Args) :-
  135    stream_property(Stream, tty(true)),
  136    current_prolog_flag(color_term, true),
  137    !,
  138    class_attrs(Class, Attr),
  139    (   is_list(Attr)
  140    ->  maplist(sgr_code_ex, Attr, Codes0),
  141        flatten(Codes0, Codes),
  142        atomic_list_concat(Codes, ;, Code)
  143    ;   sgr_code_ex(Attr, Code0),
  144        (   is_list(Code0)
  145        ->  atomic_list_concat(Code0, ;, Code)
  146        ;   Code = Code0
  147        )
  148    ),
  149    format(string(Fmt), '\e[~~wm~w\e[0m', [Format]),
  150    format(Stream, Fmt, [Code|Args]),
  151    flush_output.
  152ansi_format(Stream, _Attr, Format, Args) :-
  153    format(Stream, Format, Args).
  154
  155sgr_code_ex(Attr, Code) :-
  156    sgr_code(Attr, Code),
  157    !.
  158sgr_code_ex(Attr, _) :-
  159    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
  200sgr_code(reset, 0).
  201sgr_code(bold,  1).
  202sgr_code(faint, 2).
  203sgr_code(italic, 3).
  204sgr_code(underline, 4).
  205sgr_code(blink(slow), 5).
  206sgr_code(blink(rapid), 6).
  207sgr_code(negative, 7).
  208sgr_code(conceal, 8).
  209sgr_code(crossed_out, 9).
  210sgr_code(font(primary), 10) :- !.
  211sgr_code(font(N), C) :-
  212    C is 10+N.
  213sgr_code(fraktur, 20).
  214sgr_code(underline(double), 21).
  215sgr_code(intensity(normal), 22).
  216sgr_code(fg(Name), C) :-
  217    (   ansi_color(Name, N)
  218    ->  C is N+30
  219    ;   rgb(Name, R, G, B)
  220    ->  sgr_code(fg(R,G,B), C)
  221    ).
  222sgr_code(bg(Name), C) :-
  223    !,
  224    (   ansi_color(Name, N)
  225    ->  C is N+40
  226    ;   rgb(Name, R, G, B)
  227    ->  sgr_code(bg(R,G,B), C)
  228    ).
  229sgr_code(framed, 51).
  230sgr_code(encircled, 52).
  231sgr_code(overlined, 53).
  232sgr_code(ideogram(underline), 60).
  233sgr_code(right_side_line, 60).
  234sgr_code(ideogram(underline(double)), 61).
  235sgr_code(right_side_line(double), 61).
  236sgr_code(ideogram(overlined), 62).
  237sgr_code(left_side_line, 62).
  238sgr_code(ideogram(stress_marking), 64).
  239sgr_code(-X, Code) :-
  240    off_code(X, Code).
  241sgr_code(hfg(Name), C) :-
  242    ansi_color(Name, N),
  243    C is N+90.
  244sgr_code(hbg(Name), C) :-
  245    !,
  246    ansi_color(Name, N),
  247    C is N+100.
  248sgr_code(fg8(Name), [38,5,N]) :-
  249    ansi_color8(Name, N).
  250sgr_code(bg8(Name), [48,5,N]) :-
  251    ansi_color8(Name, N).
  252sgr_code(fg(R,G,B), [38,2,R,G,B]) :-
  253    between(0, 255, R),
  254    between(0, 255, G),
  255    between(0, 255, B).
  256sgr_code(bg(R,G,B), [48,2,R,G,B]) :-
  257    between(0, 255, R),
  258    between(0, 255, G),
  259    between(0, 255, B).
  260
  261off_code(italic_and_franktur, 23).
  262off_code(underline, 24).
  263off_code(blink, 25).
  264off_code(negative, 27).
  265off_code(conceal, 28).
  266off_code(crossed_out, 29).
  267off_code(framed, 54).
  268off_code(overlined, 55).
  269
  270ansi_color8(h(Name), N) :-
  271    !,
  272    ansi_color(Name, N0),
  273    N is N0+8.
  274ansi_color8(Name, N) :-
  275    atom(Name),
  276    !,
  277    ansi_color(Name, N).
  278ansi_color8(N, N) :-
  279    between(0, 255, N).
  280
  281ansi_color(black,   0).
  282ansi_color(red,     1).
  283ansi_color(green,   2).
  284ansi_color(yellow,  3).
  285ansi_color(blue,    4).
  286ansi_color(magenta, 5).
  287ansi_color(cyan,    6).
  288ansi_color(white,   7).
  289ansi_color(default, 9).
  290
  291rgb(Name, R, G, B) :-
  292    atom_codes(Name, [0'#,R1,R2,G1,G2,B1,B2]),
  293    hex_color(R1,R2,R),
  294    hex_color(G1,G2,G),
  295    hex_color(B1,B2,B).
  296rgb(Name, R, G, B) :-
  297    atom_codes(Name, [0'#,R1,G1,B1]),
  298    hex_color(R1,R),
  299    hex_color(G1,G),
  300    hex_color(B1,B).
  301
  302hex_color(D1,D2,V) :-
  303    code_type(D1, xdigit(V1)),
  304    code_type(D2, xdigit(V2)),
  305    V is 16*V1+V2.
  306
  307hex_color(D1,V) :-
  308    code_type(D1, xdigit(V1)),
  309    V is 16*V1+V1.
 prolog:console_color(+Term, -AnsiAttributes) is semidet
Hook that allows for mapping abstract terms to concrete ANSI attributes. This hook is used by theme files to adjust the rendering based on user preferences and context. Defaults are defined in the file boot/messages.pl.
See also
- library(theme/dark) for an example implementation and the Term values used by the system messages.
  322                 /*******************************
  323                 *             HOOK             *
  324                 *******************************/
 prolog:message_line_element(+Stream, +Term) is semidet
Hook implementation that deals with ansi(+Attr, +Fmt, +Args) in message specifications.
  331prolog:message_line_element(S, ansi(Class, Fmt, Args)) :-
  332    class_attrs(Class, Attr),
  333    ansi_format(S, Attr, Fmt, Args).
  334prolog:message_line_element(S, ansi(Class, Fmt, Args, Ctx)) :-
  335    class_attrs(Class, Attr),
  336    ansi_format(S, Attr, Fmt, Args),
  337    (   nonvar(Ctx),
  338        Ctx = ansi(_, RI-RA)
  339    ->  keep_line_pos(S, format(S, RI, RA))
  340    ;   true
  341    ).
  342prolog:message_line_element(S, begin(Level, Ctx)) :-
  343    level_attrs(Level, Attr),
  344    stream_property(S, tty(true)),
  345    current_prolog_flag(color_term, true),
  346    !,
  347    (   is_list(Attr)
  348    ->  sgr_codes(Attr, Codes),
  349        atomic_list_concat(Codes, ;, Code)
  350    ;   sgr_code(Attr, Code)
  351    ),
  352    keep_line_pos(S, format(S, '\e[~wm', [Code])),
  353    Ctx = ansi('\e[0m', '\e[0m\e[~wm'-[Code]).
  354prolog:message_line_element(S, end(Ctx)) :-
  355    nonvar(Ctx),
  356    Ctx = ansi(Reset, _),
  357    keep_line_pos(S, write(S, Reset)).
  358
  359sgr_codes([], []).
  360sgr_codes([H0|T0], [H|T]) :-
  361    sgr_code(H0, H),
  362    sgr_codes(T0, T).
  363
  364level_attrs(Level,         Attrs) :-
  365    user:message_property(Level, color(Attrs)),
  366    !.
  367level_attrs(Level,         Attrs) :-
  368    class_attrs(message(Level), Attrs).
  369
  370class_attrs(Class, Attrs) :-
  371    user:message_property(Class, color(Attrs)),
  372    !.
  373class_attrs(Class, Attrs) :-
  374    prolog:console_color(Class, Attrs),
  375    !.
  376class_attrs(Class, Attrs) :-
  377    '$messages':default_theme(Class, Attrs),
  378    !.
  379class_attrs(Attrs, Attrs).
 keep_line_pos(+Stream, :Goal)
Run goal without changing the position information on Stream. This is used to avoid that the exchange of ANSI sequences modifies the notion of, notably, the line_pos notion.
  387keep_line_pos(S, G) :-
  388    stream_property(S, position(Pos)),
  389    !,
  390    setup_call_cleanup(
  391        stream_position_data(line_position, Pos, LPos),
  392        G,
  393        set_stream(S, line_position(LPos))).
  394keep_line_pos(_, G) :-
  395    call(G).
 ansi_get_color(+Which, -RGB) is semidet
Obtain the RGB color for an ANSI color parameter. Which is either a color alias or an integer ANSI color id. Defined aliases are foreground and background. This predicate sends a request to the console (user_output) and reads the reply. This assumes an xterm compatible terminal.
Arguments:
RGB- is a term rgb(Red,Green,Blue). The color components are integers in the range 0..65535.
  409:- if(current_predicate(call_with_time_limit/2)).  410ansi_get_color(Which0, RGB) :-
  411    stream_property(user_input, tty(true)),
  412    stream_property(user_output, tty(true)),
  413    stream_property(user_error, tty(true)),
  414    supports_get_color,
  415    (   color_alias(Which0, Which)
  416    ->  true
  417    ;   must_be(between(0,15),Which0)
  418    ->  Which = Which0
  419    ),
  420    catch(keep_line_pos(user_output,
  421                        ansi_get_color_(Which, RGB)),
  422          time_limit_exceeded,
  423          no_xterm).
  424
  425supports_get_color :-
  426    getenv('TERM', Term),
  427    sub_atom(Term, 0, _, _, xterm),
  428    \+ getenv('TERM_PROGRAM', 'Apple_Terminal').
  429
  430color_alias(foreground, 10).
  431color_alias(background, 11).
  432
  433ansi_get_color_(Which, rgb(R,G,B)) :-
  434    format(codes(Id), '~w', [Which]),
  435    hex4(RH),
  436    hex4(GH),
  437    hex4(BH),
  438    append([`\e]`, Id, `;rgb:`, RH, `/`, GH, `/`, BH, `\a`], Pattern),
  439    call_with_time_limit(0.05,
  440                         with_tty_raw(exchange_pattern(Which, Pattern))),
  441    !,
  442    hex_val(RH, R),
  443    hex_val(GH, G),
  444    hex_val(BH, B).
  445
  446no_xterm :-
  447    print_message(warning, ansi(no_xterm_get_colour)),
  448    fail.
  449
  450hex4([_,_,_,_]).
  451
  452hex_val([D1,D2,D3,D4], V) :-
  453    code_type(D1, xdigit(V1)),
  454    code_type(D2, xdigit(V2)),
  455    code_type(D3, xdigit(V3)),
  456    code_type(D4, xdigit(V4)),
  457    V is (V1<<12)+(V2<<8)+(V3<<4)+V4.
  458
  459exchange_pattern(Which, Pattern) :-
  460    format(user_output, '\e]~w;?\a', [Which]),
  461    flush_output(user_output),
  462    read_pattern(user_input, Pattern, []).
  463
  464read_pattern(From, Pattern, NotMatched0) :-
  465    copy_term(Pattern, TryPattern),
  466    append(Skip, Rest, NotMatched0),
  467    append(Rest, RestPattern, TryPattern),
  468    !,
  469    echo(Skip),
  470    try_read_pattern(From, RestPattern, NotMatched, Done),
  471    (   Done == true
  472    ->  Pattern = TryPattern
  473    ;   read_pattern(From, Pattern, NotMatched)
  474    ).
 try_read_pattern(+From, +Pattern, -NotMatched)
  478try_read_pattern(_, [], [], true) :-
  479    !.
  480try_read_pattern(From, [H|T], [C|RT], Done) :-
  481    get_code(C),
  482    (   C = H
  483    ->  try_read_pattern(From, T, RT, Done)
  484    ;   RT = [],
  485        Done = false
  486    ).
  487
  488echo([]).
  489echo([H|T]) :-
  490    put_code(user_output, H),
  491    echo(T).
  492
  493:- else.  494ansi_get_color(_Which0, _RGB) :-
  495    fail.
  496:- endif.  497
  498
  499
  500:- multifile prolog:message//1.  501
  502prolog:message(ansi(no_xterm_get_colour)) -->
  503    [ 'Terminal claims to be xterm compatible,'-[], nl,
  504      'but does not report colour info'-[]
  505    ]