Xcode / terminal check

In a console app I am using this command that clears terminal window:

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

This works in the real terminal (at least the one I'm using now, set to vt100) but not in Xcode console where it prints the symbols verbatim. I want to have a check that tells if it's safe to clear the terminal (so that the print command either clears the terminal or at least does nothing without printing the gibberish out). What's the proper way to do that check?

A simple approach would be to check for the TERM environment variable. In Swift that would be

if ProcessInfo.processInfo.environment["TERM"] != nil {
    print("\u{001B}[2J")
}

But terminals do not all support the the same escape codes, so things might be more difficult.

2 Likes

Thank you. I was going to pass some custom environment variable – so when running in Xcode it is there and when running out of Xcode it is not available – but your suggestion using existing environment variable looks cleaner. With that I can even test the specific terminal type if needed.

But terminals do not all support the the same escape codes

The traditional way of dealing with this on Unix-y systems is via termcap. I don’t have any direct experience with this myself but that’ll make a good search term (hey hey).

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple