talira

Rainbow (Swift)

Text colour, background colour and styles for terminal output in Swift

open sourceMITfree

Rainbow is a Swift library by Wei Wang that adds text colour, background colour and styles to console output, on Apple platforms and Linux. A String extension gives chainable properties such as .red, .onBlue and .bold, plus ANSI 256-colour, 24-bit true colour, hex and HSL colours, and nesting inside string interpolation. It writes plain text when output isn't a terminal (a log file, say), honours NO_COLOR and FORCE_COLOR, and has a builder and batch styling for hot paths. It isn't the Ruby gem of the same name.

Problems it solves

  • colour my Swift command-line tool's output on macOS and Linux
  • coloured log lines in the terminal but plain text when my Swift tool writes to a file
  • bold and coloured text in a Swift CLI without writing ANSI escape codes by hand

Features

  • chainable colour and style properties on String
  • ANSI 256-colour, 24-bit, hex and HSL colours
  • plain output when not writing to a terminal
  • NO_COLOR and FORCE_COLOR support
  • builder pattern and batch styling for performance