12

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?

false
  • 10,264
  • 13
  • 101
  • 209
Ishan
  • 3,931
  • 11
  • 37
  • 59

1 Answers1

23

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').
Community
  • 1
  • 1
Orbling
  • 20,413
  • 3
  • 53
  • 64
  • 4
    Nice catch. I forgot about ANSI sequences. You can express ESC like \e, and thus store in SWI-Prolog configuration file the procedure: `cls :- write('\e[2J').` or `cls :- write('\e[H\e[2J').` – CapelliC Jun 09 '13 at 09:02
  • @CapelliC: Useful, I did not know it supported `\e`, a much cleaner refinement. – Orbling Jun 10 '13 at 13:45
  • @CapelliC: `\e` is not ISO and is rejected by GNU and SICStus. Use `\33\ ` instead. – false Dec 31 '14 at 16:42