1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: jan@swi-prolog.org 5 WWW: https://www.swi-prolog.org 6 Copyright (c) 2010-2026, VU University Amsterdam 7 CWI, Amsterdam 8 SWI-Prolog Solutions b.v. 9 All rights reserved. 10 11 Redistribution and use in source and binary forms, with or without 12 modification, are permitted provided that the following conditions 13 are met: 14 15 1. Redistributions of source code must retain the above copyright 16 notice, this list of conditions and the following disclaimer. 17 18 2. Redistributions in binary form must reproduce the above copyright 19 notice, this list of conditions and the following disclaimer in 20 the documentation and/or other materials provided with the 21 distribution. 22 23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 33 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 POSSIBILITY OF SUCH DAMAGE. 35*/ 36 37:- module(ansi_term, 38 [ ansi_format/3, % +Attr, +Format, +Args 39 ansi_format/4, % +Stream, +Attr, +Format, +Args 40 ansi_sgr/2, % +Attr, -Sequence 41 ansi_get_color/2, % +Which, -rgb(R,G,B) 42 ansi_hyperlink/2, % +Stream,+Location 43 ansi_hyperlink/3 % +Stream,+URL,+Label 44 ]). 45:- autoload(library(error), [domain_error/2, must_be/2, instantiation_error/1]). 46:- autoload(library(lists), [append/3, selectchk/3]). 47:- autoload(library(utf8), [utf8_codes/3]). 48:- autoload(library(apply), [maplist/2]).
75:- multifile 76 prolog:console_color/2, % +Term, -AnsiAttrs 77 supports_get_color/0, 78 hyperlink/2, % +Stream, +Spec 79 tty_url_hook/2. % +For, -URL 80 81color_term_flag_default(true) :- 82 stream_property(user_input, tty(true)), 83 stream_property(user_error, tty(true)), 84 stream_property(user_output, tty(true)), 85 \+ getenv('TERM', dumb), 86 !. 87color_term_flag_default(false).
98hyperlink_term_flag_default(true) :- 99 color_term_flag_default(true), 100 \+ getenv('TMUX', _), 101 \+ getenv('STY', _), 102 osc8_terminal, 103 !. 104hyperlink_term_flag_default(false). 105 106osc8_terminal :- 107 getenv('TERM_PROGRAM', Program), 108 osc8_term_program(Program), 109 !. 110osc8_terminal :- 111 getenv('TERM', Term), 112 osc8_term(Term), 113 !. 114osc8_terminal :- 115 getenv('VTE_VERSION', Atom), % GNOME Terminal, Tilix, ... 116 atom_number(Atom, Version), 117 Version >= 5000, 118 !. 119osc8_terminal :- 120 getenv('KITTY_WINDOW_ID', _), 121 !. 122osc8_terminal :- 123 getenv('WT_SESSION', _). % Windows Terminal 124 125osc8_term_program('Epilog'). 126osc8_term_program('iTerm.app'). 127osc8_term_program('WezTerm'). 128osc8_term_program(ghostty). 129osc8_term_program(vscode). 130 131osc8_term('xterm-kitty'). 132osc8_term('xterm-ghostty'). 133osc8_term(foot). 134osc8_term('foot-extra'). 135osc8_term(wezterm). 136osc8_term(alacritty). 137 138init_color_term_flag :- 139 color_term_flag_default(Color), 140 create_prolog_flag(color_term, Color, 141 [ type(boolean), 142 keep(true) 143 ]), 144 hyperlink_term_flag_default(Hyperlink), 145 create_prolog_flag(hyperlink_term, Hyperlink, 146 [ type(boolean), 147 keep(true) 148 ]). 149 150:- initialization 151 init_color_term_flag. 152 153:- multifile 154 user:message_property/2.
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:
fg(Color) and bg(Color), the colour name can be '#RGB' or
'#RRGGBB'h(Color)
or an integer 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
current_output has the property tty(true) (see
stream_property/2).color_term is true.199ansi_format(Attr, Format, Args) :- 200 ansi_format(current_output, Attr, Format, Args). 201 202ansi_format(Stream, Class, Format, Args) :- 203 stream_property(Stream, tty(true)), 204 current_prolog_flag(color_term, true), 205 class_attrs(Class, Attr), 206 Attr \== [], 207 !, 208 ( selectchk(href(HREF), Attr, Attr1) 209 -> true 210 ; Attr1 = Attr 211 ), 212 sgr_sequence(Attr1, Sequence), 213 with_output_to( 214 Stream, 215 ( write(Sequence), 216 format_content(Format, Args, HREF), 217 format('\e[0m') 218 ) 219 ), 220 flush_output. 221ansi_format(Stream, _Attr, Format, Args) :- 222 format(Stream, Format, Args). 223 224format_content(Format, Args, HREF) :- 225 var(HREF), 226 !, 227 format(Format, Args). 228format_content(Format, Args, HREF) :- 229 format(string(Label), Format, Args), 230 ansi_hyperlink(current_output, HREF, Label).
color_term is false or the class
resolves to no attributes.
Unlike ansi_format/4 this does not write to a stream and thus does
not require a terminal. It is used to decorate strings that are
handed to code that is not aware of colors, notably the toplevel
prompt (see the Prolog flag toplevel_prompt). As the caller
controls where the sequence ends up, the caller is also responsible
for verifying that the destination is a terminal and for emitting
the reset sequence `\e[0m`.
249ansi_sgr(Class, Sequence) :- 250 current_prolog_flag(color_term, true), 251 class_attrs(Class, Attr), 252 Attr \== [], 253 ( selectchk(href(_), Attr, Attr1) 254 -> true 255 ; Attr1 = Attr 256 ), 257 !, 258 sgr_sequence(Attr1, Sequence). 259ansi_sgr(_, "").
bg8(Color).266sgr_sequence(Attrs, Sequence) :- 267 phrase(sgr_codes_ex(Attrs), Codes), 268 codes_sequence(Codes, Sequence). 269 270codes_sequence(Codes, Sequence) :- 271 atomics_to_string(Codes, ;, Code), 272 format(string(Sequence), '\e[~wm', [Code]).
280sgr_codes(X) --> 281 { var(X), 282 !, 283 fail 284 }. 285sgr_codes([]) --> 286 !. 287sgr_codes([H|T]) --> 288 !, 289 sgr_codes(H), 290 sgr_codes(T). 291sgr_codes(Attr) --> 292 { sgr_code(Attr, Code) }, 293 ( { is_list(Code) } 294 -> list(Code) 295 ; [Code] 296 ). 297 298sgr_codes_ex(X) --> 299 { var(X), 300 !, 301 instantiation_error(X) 302 }. 303sgr_codes_ex([]) --> 304 !. 305sgr_codes_ex([H|T]) --> 306 !, 307 sgr_codes_ex(H), 308 sgr_codes_ex(T). 309sgr_codes_ex(Attr) --> 310 ( { sgr_code(Attr, Code) } 311 -> ( { is_list(Code) } 312 -> list(Code) 313 ; [Code] 314 ) 315 ; { domain_error(sgr_code, Attr) } 316 ). 317 318list([]) --> []. 319list([H|T]) --> [H], list(T).
| reset | all 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) | |
| -Off | Switch attributes off |
hfg(Name) | Color name |
hbg(Name) | Color name |
361sgr_code(reset, 0). 362sgr_code(bold, 1). 363sgr_code(faint, 2). 364sgr_code(italic, 3). 365sgr_code(underline, 4). 366sgr_code(blink(slow), 5). 367sgr_code(blink(rapid), 6). 368sgr_code(negative, 7). 369sgr_code(conceal, 8). 370sgr_code(crossed_out, 9). 371sgr_code(font(primary), 10) :- !. 372sgr_code(font(N), C) :- 373 C is 10+N. 374sgr_code(fraktur, 20). 375sgr_code(underline(double), 21). 376sgr_code(intensity(normal), 22). 377sgr_code(fg(Name), C) :- 378 ( ansi_color(Name, N) 379 -> C is N+30 380 ; rgb(Name, R, G, B) 381 -> sgr_code(fg(R,G,B), C) 382 ). 383sgr_code(bg(Name), C) :- 384 !, 385 ( ansi_color(Name, N) 386 -> C is N+40 387 ; rgb(Name, R, G, B) 388 -> sgr_code(bg(R,G,B), C) 389 ). 390sgr_code(framed, 51). 391sgr_code(encircled, 52). 392sgr_code(overlined, 53). 393sgr_code(ideogram(underline), 60). 394sgr_code(right_side_line, 60). 395sgr_code(ideogram(underline(double)), 61). 396sgr_code(right_side_line(double), 61). 397sgr_code(ideogram(overlined), 62). 398sgr_code(left_side_line, 62). 399sgr_code(ideogram(stress_marking), 64). 400sgr_code(-X, Code) :- 401 off_code(X, Code). 402sgr_code(hfg(Name), C) :- 403 ansi_color(Name, N), 404 C is N+90. 405sgr_code(hbg(Name), C) :- 406 !, 407 ansi_color(Name, N), 408 C is N+100. 409sgr_code(fg8(Name), [38,5,N]) :- 410 ansi_color8(Name, N). 411sgr_code(bg8(Name), [48,5,N]) :- 412 ansi_color8(Name, N). 413sgr_code(fg(R,G,B), [38,2,R,G,B]) :- 414 between(0, 255, R), 415 between(0, 255, G), 416 between(0, 255, B). 417sgr_code(bg(R,G,B), [48,2,R,G,B]) :- 418 between(0, 255, R), 419 between(0, 255, G), 420 between(0, 255, B). 421 422off_code(italic_and_franktur, 23). 423off_code(underline, 24). 424off_code(blink, 25). 425off_code(negative, 27). 426off_code(conceal, 28). 427off_code(crossed_out, 29). 428off_code(framed, 54). 429off_code(overlined, 55). 430 431ansi_color8(h(Name), N) :- 432 !, 433 ansi_color(Name, N0), 434 N is N0+8. 435ansi_color8(Name, N) :- 436 atom(Name), 437 !, 438 ansi_color(Name, N). 439ansi_color8(N, N) :- 440 between(0, 255, N). 441 442ansi_color(black, 0). 443ansi_color(red, 1). 444ansi_color(green, 2). 445ansi_color(yellow, 3). 446ansi_color(blue, 4). 447ansi_color(magenta, 5). 448ansi_color(cyan, 6). 449ansi_color(white, 7). 450ansi_color(default, 9). 451 452rgb(Name, R, G, B) :- 453 atom_codes(Name, [0'#,R1,R2,G1,G2,B1,B2]), 454 hex_color(R1,R2,R), 455 hex_color(G1,G2,G), 456 hex_color(B1,B2,B). 457rgb(Name, R, G, B) :- 458 atom_codes(Name, [0'#,R1,G1,B1]), 459 hex_color(R1,R), 460 hex_color(G1,G), 461 hex_color(B1,B). 462 463hex_color(D1,D2,V) :- 464 code_type(D1, xdigit(V1)), 465 code_type(D2, xdigit(V2)), 466 V is 16*V1+V2. 467 468hex_color(D1,V) :- 469 code_type(D1, xdigit(V1)), 470 V is 16*V1+V1.
boot/messages.pl, default_theme/2.
Besides the classes used for messages (code, comment, var,
warning, error, truth(Truth), port(Port), message(Kind),
...), the interactive toplevel uses these:
?- prompt and its `| ` continuation.odd or even
and alternates over the answers of a single query, which allows
for striping the answers using a background color. Only an
answer that shows bindings, residual goals or delays uses this
class: true. and false. are not answers to stripe, and
neither is the empty line that separates the answer from the
next query.X = 1.
The debugger uses, besides frame(level) and port(Port):
odd or even
and alternates over the steps of a trace, which allows for
striping the goals using a background color. Match on Port to
color the goal by port instead of (or in addition to) striping.
Note that a background color on prompt, input or answer(_) is
painted up to the right margin using `\e[K`. A background on
goal(_,_) is not: the debugger writes its ? prompt on the same
line.
515 /******************************* 516 * HOOK * 517 *******************************/
nl. Resetting matters because a
terminal that scrolls while a background colour is in effect
paints the newly exposed line with it.Ctx is the message context. It is created by the handler for begin/2 as a term
ansi(Reset, ReInstall, EraseEol)
where Reset is the sequence written by end/1, ReInstall is a
Format-Args pair that re-installs the attributes of the message and
EraseEol is the sequence that paints the remainder of the line or
the empty atom. Callers must treat Ctx as opaque. It is left
unbound if Stream is not a terminal, if the color_term flag is
false or if the message has no attributes. All handlers that use
Ctx therefore fail if it is unbound, which makes
print_message_lines/3 fall back to writing plain text.
562prolog:message_line_element(S, ansi(Class, Fmt, Args)) :- 563 class_attrs(Class, Attr), 564 ansi_format(S, Attr, Fmt, Args). 565prolog:message_line_element(S, ansi(Class, Fmt, Args, Ctx)) :- 566 class_attrs(Class, Attr), 567 ansi_format(S, Attr, Fmt, Args), 568 reinstall_message_attrs(S, Ctx). 569prolog:message_line_element(S, nl(Ctx)) :- 570 nonvar(Ctx), 571 Ctx = ansi(_, _, EOL), 572 write(S, EOL), 573 nl(S). 574prolog:message_line_element(S, flush(Ctx)) :- 575 nonvar(Ctx), 576 Ctx = ansi(_, _, EOL), 577 write(S, EOL), 578 flush_output(S). 579prolog:message_line_element(S, eol(Ctx)) :- 580 nonvar(Ctx), 581 Ctx = ansi(Reset, _, EOL), 582 write(S, EOL), 583 write(S, Reset). 584prolog:message_line_element(S, url(Location)) :- 585 ansi_hyperlink(S, Location). 586prolog:message_line_element(S, url(URL, Label)) :- 587 link_label(Label, Class, Fmt, Args, Ctx), 588 !, 589 format(string(Text), Fmt, Args), 590 ( ansi_sgr_for(S, Class, Sequence) 591 -> write(S, Sequence), 592 ansi_hyperlink(S, URL, Text), 593 write(S, '\e[0m'), 594 reinstall_message_attrs(S, Ctx) 595 ; ansi_hyperlink(S, URL, Text) 596 ). 597prolog:message_line_element(S, url(URL, Label)) :- 598 ansi_hyperlink(S, URL, Label). 599prolog:message_line_element(S, begin(Level, Ctx)) :- 600 level_attrs(Level, Attr), 601 Attr \== [], 602 stream_property(S, tty(true)), 603 current_prolog_flag(color_term, true), 604 phrase(sgr_codes(Attr), Codes), % fails on a kind without a theme 605 !, 606 codes_sequence(Codes, Sequence), 607 write(S, Sequence), 608 erase_eol(Attr, EOL), 609 Ctx = ansi('\e[0m', '\e[0m~w'-[Sequence], EOL). 610prolog:message_line_element(S, end(Ctx)) :- 611 nonvar(Ctx), 612 Ctx = ansi(Reset, _, _), 613 write(S, Reset).
620reinstall_message_attrs(S, Ctx) :-
621 ( nonvar(Ctx),
622 Ctx = ansi(_, RI-RA, _)
623 -> format(S, RI, RA)
624 ; true
625 ).632link_label(Fmt-Args, -, Fmt, Args, _) :- 633 atom(Fmt), 634 is_list(Args), 635 !. 636link_label(ansi(Class, Fmt, Args), Class, Fmt, Args, _). 637link_label(ansi(Class, Fmt, Args, Ctx), Class, Fmt, Args, Ctx).
645ansi_sgr_for(S, Class, Sequence) :-
646 Class \== (-),
647 stream_property(S, tty(true)),
648 ansi_sgr(Class, Sequence),
649 Sequence \== "".658erase_eol(Attrs, EOL) :- 659 ( is_list(Attrs), 660 has_background(Attrs) 661 -> EOL = '\e[K' 662 ; EOL = '' 663 ). 664 665has_background([H|T]) :- 666 ( background(H) 667 -> true 668 ; has_background(T) 669 ). 670 671background(bg(_)). 672background(bg(_,_,_)). 673background(bg8(_)). 674background(hbg(_)). 675 676level_attrs(Level, Attrs) :- 677 user:message_property(Level, color(Attrs)), 678 !. 679level_attrs(Level, Attrs) :- 680 class_attrs(message(Level), Attrs). 681 682class_attrs(Class, Attrs) :- 683 user:message_property(Class, color(Attrs)), 684 !. 685class_attrs(Class, Attrs) :- 686 prolog:console_color(Class, Attrs), 687 !. 688class_attrs(Class, Attrs) :- 689 '$messages':default_theme(Class, Attrs), 690 !. 691class_attrs(Attrs, Attrs).
OSC 8
escape sequence. Location is one of
File:LineFile:Line:ColumnThere is no official standard for encoding the Line and Column. We emit
``file://AbsFileName[#Line[:Column]]``
Both Line and Column count from 1, as in the messages we
print and as used by e.g., rg --hyperlink-format=.... Note that
a capital L before the line, as used by GitHub, is accepted by
Epilog as well.
The sequence is emitted if and only if the Prolog flag
hyperlink_term is true and Stream has the property tty(true).
ansi_format/4 is guarded the same way for color_term, so that a
message captured using with_output_to/2 is plain text.
721ansi_hyperlink(Stream, Location) :- 722 hyperlink(Stream, url(Location)), 723 !. 724ansi_hyperlink(Stream, Location) :- 725 location_label(Location, Label), 726 ansi_hyperlink(Stream, Location, Label). 727 728location_label(File:Line:Column, Label) => 729 format(string(Label), '~w:~w:~w', [File,Line,Column]). 730location_label(File:Line, Label) => 731 format(string(Label), '~w:~w', [File,Line]). 732location_label(File, Label) => 733 format(string(Label), '~w', [File]). 734 735ansi_hyperlink(Stream, Location, Label), 736 hyperlink(Stream, url(Location, Label)) => 737 true. 738ansi_hyperlink(Stream, Location, Label) => 739 ( hyperlink_stream(Stream), 740 location_url(Location, URL) 741 -> format(Stream, '\e]8;;~w\e\\', [URL]), 742 format(Stream, '~w', [Label]), 743 format(Stream, '\e]8;;\e\\', []) 744 ; format(Stream, '~w', [Label]) 745 ).
752hyperlink_stream(Stream) :-
753 current_prolog_flag(hyperlink_term, true),
754 stream_property(Stream, tty(true)).761is_url(URL) :- 762 ( atom(URL) 763 -> true 764 ; string(URL) 765 ), 766 sub_string(URL, Before, _, _, :), 767 !, 768 Before > 0, 769 sub_string(URL, 0, Before, _, Scheme), 770 atom_codes(Scheme, Codes), 771 maplist(between(0'a, 0'z), Codes), 772 not_drive_scheme(Scheme). 773 774:- if(current_prolog_flag(windows, true)). 775not_drive_scheme(Scheme) :- 776 \+ string_length(Scheme, 1). 777:- else. 778not_drive_scheme(_). 779:- endif.
787location_url(Location, URL), 788 tty_url_hook(Location, URL0) => 789 URL = URL0. 790location_url(File:Line:Column, URL) => 791 url_file_name(FileURL, File), 792 format(string(URL), '~w#~d:~d', [FileURL, Line, Column]). 793location_url(File:Line, URL) => 794 url_file_name(FileURL, File), 795 format(string(URL), '~w#~w', [FileURL, Line]). 796location_url(File, URL) => 797 url_file_name(URL, File).
clib package and its foreign support.809url_file_name(URL, File) :- 810 is_url(File), !, 811 URL = File. 812url_file_name(URL, File) :- 813 absolute_file_name(File, AbsFile), 814 ensure_leading_slash(AbsFile, AbsFile1), 815 url_encode_path(AbsFile1, Encoded), 816 format(string(URL), 'file://~s', [Encoded]). 817 818ensure_leading_slash(Path, SlashPath) :- 819 ( sub_atom(Path, 0, _, _, /) 820 -> SlashPath = Path 821 ; atom_concat(/, Path, SlashPath) 822 ). 823 824url_encode_path(Name, Encoded) :- 825 atom_codes(Name, Codes), 826 phrase(utf8_codes(Codes), UTF8), 827 phrase(encode(UTF8), Encoded). 828 829encode([]) --> []. 830encode([H|T]) --> encode1(H), encode(T). 831 832encode1(C) --> 833 { reserved(C), 834 !, 835 format(codes([C1,C2]), '~`0t~16r~2|', [C]) 836 }, 837 "%", [C1,C2]. 838encode1(C) --> 839 [C]. 840 841reserved(C) :- C =< 0'\s. 842reserved(C) :- C >= 127. 843reserved(0'#).
foreground and background. This predicate sends a request to the
console (user_output) and reads the reply. This assumes an xterm
compatible terminal.
863ansi_get_color(Which0, RGB) :- 864 \+ current_prolog_flag(console_menu, true), 865 stream_property(user_input, tty(true)), 866 stream_property(user_output, tty(true)), 867 stream_property(user_error, tty(true)), 868 supports_get_color, 869 ( color_alias(Which0, Which) 870 -> true 871 ; must_be(between(0,15),Which0) 872 -> Which = Which0 873 ), 874 catch(ansi_get_color_(Which, RGB), 875 error(timeout_error(_,_), _), 876 no_xterm). 877 878supports_get_color :- 879 \+ current_prolog_flag(windows, true), 880 getenv('TERM', Term), 881 sub_atom(Term, 0, _, _, xterm), 882 \+ getenv('TERM_PROGRAM', 'Apple_Terminal'). 883 884color_alias(foreground, 10). 885color_alias(background, 11). 886 887ansi_get_color_(Which, RGB) :- 888 stream_property(user_input, timeout(Old)), 889 setup_call_cleanup( 890 set_stream(user_input, timeout(0.05)), 891 with_tty_raw(exchange_color(Which, RGB)), 892 set_stream(user_input, timeout(Old))), 893 !. 894 895no_xterm :- 896 print_message(warning, ansi(no_xterm_get_colour)), 897 fail.
903exchange_color(Which, RGB) :-
904 format(user_output, '\e]~w;?\a', [Which]),
905 flush_output(user_output),
906 format(codes(Id), '~w', [Which]),
907 read_osc_reply(user_input, Id, Codes),
908 phrase(color_reply(RGB), Codes).ESC ] Param ; and the string terminator. Terminals reply with
either BEL or ST (ESC \), so we accept both.
Anything that is not our reply is echoed: it was typed by the user while we were waiting rather than sent by the terminal.
919read_osc_reply(In, Param, Body) :- 920 get_code(In, C), 921 C \== -1, 922 ( C == 0'\e, 923 osc_reply(In, Param, Body0) 924 -> Body = Body0 925 ; put_code(user_output, C), 926 read_osc_reply(In, Param, Body) 927 ). 928 929osc_reply(In, Param, Body) :- 930 get_code(In, 0']), 931 read_osc_param(In, Param), 932 read_osc_string(In, Body). 933 934read_osc_param(In, Param) :- 935 read_osc_param_(In, Codes), 936 Codes == Param. 937 938read_osc_param_(In, Codes) :- 939 get_code(In, C), 940 C \== -1, 941 ( C == 0'; 942 -> Codes = [] 943 ; Codes = [C|T], 944 read_osc_param_(In, T) 945 ). 946 947read_osc_string(In, Codes) :- 948 get_code(In, C), 949 C \== -1, 950 ( C == 0'\a % BEL 951 -> Codes = [] 952 ; C == 0'\e % ST: ESC \ 953 -> get_code(In, 0'\\), 954 Codes = [] 955 ; Codes = [C|T], 956 read_osc_string(In, T) 957 ).
968color_reply(rgb(R,G,B)) --> 969 "rgb:", !, 970 hex_component(R), "/", hex_component(G), "/", hex_component(B). 971color_reply(rgb(R,G,B)) --> % #RGB, #RRGGBB, ... 972 "#", 973 hex_digits(Ds), 974 { length(Ds, Len), 975 N is Len//3, 976 N > 0, N =< 4, 977 length(RDs, N), length(GDs, N), length(BDs, N), 978 append(RDs, Rest, Ds), 979 append(GDs, BDs, Rest), 980 hex_value(RDs, R), 981 hex_value(GDs, G), 982 hex_value(BDs, B) 983 }. 984 985hex_component(V) --> 986 hex_digits(Ds), 987 { Ds \== [], 988 length(Ds, N), 989 N =< 4, 990 hex_value(Ds, V) 991 }. 992 993hex_digits([H|T]) --> 994 [H], 995 { code_type(H, xdigit(_)) }, 996 !, 997 hex_digits(T). 998hex_digits([]) --> 999 [].
prefer_rationals.1007hex_value(Ds, V) :- 1008 hex_value(Ds, 0, V0), 1009 length(Ds, N), 1010 Max is 16^N - 1, 1011 V is float(V0)/Max. 1012 1013hex_value([], V, V). 1014hex_value([D|T], V0, V) :- 1015 code_type(D, xdigit(DV)), 1016 V1 is V0*16+DV, 1017 hex_value(T, V1, V). 1018 1019:- multifile prolog:message//1. 1020 1021prologmessage(ansi(no_xterm_get_colour)) --> 1022 [ 'Terminal claims to be xterm compatible,'-[], nl, 1023 'but does not report colour info'-[] 1024 ]
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:
The behavior of this library is controlled by two Prolog flags:
true, activate the color output for this library. Otherwise simply call format/3.url(Location)andurl(URL, Label)elements of Prolog messages. The default istrueif the environment identifies a terminal known to support OSC 8 hyperlinks.