1

Getting started with Pascal here. I wanted to write a simple program that first clears the terminal window and then reads user input. The first result of searching for clearing the screen showed the ClrScr procedure. Using it gets the job done but ClrScr needs Crt which results in a new problem. Terminating the program using Ctrl + C does not work. Searching online again, I found that Crt takes over I/O. I have been looking for alternative to ClrScr but haven't really found anything so far.

So how can I clear terminal while still being able to terminate the program using Ctrl + C. Also how can I terminate the program in the current case using Crt?

Current Code:

    program Test;
    uses Crt;
    var     
            x : integer;
    begin
            read(x);
    end.
  • What operating system are you using? Linux and Windows have a command to clear the screen, so you could make a system call to that command. The other option, while using `Crt`, is to catch `Ctrl+C` by reading it as a key and acting on it explicitly. – lurker Sep 01 '17 at 21:34
  • @lurker macOS sierra –  Sep 02 '17 at 02:14
  • You will have to explicitly read `^C` as a key (using `KeyPressed`, `ReadKey`) at appropriate places and react to it. On Windows, there is a [`SysSetCtrlBreakHandler`](https://www.freepascal.org/docs-html/rtl/system/syssetctrlbreakhandler.html), but I don't know if that exists on other platforms. – Rudy Velthuis Sep 02 '17 at 11:44
  • @RudyVelthuis So far, reading `^C` seems the only way to do it and something that I don't feel like implementing. Might just discard `Crt` from the code totally. –  Sep 02 '17 at 12:08
  • Yes, discarding Crt is not such a problem. If you need coloured text or something like ClrScr, simply use the appropriate OS routines, or use escape sequences (IIRC, [that should work on the Mac too](https://stackoverflow.com/questions/25879183/can-terminal-app-be-made-to-respect-ansi-escape-codes)). – Rudy Velthuis Sep 02 '17 at 12:19
  • I sometimes used to write enough blank lines to the screen to make the text scroll off. – Stuart Sep 03 '17 at 17:04
  • @Stuart Might work but will also need to edit cursor position which can be troublesome at times. –  Sep 04 '17 at 02:45

1 Answers1

1

So far the solutions I have seen online and the ones suggested in the comments for keeping CRT just make it troublesome and unnecessary makes the program complex. So for now I have decided just to discard it totally.

The workaround I have found now is to use unix.fpsystem with the clear command which gets the job done just fine.