Other Prompted Input -- library(prompt)

library(prompt) defines several commands for reading prompted input from the terminal. In fact, library(ask) is built on top of this package.


prompt(+Prompt)
is used by all the commands in library(ask) to print the prompt or question. You may find it useful in constructing your prompted input commands. If Prompt is any term except a list, it is written to the terminal using write/1; if Prompt is a list of terms, each element of the list is written to the terminal using write/1, with no additional layout or punctuation. After writing Prompt to the terminal, the terminal output is flushed, so that Prompt will appear on the terminal before the user has to type an answer. prompt/1 ensures that Prompt always starts at the beginning of a line.
prompted_char(+Prompt, -Char)
writes Prompt to the terminal, and reads a line of characters from it. The first of these characters is returned as Char; the rest are discarded. The original case of the character is preserved. Note that Char might be a newline character or the end-of-file character.
prompted_line(+Prompt, -Chars)

prompted_line(+Prompt, -Chars, -Terminator)
These predicates write Prompt to the terminal, regardless of the current output stream; and read a list of character codes from the terminal, regardless of the current input stream. Prompt is written using write/1; it is normally a single atom, and should never be a list of character codes. prompted_line/3 also returns the end-of-line character, while prompted_line/2 simply checks that the end-of-line character is not end-of-file. When you want to ask the user of your program a question, use prompted_line/[2,3] instead of changing I/O streams yourself, or using ttyget0/1 and its associates. In order to ask the user of your program "Do you really want to stop?" and stop if the user says yes or anything else beginning with a lowercase y, simply write
          conditional_halt :-
             prompted_line('Do you really want to stop? ',
                            [0'y|_]),
             halt.
          

You can use prompted_line/[2,3] without worrying what the current I/O streams are, or whether you need to call ttyflush/0 or not. Also, as with get_line/[1,2], an input line ends with the line terminator character. The user does not have to end prompted_line/[2,3] input with a ., just with <RET>.