I am writing a BASIC Interpreter for the Command Line in Swift 2, and I cannot find a way to implement the simple command, CLS (clear all text from the Terminal.) Should I simply print spaces in a loop, or is there a function I'm not aware of that would clear the Terminal screen?
You can use Ctrl+L keyboard shortcut in Linux to clear the screen. It works in most terminal emulators.
You can use the following ANSI sequence:
print("\u{001B}[2J")
... where \u{001B}
is ESCAPE
and [2J
is clear screen.
This answer applies to Swift 2.1 or earlier only
To elaborate on Arc676's answer:
The system
command is imported into Swift via the Darwin module on Mac platforms (with other C APIs). On Linux, Glibc replaces Darwin for bridging low-level C APIs to Swift.
import Glibc
// ...
system("clear")
Or, if the system
call is ambiguous, explicitly call Glibc's system
(or Darwin on Mac platforms):
import Glibc
// ...
Glibc.system("clear")
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