Swift is a really fun language, but it’s easy to make it hard on developers with unclear error messages or output. So, I thought I’d write a blog post aimed at Swift developers to help demystify the language’s output, and help boost your enjoyment of the language.
Have you ever tried to run a console application in Swift on MacOS? If so, you might have experienced the pain of having to copy and paste quite a bit of code just to run a single function that you need to test.
Rainbow
Rainbow adds the text color, background color and style for console and command line output in Swift. It was born for cross-platform software registration in terminals and works on both Apple and Linux platforms.
Basic usage
A convenient way to produce a colored string using the string extension.
Colour and style called
Importing the rainbow print(Text red.red) print(Blue background.onBlue) print(Light green text on white background.lightGreen.onWhite) print(underline.underline) print(cyan with bold and flashing.cyan.bold.flashing) print(Plain text.red.onYellow.bold.clearColor.clearBackgroundColor.clearStyles) The result is something like this:
Setting
Swift Package Manager
If you develop cross-platform software in Swift, the Swift Package Manager may be your choice for package management. Just add the url of this repository as a dependency in your Package.swift file: Import Package Description let package = package( name : YourAwesomeSoftware, dependencies : [ .package(url : https://github.com/onevcat/Rainbow, .upToNextMajor(from : 4.0.0)) ], targets : [ .target( name : MyApp, dependencies : [Rainbow] ) ] ) Then start the Swift compilation when you’re ready. For more information on using the Swift package manager, visit the official Apple website.
other uses
Line interpolation and interleaving
Swift string interpolation is supported. Set the color of a part of the chain. Or even make interlocking multicolored necklaces. The internal colour style is retained: print(接天莲叶(无穷碧.green),映日荷花(别样红.red)) print((两只黄鹂.yellow)鸣翠柳,一行白鹭(上青天.lightBlue)。.lightGreen.underline)
ANSI 256 colour mode
Full 8-bit color support for text and background color: print(停车坐爱(枫林晚.bit8(31)),(霜叶.bit8(160))红于(二月花.bit8(198))。) print((一道残阳.bit8(202))水中,(半江瑟瑟.bit8(30).onBit8(226))半江红。)
Hexagonal colours (approx.)
It also accepts hexagonal color. Rainbow tries to convert it to a .bit8 color as close as possible: print(黑云压城(城欲.hex(#666)),甲光向日(金鳞开.hex(000000).onHex(#E6B422))。 print(日出江花(红胜火.hex(0xd11a2d)),春来江水(绿如蓝.hex(0x10aec2)) Acceptable size: FFF, #FFFF, #FFFF, 0xFFFF
True Color
Some terminal emulators support 24-bit true color. If you are sure that 24-bit colors can be displayed in your user’s terminal, there is no reason for Rainbow to reject them! print(疏影横斜(水清浅.bit24(36,116,181)),暗香浮动(月黄昏.bit24(254,215,26)) print((春色满园.hex(#ea517f, to: .bit24))关不住,(一枝红杏.hex(#f43e06, to: .bit24))出墙来。)
First destination
By default, Rainbow should be smart enough to recognize the destination of the output and determine if it is a tty. For example, automatically produces plain text when writing to a : // main.swift print(Hello Rainbow.red) .build/debug/RainbowDemo > output.txt // output.txt Hello Rainbow This is useful if you use the same code to write to the console and the log file. You can also change this behavior manually:
- Set the Rainbow.outputTarget yourself.
- Enter the environment value NO_COLOR if your application is active.
- Or set Rainbow.enabled to false.
Verbose Way
You can use a more detailed method if you wish: import Rainbow let output = The brown fox jumps over the lazy dog .applyingCodes(Color.red, BackgroundColor.yellow, Style.bold) print(output) // Red text on yellow, bold of course 🙂 Or even build everything from scratch: let entry = Rainbow.Entry( segments : [ .init(text : Hello , color : .named(.magenta)), .init(text : Rainbow, color : .bit8(214), backgroundColor : .named(.lightBlue), styles : [.underline]), ] ) print(Rainbow.generateString(for : entry)) Remember that the string extension (e.g. hello.red) is O(n). So, if you are working with a huge string or a very complex nesting, you may get performance issues or difficulties with threading operations. The manual method is the salvation for these cases.
Motivation and compatibility
With Swift Open Source, developers can now write cross-platform programs in the same language. And I think command line programs will be the next big platform for Swift. Colorful and clear outputs always help us understand what is going on. It is really an indispensable tool for creating great software. Rainbow should work well on OS X and Linux terminals. It is smart enough for to check whether or not the output is connected to a valid text terminal, to decide to change the protocol or not. This can be useful if you want to send the log to a file instead of the console.
GitHub
https://github.com/onevcat/Rainbow
Related Tags:
swift console logswift logger examplekingfisher swiftcocoalumberjack/swift log levelswiftytexttableswift log to file,People also search for,Privacy settings,How Search works,swift console log,swift logger example,kingfisher swift,cocoalumberjack/swift log level,swiftytexttable,swift log to file,swift terminal colors,tinyconsole