Skip to content

Commit 17eed66

Browse files
committed
attach_console_x
1 parent 97a1603 commit 17eed66

File tree

5 files changed

+78
-15
lines changed

5 files changed

+78
-15
lines changed

prolog/metta_lang/metta_interp.pl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,8 @@
781781
% This directive allows the predicate `original_user_output/1` to be modified during runtime.
782782
:- dynamic(original_user_output/1).
783783

784+
785+
784786
%! original_user_output(-X) is nondet.
785787
%
786788
% Retrieves the stream associated with the standard output (file descriptor 1).
@@ -793,6 +795,8 @@
793795
% @example
794796
% ?- original_user_output(Stream).
795797
% Stream = <stream>.
798+
799+
original_user_output(X) :- thread_self(Id), thread_util:has_console(Id, _, X, _),!.
796800
original_user_output(X) :- stream_property(X, file_no(1)).
797801

798802
%! original_user_error(-X) is nondet.
@@ -807,6 +811,7 @@
807811
% @example
808812
% ?- original_user_error(Stream).
809813
% Stream = <stream>.
814+
original_user_error(X) :- thread_self(Id), thread_util:has_console(Id, _, _, X),!.
810815
original_user_error(X) :- stream_property(X, file_no(2)).
811816

812817
% Ensure that the original output stream is set if not already defined.
@@ -2343,6 +2348,7 @@
23432348
:- ensure_loaded(metta_convert).
23442349
:- ensure_loaded(metta_types).
23452350
:- ensure_loaded(metta_space).
2351+
:- ensure_loaded(metta_threads).
23462352
:- ensure_loaded(metta_eval).
23472353
:- nb_setval(self_space, '&top').
23482354

prolog/metta_lang/metta_repl.pl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,9 @@
296296
set_metta_prompt:-
297297
with_output_to(atom(P), write_metta_prompt),
298298
%prompt1(P),
299-
prompt(_, P).
300-
299+
metta_prompt(_, P).
301300

301+
metta_prompt(G,S):- once(nb_current('$metta_prompt',G);prompt(G,G)),nb_setval('$metta_prompt',S),prompt(_,S).
302302

303303
%! repl4 is det.
304304
% Executes the REPL logic by reading the input, processing expressions, and handling directives or commands.
@@ -575,8 +575,8 @@
575575
% Read the next line of input, accumulate it, and continue processing.
576576
repl_read_next(Accumulated, Expr) :-
577577
if_t(flag(need_prompt,1,0),(format('~N'),set_metta_prompt)),
578-
% On windows we need to output the prompt again
579-
(is_win64-> (ttyflush,prompt(P, P),write(P), ttyflush) ; true),
578+
% On windows we need to output the prompt again ; and now on linux since the prompt we are settign is ''
579+
maybe_write_prompt_now,
580580
% Read a line from the current input stream.
581581
read_line_to_string(current_input, Line),
582582
% switch prompts after the first line is read
@@ -592,10 +592,15 @@
592592
% Concatenate the accumulated input with the new line using a space between them.
593593
symbolics_to_string([Accumulated, "\n", Line], NewAccumulated), !,
594594
% Continue reading and processing the new accumulated input.
595-
format(atom(T),'| ~t',[]),prompt(_,T),
595+
format(atom(T),'| ~t',[]),metta_prompt(_,T),
596596
repl_read_next(NewAccumulated, Expr).
597597

598598

599+
maybe_write_prompt_now:- is_win64,!,metta_prompt(P, P),write_prompt_now(P).
600+
maybe_write_prompt_now:- thread_self(main),!.
601+
maybe_write_prompt_now:- metta_prompt(P, P),write_prompt_now(P),prompt(_, '').
602+
603+
write_prompt_now(P):- flush_output(current_output), ttyflush, write(P), ttyflush, flush_output(current_output).
599604

600605
% if stream error is not recoverable restart_reading
601606
check_unbalanced_parens(SE):- var(SE),!. % no error

prolog/metta_lang/metta_server.pl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@
215215
% Open the socket as a stream
216216
tcp_open_socket(RemoteFd, Stream),
217217
% Generate a unique symbol for the thread alias
218-
nb_setval(self_space,MSpace),
218+
(nb_setval(self_space,MSpace),
219219
% Handle the connection by processing incoming goals
220-
ignore(handle_vspace_peer(Stream)),
220+
ignore(handle_vspace_peer(Stream))),
221221
% Ensure the stream is closed when done
222222
catch(close(Stream),_,true)
223223
),

prolog/metta_lang/metta_threads.pl

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,10 +816,14 @@
816816

