How to clear the Terminal screen in Swift?

前端 未结 5 1736
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 19:02

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 s

5条回答
  •  生来不讨喜
    2020-12-11 19:31

    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.

提交回复
热议问题