Color Ouput with Swift Command Line Tool

Color ouput with Swift command line tool

Swift has built in unicode support. This invalidates using of back slash. So that I use color codes with "\u{}" syntax. Here is a println code which works perfectly on terminal.

// \u{001B}[\(attribute code like bold, dim, normal);\(color code)m

// Color codes
// black 30
// red 31
// green 32
// yellow 33
// blue 34
// magenta 35
// cyan 36
// white 37

println("\u{001B}[0;33myellow")

Hope it helps.

print() to console log with color

Nowadays, Xcode debugging console doesn't support coloring.

Update current line with command line tool in Swift

Your example will only work on the actual command line, not in the debugger console. And you also need to flush stdout for every iteration, like this:

var logString = String(format: "%2i%% %.2fM \r", 10, 5)
print(logString)
fflush(__stdoutp)

Replace printed out text

If the output goes to the Terminal then you can use the fact that
\r (Carriage Return) moves the "cursor" to the start of the current
line, without advancing to the next line:

print("10% done ", terminator: "\r")
print("20% done ", terminator: "\r")
print("100% done")

(But note that this does not work in the Xcode debugger console.)



Related Topics



Leave a reply



Submit