How to write a prolog program to control another command line program?

Viewed 122

I would like to learn how to write a prolog program to control another command line program.

As a concrete example say I want to control mps-youtube (https://github.com/mps-youtube/mps-youtube). To begin with I need to have some kind of loop that reads the on screen menus and an ability to enter commands to control the program. This is what I wrote so far:

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.

The idea being that I would interact with this by for example entering command('/ oasis') followed by command(1). and finally command(stop).

If I try and run this I get the following errors:

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

which seems to suggest that the python program mps-youtube is not loading correctly in the process_create. Is this idea possible? What is a good way to implement this?

0 Answers
Related