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)).

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

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