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:- use_module(library(apply)).   41:- use_module(library(lists)).   42:- use_module(library(error)).   43:- use_module(library(lists), [flatten/2]).

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 */
   59:- multifile
   60    prolog:console_color/2,                     % +Term, -AnsiAttrs
   61    supports_get_color/0.   62
   63
   64color_term_flag_default(true) :-
   65    stream_property(user_input, tty(true)),
   66    stream_property(user_error, tty(true)),
   67    stream_property(user_output, tty(true)),
   68    \+ getenv('TERM', dumb),
   69    !.
   70color_term_flag_default(false).
   71
   72init_color_term_flag :-
   73    color_term_flag_default(Default),
   74    create_prolog_flag(color_term, Default,
   75                       [ type(boolean),
   76                         keep(true)
   77                       ]).
   78
   79:- init_color_term_flag.   80
   81
   82:- meta_predicate
   83    keep_line_pos(+, 0).   84
   85:- multifile
   86    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

  128ansi_format(Attr, Format, Args) :-
  129    ansi_format(current_output, Attr, Format, Args).
  130
  131ansi_format(Stream, Class, Format, Args) :-
  132    stream_property(Stream, tty(true)),
  133    current_prolog_flag(color_term, true),
  134    !,
  135    class_attrs(Class, Attr),
  136    (   is_list(Attr)
  137    ->  maplist(sgr_code_ex, Attr, Codes0),
  138        flatten(Codes0, Codes),
  139        atomic_list_concat(Codes, ;, Code)
  140    ;   sgr_code_ex(Attr, Code0),
  141        (   is_list(Code0)
  142        ->  atomic_list_concat(Code0, ;, Code)
  143        ;   Code = Code0
  144        )
  145    ),
  146    format(string(Fmt), '\e[~~wm~w\e[0m', [Format]),
  147    format(Stream, Fmt, [Code|Args]),
  148    flush_output.
  149ansi_format(Stream, _Attr, Format, Args) :-
  150    format(Stream, Format, Args).
  151
  152sgr_code_ex(Attr, Code) :-
  153    sgr_code(Attr, Code),
  154    !.
  155sgr_code_ex(Attr, _) :-
  156    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
  197sgr_code(reset, 0).
  198sgr_code(bold,  1).
  199sgr_code(faint, 2).
  200sgr_code(italic, 3).
  201sgr_code(underline, 4).
  202sgr_code(blink(slow), 5).
  203sgr_code(blink(rapid), 6).
  204sgr_code(negative, 7).
  205sgr_code(conceal, 8).
  206sgr_code(crossed_out, 9).
  207sgr_code(font(primary), 10) :- !.
  208sgr_code(font(N), C) :-
  209    C is 10+N.
  210sgr_code(fraktur, 20).
  211sgr_code(underline(double), 21).
  212sgr_code(intensity(normal), 22).
  213sgr_code(fg(Name), C) :-
  214    (   ansi_color(Name, N)
  215    ->  C is N+30
  216    ;   rgb(Name, R, G, B)
  217    ->  sgr_code(fg(R,G,B), C)
  218    ).
  219sgr_code(bg(Name), C) :-
  220    !,
  221    (   ansi_color(Name, N)
  222    ->  C is N+40
  223    ;   rgb(Name, R, G, B)
  224    ->  sgr_code(bg(R,G,B), C)
  225    ).
  226sgr_code(framed, 51).
  227sgr_code(encircled, 52).
  228sgr_code(overlined, 53).
  229sgr_code(ideogram(underline), 60).
  230sgr_code(right_side_line, 60).
  231sgr_code(ideogram(underline(double)), 61).
  232sgr_code(right_side_line(double), 61).
  233sgr_code(ideogram(overlined), 62).
  234sgr_code(left_side_line, 62).
  235sgr_code(ideogram(stress_marking), 64).
  236sgr_code(-X, Code) :-
  237    off_code(X, Code).
  238sgr_code(hfg(Name), C) :-
  239    ansi_color(Name, N),
  240    C is N+90.
  241sgr_code(hbg(Name), C) :-
  242    !,
  243    ansi_color(Name, N),
  244    C is N+100.
  245sgr_code(fg8(Name), [38,5,N]) :-
  246    (   ansi_color(Name, N)
  247    ->  true
  248    ;   ansi_color(h(Name), N0)
  249    ->  N is N0+8
  250    ;   between(0, 255, Name)
  251    ->  N is Name
  252    ).
  253sgr_code(bg8(Name), [48,5,N]) :-
  254    (   ansi_color(Name, N)
  255    ->  true
  256    ;   ansi_color(h(Name), N0)
  257    ->  N is N0+8
  258    ;   between(0, 255, Name)
  259    ->  N is Name
  260    ).
  261sgr_code(fg(R,G,B), [38,2,R,G,B]) :-
  262    between(0, 255, R),
  263    between(0, 255, G),
  264    between(0, 255, B).
  265sgr_code(bg(R,G,B), [48,2,R,G,B]) :-
  266    between(0, 255, R),
  267    between(0, 255, G),
  268    between(0, 255, B).
  269
  270off_code(italic_and_franktur, 23).
  271off_code(underline, 24).
  272off_code(blink, 25).
  273off_code(negative, 27).
  274off_code(conceal, 28).
  275off_code(crossed_out, 29).
  276off_code(framed, 54).
  277off_code(overlined, 55).
  278
  279
  280ansi_color(black,   0).
  281ansi_color(red,     1).
  282ansi_color(green,   2).
  283ansi_color(yellow,  3).
  284ansi_color(blue,    4).
  285ansi_color(magenta, 5).
  286ansi_color(cyan,    6).
  287ansi_color(white,   7).
  288ansi_color(default, 9).
  289
  290rgb(Name, R, G, B) :-
  291    atom_codes(Name, [0'#,R1,R2,G1,G2,B1,B2]),
  292    hex_color(R1,R2,R),
  293    hex_color(G1,G2,G),
  294    hex_color(B1,B2,B).
  295rgb(Name, R, G, B) :-
  296    atom_codes(Name, [0'#,R1,G1,B1]),
  297    hex_color(R1,R),
  298    hex_color(G1,G),
  299    hex_color(B1,B).
  300
  301hex_color(D1,D2,V) :-
  302    code_type(D1, xdigit(V1)),
  303    code_type(D2, xdigit(V2)),
  304    V is 16*V1+V2.
  305
  306hex_color(D1,V) :-
  307    code_type(D1, xdigit(V1)),
  308    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.
  321                 /*******************************
  322                 *             HOOK             *
  323                 *******************************/
 prolog:message_line_element(+Stream, +Term) is semidet
Hook implementation that deals with ansi(+Attr, +Fmt, +Args) in message specifications.
  330prolog:message_line_element(S, ansi(Class, Fmt, Args)) :-
  331    class_attrs(Class, Attr),
  332    ansi_format(S, Attr, Fmt, Args).
  333prolog:message_line_element(S, ansi(Class, Fmt, Args, Ctx)) :-
  334    class_attrs(Class, Attr),
  335    ansi_format(S, Attr, Fmt, Args),
  336    (   nonvar(Ctx),
  337        Ctx = ansi(_, RI-RA)
  338    ->  keep_line_pos(S, format(S, RI, RA))
  339    ;   true
  340    ).
  341prolog:message_line_element(S, begin(Level, Ctx)) :-
  342    level_attrs(Level, Attr),
  343    stream_property(S, tty(true)),
  344    current_prolog_flag(color_term, true),
  345    !,
  346    (   is_list(Attr)
  347    ->  maplist(sgr_code, Attr, Codes),
  348        atomic_list_concat(Codes, ;, Code)
  349    ;   sgr_code(Attr, Code)
  350    ),
  351    keep_line_pos(S, format(S, '\e[~wm', [Code])),
  352    Ctx = ansi('\e[0m', '\e[0m\e[~wm'-[Code]).
  353prolog:message_line_element(S, end(Ctx)) :-
  354    nonvar(Ctx),
  355    Ctx = ansi(Reset, _),
  356    keep_line_pos(S, write(S, Reset)).
  357
  358level_attrs(Level,         Attrs) :-
  359    user:message_property(Level, color(Attrs)),
  360    !.
  361level_attrs(Level,         Attrs) :-
  362    class_attrs(message(Level), Attrs).
  363
  364class_attrs(Class, Attrs) :-
  365    user:message_property(Class, color(Attrs)),
  366    !.
  367class_attrs(Class, Attrs) :-
  368    prolog:console_color(Class, Attrs),
  369    !.
  370class_attrs(Class, Attrs) :-
  371    '$messages':default_theme(Class, Attrs),
  372    !.
  373class_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.
  381keep_line_pos(S, G) :-
  382    stream_property(S, position(Pos)),
  383    !,
  384    setup_call_cleanup(
  385        stream_position_data(line_position, Pos, LPos),
  386        G,
  387        set_stream(S, line_position(LPos))).
  388keep_line_pos(_, G) :-
  389    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.
  403ansi_get_color(Which0, RGB) :-
  404    stream_property(user_input, tty(true)),
  405    stream_property(user_output, tty(true)),
  406    stream_property(user_error, tty(true)),
  407    supports_get_color,
  408    (   color_alias(Which0, Which)
  409    ->  true
  410    ;   must_be(between(0,15),Which0)
  411    ->  Which = Which0
  412    ),
  413    catch(keep_line_pos(user_output,
  414                        ansi_get_color_(Which, RGB)),
  415          time_limit_exceeded,
  416          no_xterm).
  417
  418supports_get_color :-
  419    getenv('TERM', Term),
  420    sub_atom(Term, 0, _, _, xterm),
  421    \+ getenv('TERM_PROGRAM', 'Apple_Terminal').
  422
  423color_alias(foreground, 10).
  424color_alias(background, 11).
  425
  426ansi_get_color_(Which, rgb(R,G,B)) :-
  427    format(codes(Id), '~w', [Which]),
  428    hex4(RH),
  429    hex4(GH),
  430    hex4(BH),
  431    append([`\e]`, Id, `;rgb:`, RH, `/`, GH, `/`, BH, `\a`], Pattern),
  432    call_with_time_limit(0.05,
  433                         with_tty_raw(exchange_pattern(Which, Pattern))),
  434    !,
  435    hex_val(RH, R),
  436    hex_val(GH, G),
  437    hex_val(BH, B).
  438
  439no_xterm :-
  440    print_message(warning, ansi(no_xterm_get_colour)),
  441    fail.
  442
  443hex4([_,_,_,_]).
  444
  445hex_val([D1,D2,D3,D4], V) :-
  446    code_type(D1, xdigit(V1)),
  447    code_type(D2, xdigit(V2)),
  448    code_type(D3, xdigit(V3)),
  449    code_type(D4, xdigit(V4)),
  450    V is (V1<<12)+(V2<<8)+(V3<<4)+V4.
  451
  452exchange_pattern(Which, Pattern) :-
  453    format(user_output, '\e]~w;?\a', [Which]),
  454    flush_output(user_output),
  455    read_pattern(user_input, Pattern, []).
  456
  457read_pattern(From, Pattern, NotMatched0) :-
  458    copy_term(Pattern, TryPattern),
  459    append(Skip, Rest, NotMatched0),
  460    append(Rest, RestPattern, TryPattern),
  461    !,
  462    echo(Skip),
  463    try_read_pattern(From, RestPattern, NotMatched, Done),
  464    (   Done == true
  465    ->  Pattern = TryPattern
  466    ;   read_pattern(From, Pattern, NotMatched)
  467    ).
 try_read_pattern(+From, +Pattern, -NotMatched)
  471try_read_pattern(_, [], [], true) :-
  472    !.
  473try_read_pattern(From, [H|T], [C|RT], Done) :-
  474    get_code(C),
  475    (   C = H
  476    ->  try_read_pattern(From, T, RT, Done)
  477    ;   RT = [],
  478        Done = false
  479    ).
  480
  481echo([]).
  482echo([H|T]) :-
  483    put_code(user_output, H),
  484    echo(T).
  485
  486
  487:- multifile prolog:message//1.  488
  489prolog:message(ansi(no_xterm_get_colour)) -->
  490    [ 'Terminal claims to be xterm compatible,'-[], nl,
  491      'but does not report colour info'-[]
  492    ]