817817
mc_unit(repl_x):- repl_x.
818818

819+
820+
can_open_console_x:- getenv('DISPLAY', _),
821+
absolute_file_name(path(xterm), _XTerm, [access(execute)]).
822+
819823
repl_x :-
820824
repl_x(_).
821825

822-
repl_x(Title) :- can_open_console, !,
826+
repl_x(Title) :- can_open_console_x, !,
823827
thread_self(Me),
824828
thread_create(thread_run_repl_x(Me, Title),
825829
_Id,
@@ -833,20 +837,70 @@
833837
-> fail
834838
).
835839
repl_x(Title) :-
840+
print_message(error, cannot_attach_console_x(Title)),
841+
fail.
842+
843+
844+
thread_has_console_x :- current_prolog_flag(break_level, _), !.
845+
thread_has_console_x :- thread_self(Id), thread_has_console_x(Id), !.
846+
thread_has_console_x(main) :- !.
847+
thread_has_console_x(Id) :- thread_util:has_console(Id, _, _, _).
848+
849+
console_title_x(Thread, Title) :-
850+
current_prolog_flag(console_menu_version, qt), !, human_x_id(Thread, Id),
851+
format(atom(Title), 'Thread ~w', [Id]).
852+
console_title_x(Thread, Title) :-
853+
current_prolog_flag(system_thread_id, SysId), human_x_id(Thread, Id), format(atom(Title),
854+
'MeTTa Thread ~w (~d) X-REPL', [Id, SysId]).
855+
856+
human_x_id(Thread, Id) :- atom(Thread), !, Id=Thread.
857+
human_x_id(Thread, Id) :- thread_property(Th, alias(Id)), (Th==Thread; Thread==Id), !.
858+
human_x_id(Thread, Id) :- thread_property(Th, id(Id)), (Th==Thread; Thread==Id), !.
859+
human_x_id(Thread, Id) :- format(atom(Id),'~q',[Thread]), !.
860+
861+
862+
attach_console_x :- attach_console_x(_).
863+
864+
attach_console_x(_) :- thread_has_console_x, !.
865+
attach_console_x(Title) :- can_open_console_x, !,
866+
thread_self(Id),
867+
( var(Title)
868+
-> console_title_x(Id, Title)
869+
; true
870+
),
871+
thread_util:open_console(Title, In, Out, Err),
872+
assert(thread_util:has_console(Id, In, Out, Err)),
873+
set_stream(In, alias(user_input)),
874+
set_stream(Out, alias(user_output)),
875+
set_stream(Err, alias(user_error)),
876+
set_stream(In, alias(current_input)),
877+
set_stream(Out, alias(current_output)),
878+
thread_util:enable_line_editing(In, Out, Err),
879+
thread_at_exit(detach_console_x(Id)).
880+
attach_console_x(Title) :-
836881
print_message(error, cannot_attach_console(Title)),
837882
fail.
838883

884+
detach_console_x(Id) :-
885+
( retract(thread_util:has_console(Id, In, Out, Err))
886+
-> thread_util:disable_line_editing(In, Out, Err),
887+
close(In, [force(true)]),
888+
close(Out, [force(true)]),
889+
close(Err, [force(true)])
890+
; true
891+
).
892+
839893

840894
thread_run_repl_x :-
841895
set_prolog_flag(query_debug_settings, debug(false, false)),
842-
attach_console(_Title),
896+
attach_console_x(_Title),
843897
print_message(banner, thread_welcome),
844-
prolog.
898+
repl.
845899

846900
thread_run_repl_x(Creator, Title) :-
847901
set_prolog_flag(query_debug_settings, debug(false, false)),
848902
Error=error(Formal, _),
849-
( catch(attach_console(Title), Error, true)
903+
( catch(attach_console_x(Title), Error, true)
850904
-> ( var(Formal)
851905
-> thread_send_message(Creator, title(Title)),
852906
print_message(banner, thread_welcome),

prolog/metta_lang/metta_transpiled_header.pl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,9 @@
9090
:- dynamic(metta_atom_asserted_last/2).
9191
:- multifile(metta_atom_asserted_last/2).
9292

93-
94-
%:- initialization(maybe_load_history, now).
95-
96-
%:- ensure_loaded(library(metta_lang/metta_interp)).
93+
:- time(ensure_loaded(library(metta_lang/metta_interp))).
9794
:- ensure_loaded(library(metta_rt)). % avoids starting the REPL
95+
:- initialization(maybe_load_history, now).
9896
:- setup_library_calls.
9997
:- dynamic(top_call/0).
10098
:- multifile(top_call/0).

0 commit comments

Comments
 (0)