How to clear the Terminal screen in Swift?

前端 未结 5 1735
佛祖请我去吃肉
佛祖请我去吃肉 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:24

    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) ?? "")
    

提交回复
热议问题