我想学习如何编写一个prolog程序来控制另一个命令行程序。
作为一个具体的例子,我想控制mps-youtube(https://github.com/mps-youtube/mps-youtube)。 首先,我需要一种能够读取屏幕菜单的循环以及输入命令来控制程序的能力。这是我到目前为止写的:
youtube :-
setup_call_cleanup(
process_create(path(mpsyt), [],
[ stdout(pipe(Out)),stdin(pipe(In))
]),
( read_lines(Out, Lines),
maplist(writeln,Lines),
my_command_loop(In,Out)),
close(Out)).
read_lines(Out, Lines) :-
read_line_to_codes(Out, Line1),
read_lines(Line1, Out, Lines).
read_lines(end_of_file, _, []) :- !.
read_lines(Codes, Out, [Line|Lines]) :-
atom_codes(Line, Codes),
read_line_to_codes(Out, Line2),
read_lines(Line2, Out, Lines).
my_command_loop(In,Out):-
writeln("Enter a command:"),
read(command(Command)),
dif(Command,stop),
writeln(Command),
writeln(In,Command),
read_lines(Out,Lines),
maplist(writeln,Lines),
my_command_loop(In,Out).
my_command_loop(_,_):-
true.
我想与之互动,例如输入command('/ oasis')
后跟command(1).
,最后command(stop).
如果我尝试运行此操作,我会收到以下错误:
Traceback (most recent call last):
File "/usr/bin/mpsyt", line 9, in <module>
load_entry_point('mps-youtube==0.2.5', 'console_scripts', 'mpsyt')()
File "/usr/lib/python3/dist-packages/mps_youtube/main.py", line 4696, in main
set_window_title("mpsyt")
File "/usr/lib/python3/dist-packages/mps_youtube/main.py", line 152, in set_window_title
sys.stdout.write(xenc('\x1b]2;' + title + '\x07'))
TypeError: write() argument must be str, not bytes
这似乎暗示python程序mps-youtube未在process_create中正确加载。这个想法有可能吗?实现这个的好方法是什么?