7

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?

JAL
  • 41,701
  • 23
  • 172
  • 300
Brandon Bradley
  • 3,160
  • 4
  • 22
  • 39

5 Answers5

14

You can use the following ANSI sequence:

print("\u{001B}[2J")

... where \u{001B} is ESCAPE and [2J is clear screen.

Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118
  • Only answer that worked for me. MacBook Pro (Retina, 15-inch, Mid 2014) OS X El Capitan. – Mark Oct 01 '16 at 21:25
  • This works because `system` is unavailable in Swift 3. I'll see what I can do to update my answer to use `posix_spawn` APIs or `NSTask`. – JAL Nov 29 '16 at 16:48
  • 8
    Any idea why when I use this it prints `[2J` instead of clearing? – Ben Ong Jan 13 '17 at 10:29
  • 3
    @BenOng I've discovered that this does work in a standard terminal window, but prints "[2J" if you are using the console built into Xcode. The Xcode terminal is also unable to print colors properly, so I'm guessing it's just a general ANSI interpretation problem when running your console app from the IDE. Try archiving your build and running it standalone. – bitwit Feb 16 '19 at 20:55
  • 1
  • Any idea how to go to first line as well? this clears the screen but new prints appear in the middle of terminal while the top is empty – Swiftly Mar 31 '23 at 01:18
2

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")
Community
  • 1
  • 1
JAL
  • 41,701
  • 23
  • 172
  • 300
  • 3
    With Swift 3 I am getting `"system" is unavailable in Swift: Use posix_spawn APIs or NSTask instead` – Chris Apr 27 '17 at 18:32
1

This code makes a synchronous call to the built-in clear command. This won't cause problem with readLine() since it prints the escape sequence returned by clear using Swift's print() function

var cls = Process()
var out = Pipe()
cls.launchPath = "/usr/bin/clear"
cls.standardOutput = out
cls.launch()
cls.waitUntilExit()
        print (String(data: out.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.utf8) ?? "")
Alessandro Mascolo
  • 133
  • 1
  • 3
  • 13
  • Just to be clear, this works in terminal. But if you try it to clear console screen inside Xcode, it gives `TERM environment variable not set.` – Anton Malmygin Nov 07 '18 at 15:54
0

Use the built-in clear command either with system

system("clear")

or popen (ask Google)

Alternatively, simulate the pressing of Ctrl+L using AppleScript via the command line:

osascript -e 'tell app "terminal" to tell app "system events" to keystroke "l" using {control down}'

EDIT: system is no longer available in newer verions of Swift. See Rudolf Adamkovič's answer.

Arc676
  • 4,445
  • 3
  • 28
  • 44
0

This works for me in Swift 3.1

var clearScreen = Process()
clearScreen.launchPath = "/usr/bin/clear"
clearScreen.arguments = []
clearScreen.launch()
clearScreen.waitUntilExit()

You can create a function with a callback like this

func clearScreen(completion:@escaping (Bool) -> () ) {
        let clearScreen = Process()
        clearScreen.launchPath = "/usr/bin/clear"
        clearScreen.arguments = []
        clearScreen.terminationHandler = { task in completion(true) }
        clearScreen.launch()
        clearScreen.waitUntilExit()
}
Alessandro Mascolo
  • 133
  • 1
  • 3
  • 13
Chris
  • 5,485
  • 15
  • 68
  • 130