On the command line, fill_buffer/1 waits for a terminating newline:
?- sleep(4),fill_buffer(user_input). hello|: hello true.
The `hello\n` is then hanging in the input buffer and is read by the toplevel on the next occasion:
?- ls. ERROR: Syntax error: Operator expected ERROR: hello ERROR: ** here ** ERROR: ls .
To consume the data in the input buffer, use read_pending_codes/3:
Here a newline is needed to push data into the stream:
?- sleep(4),(fill_buffer(user_input),read_pending_codes(user_input,Codes,Tail)). hello world |: hello world Codes = [104,101,108,108,111,32,119,111,114,108,100,10|Tail].
But if you read "raw" you don't need a final newline, although the goal still blocks if there is no input (how to make it return at once?)
?- sleep(4),with_tty_raw((fill_buffer(user_input),read_pending_codes(user_input,Codes,Tail))). hello world Codes = [104,101,108,108,111,32,119,111,114,108,100|Tail].