
% invoke the menu system using the command:
%     menu.

% command(Cmd)
% ------------
% gets user command as a character and
%    translates into a supported command,
%    skips assumed whitespace char after each command
command(Cmd) :- get_char(C), get_char(_), command(C, Cmd), !.
command(C, help) :- C = 'H'; C = 'h'.
command(C, quit) :- C = 'Q'; C = 'q'.
command(C, print) :- C = 'P'; C = 'p'.
command(_, invalid).

% writeMsg(Text)
% --------------
% assuming Text is list of ascii codes, prints as characters
writeMsg([]).
writeMsg([H|T]) :- put(H), writeMsg(T).

% process(Command)
% ----------------
% does whatever the given command is supposed to do
process(invalid) :- nl, writeMsg("Invalid command"),nl.
process(quit) :- nl, writeMsg("BYE!"), nl.
process(help) :- nl, writeMsg("HELP: Available commands are H (help), Q (quit), P (print)"), nl.
process(print):- nl, writeMsg("PRINT: This is a print!"), nl.

% menu.
% -----
% repeatedly displays the user menu, 
%    allows them to enter a single-character command,
%    validates and processes the command
% ends when the user selects the quit command
menu :- 
    repeat,
        nl, writeMsg("Enter a command (H,Q,P):"),
        command(Cmd),
        process(Cmd),
        Cmd == quit.

