|
816 | 816 |
|
817 | 817 | mc_unit(repl_x):- repl_x. |
818 | 818 |
|
| 819 | + |
| 820 | +can_open_console_x:- getenv('DISPLAY', _), |
| 821 | + absolute_file_name(path(xterm), _XTerm, [access(execute)]). |
| 822 | + |
819 | 823 | repl_x :- |
820 | 824 | repl_x(_). |
821 | 825 |
|
822 | | -repl_x(Title) :- can_open_console, !, |
| 826 | +repl_x(Title) :- can_open_console_x, !, |
823 | 827 | thread_self(Me), |
824 | 828 | thread_create(thread_run_repl_x(Me, Title), |
825 | 829 | _Id, |
|
833 | 837 | -> fail |
834 | 838 | ). |
835 | 839 | 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) :- |
836 | 881 | print_message(error, cannot_attach_console(Title)), |
837 | 882 | fail. |
838 | 883 |
|
| 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 | + |
839 | 893 |
|
840 | 894 | thread_run_repl_x :- |
841 | 895 | set_prolog_flag(query_debug_settings, debug(false, false)), |
842 | | - attach_console(_Title), |
| 896 | + attach_console_x(_Title), |
843 | 897 | print_message(banner, thread_welcome), |
844 | | - prolog. |
| 898 | + repl. |
845 | 899 |
|
846 | 900 | thread_run_repl_x(Creator, Title) :- |
847 | 901 | set_prolog_flag(query_debug_settings, debug(false, false)), |
848 | 902 | Error=error(Formal, _), |
849 | | - ( catch(attach_console(Title), Error, true) |
| 903 | + ( catch(attach_console_x(Title), Error, true) |
850 | 904 | -> ( var(Formal) |
851 | 905 | -> thread_send_message(Creator, title(Title)), |
852 | 906 | print_message(banner, thread_welcome), |
|
0 commit comments