Skip to content

Commit 3575a02

Browse files
JanWielemakerclaude
andcommitted
CLEANUP: ansi_term: drop keep_line_pos/2
The stream layer no longer counts escape sequences as columns, so saving and restoring line_position/2 around every sequence written is redundant. set_stream/2 keeps the line_position property; it now has no callers in the system but remains useful for text whose width the stream cannot know. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LmQHEtHoVQ2NH2Zzusvepa
1 parent 84084e5 commit 3575a02

2 files changed

Lines changed: 29 additions & 30 deletions

File tree

library/ansi_term.pl

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@
9696
:- initialization
9797
init_color_term_flag.
9898

99-
:- meta_predicate
100-
keep_line_pos(+, 0).
101-
10299
:- multifile
103100
user:message_property/2.
104101

@@ -156,9 +153,9 @@
156153
atomic_list_concat(Codes, ;, Code),
157154
with_output_to(
158155
Stream,
159-
( keep_line_pos(current_output, format('\e[~wm', [Code])),
156+
( format('\e[~wm', [Code]),
160157
format(Format, Args),
161-
keep_line_pos(current_output, format('\e[0m'))
158+
format('\e[0m')
162159
)
163160
),
164161
flush_output.
@@ -367,7 +364,7 @@
367364
ansi_format(S, Attr, Fmt, Args),
368365
( nonvar(Ctx),
369366
Ctx = ansi(_, RI-RA)
370-
-> keep_line_pos(S, format(S, RI, RA))
367+
-> format(S, RI, RA)
371368
; true
372369
).
373370
prolog:message_line_element(S, url(Location)) :-
@@ -384,12 +381,12 @@
384381
atomic_list_concat(Codes, ;, Code)
385382
; sgr_code(Attr, Code)
386383
),
387-
keep_line_pos(S, format(S, '\e[~wm', [Code])),
384+
format(S, '\e[~wm', [Code]),
388385
Ctx = ansi('\e[0m', '\e[0m\e[~wm'-[Code]).
389386
prolog:message_line_element(S, end(Ctx)) :-
390387
nonvar(Ctx),
391388
Ctx = ansi(Reset, _),
392-
keep_line_pos(S, write(S, Reset)).
389+
write(S, Reset).
393390

394391
sgr_codes([], []).
395392
sgr_codes([H0|T0], [H|T]) :-
@@ -444,11 +441,9 @@
444441
true.
445442
ansi_hyperlink(Stream, Location, Label) =>
446443
( location_url(Location, URL)
447-
-> keep_line_pos(Stream,
448-
format(Stream, '\e]8;;~w\e\\', [URL])),
444+
-> format(Stream, '\e]8;;~w\e\\', [URL]),
449445
format(Stream, '~w', [Label]),
450-
keep_line_pos(Stream,
451-
format(Stream, '\e]8;;\e\\', []))
446+
format(Stream, '\e]8;;\e\\', [])
452447
; format(Stream, '~w', [Label])
453448
).
454449

@@ -530,22 +525,6 @@
530525
reserved(C) :- C >= 127.
531526
reserved(0'#).
532527

533-
%! keep_line_pos(+Stream, :Goal)
534-
%
535-
% Run goal without changing the position information on Stream. This
536-
% is used to avoid that the exchange of ANSI sequences modifies the
537-
% notion of, notably, the `line_pos` notion.
538-
539-
keep_line_pos(S, G) :-
540-
stream_property(S, position(Pos)),
541-
!,
542-
setup_call_cleanup(
543-
stream_position_data(line_position, Pos, LPos),
544-
G,
545-
set_stream(S, line_position(LPos))).
546-
keep_line_pos(_, G) :-
547-
call(G).
548-
549528
%! ansi_get_color(+Which, -RGB) is semidet.
550529
%
551530
% Obtain the RGB color for an ANSI color parameter. Which is either a
@@ -568,8 +547,7 @@
568547
; must_be(between(0,15),Which0)
569548
-> Which = Which0
570549
),
571-
catch(keep_line_pos(user_output,
572-
ansi_get_color_(Which, RGB)),
550+
catch(ansi_get_color_(Which, RGB),
573551
error(timeout_error(_,_), _),
574552
no_xterm).
575553

tests/library/test_ansi_term.pl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,25 @@
9494
test(multi_arguments, O = "\e[1mfoo bar\e[0m") :-
9595
captured_tty_output(ansi_format([bold], '~w~4|~w', [foo, bar]), O).
9696

97+
% Hyperlinks are OSC rather than SGR sequences and must not move the
98+
% column either.
99+
100+
test(hyperlink_position, O == "\e]8;;http://x\e\\ab\e]8;;\e\\ y") :-
101+
setup_call_cleanup(
102+
push_prolog_flag(hyperlink_term, true),
103+
captured_tty_output(
104+
( ansi_hyperlink(current_output, 'http://x', ab),
105+
format('~t~4|y')
106+
),
107+
O),
108+
pop_prolog_flag(hyperlink_term)).
109+
110+
% The column reported by line_position/2 ignores the escape sequences.
111+
112+
test(line_position, Pos == 2) :-
113+
captured_tty_output(
114+
( ansi_format([bold], 'ab', []),
115+
line_position(current_output, Pos)
116+
), _).
117+
97118
:- end_tests(ansi_term).

0 commit comments

Comments
 (0)