When you are running command prompt in windows you can type the clear
command to clear the screen. How do you do the same thing when you are running swipl prolog (by typing swipl
in the command prompt) in windows?
On UNIX terminals, you can simply use the built-in tty_clear predicate to clear the console.
Open a terminal (Ctrl+Alt+T) and navigate to the directory where you stored your program. Open SWI-Prolog by invoking swipl . In SWI-Prolog, type [program] to load the program, i.e. the file name in brackets, but without the ending. In order to query the loaded program, type goals and watch the output.
If you want to exit SWI-Prolog, issue the command halt., or simply type CTRL-d at the SWI-Prolog prompt.
To get the current working directory use working_directory(CWD, CWD) . To change the current working directory use working_directory(_, NewCWD) .
On unix terminals, there is the library(tty)
resource and that has tty_clear/0
, but windows terminals do not support the terminal library. However, they do support ANSI Escape Codes.
Escape codes are character sequences starting with the ESC
(escape) character, ASCII 0x1B = 27
. Most start with the control sequence introducer, which is the escape followed by a left bracker: ESC [
, known as the CSI
.
So you can issue the code sequence for a screen clear, which is the ED (Erase Data) command, which takes the form:
CSI 2 J -- which expands to: ESC [ 2 J
From SWI-Prolog this can be issued using the format/2
primitive.
format('~c~s', [0x1b, "[2J"]). % issue CSI 2 J
The ED 2 command, full terminal clear, on the MSDOS ANSI handling resets the cursor to top-left, but that is not necessarily the case on all terminals, so best to combine with the CUP (Cursor Position) command, which as a reset to home is simply: CSI H
.
format('~c~s~c~s', [0x1b, "[H", 0x1b, "[2J"]). % issue CSI H CSI 2 J
Update: Simplification
Thanks to @CapelliC for an alternate and clearer form, using the \e
escape code for escape!
Plain clear screen:
cls :- write('\e[2J').
Or with home reset:
cls :- write('\e[H\e[2J').
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With