Timed single character input in SWI-Prolog

14 views
Skip to first unread message

Jamie Moschella

unread,
Apr 8, 2018, 7:11:16 PM4/8/18
to SWI-Prolog
Hi everyone! I posted this quick question to Stack Overflow about a week ago, but someone suggested that I might get a more helpful answer here. I'm trying to make a simple timed menu. The user should be able to hit a key to select an option, or if they press nothing it will automatically select the default option after a few seconds. I wrote this to test the character input:

char_time:- catch(call_with_time_limit(5, get_single_char(X)), time_limit_exceeded, writeln('Too late!')),
           
(ground(X) -> (char_code(Y, X), write('You picked '), writeln(Y)); true).
When I call ?- char_time. and enter a character, it behaves as expected. If I don't enter a character, it prints "Too late!" and returns true as it should, but then I get the error message:
ERROR: '$raw_read'/2: I/O error in read on stream user_input (Interrupted system call)
ERROR
: I/O error in read on stream user_input (Interrupted system call)
ERROR
: In:
ERROR
: [7] throw(error(io_error(read,user_input),context(...,'Interrupted system call')))
Is there any way around this problem? I thought of replacing get_single_char/1 with something else, but most other predicates similar to it wait for you to press Enter, and I'd prefer the menu to be operated with a single key press. I also tried enclosing the whole thing in an additional catch/3 predicate, leaving the second argument as _ so it should theoretically catch any error, but that didn't solve the problem.
Reply all
Reply to author
Forward
0 new messages