ColorizeSwift

repository·master·Indexed 43 days ago

https://github.com/mtynior/colorizeswift

A Swift library for adding terminal string styling, including standard and 256-color foreground and background colors, as well as text modifiers like bold and italic, using a fluent API.

Tokens
1.2K
Snippets
4
Records
10
Agent score
5%

What's inside ColorizeSwift

  1. Use ColorizeSwift for terminal string styling

    master

    ColorizeSwift provides a fluent API to apply styles, foreground colors, and background colors to String instances. You can chain modifiers and colors to create complex styled strings.

    print("Bold".bold())
    print("Red".red())
    print("On yellow".onYellow())
    print("256 foreground".foregroundColor(.orange1))
    print("226 background".backgroundColor(.orange1))
    print("Awful combination".colorize(.yellow, background: .red))
    
    let nested = "with a blue substring".blue().underline()
    print("A bold, green line \(nested) that becomes bold and green again".green().bold())
  2. Install ColorizeSwift via Swift Package Manager

    master

    Add ColorizeSwift as a dependency in your Package.swift file to integrate it into your Swift project using SPM.

    // swift-tools-version:4.0
    import PackageDescription
    
    let package = Package(
        name: "MyLibrary",
        products: [
            .library(name: "MyLibrary", targets: ["MyLibrary"]),
        ],
        dependencies: [
             .package(url: "https://github.com/mtynior/ColorizeSwift.git", from: "1.5.0"),
        ],
        targets: [
            .target(name: "MyLibrary", dependencies: ["ColorizeSwift"]),
            .testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary", "ColorizeSwift"]),
        ]
    )
  3. Remove colorization from a string

    master

    Use the uncolorized() method to strip all ANSI escape codes from a styled string, returning the plain text.

    let styledString = "Awful combination".colorize(.yellow, background: .red)
    let withoutStyles = styledString.uncolorized() // "Awful combination"
  4. Disable colorization globally

    master

    You can disable colorization for all strings by setting the static property String.isColorizationEnabled to false. The default value is true.

    Common use cases:

    Via command line flag:

    String.isColorizationEnabled = !CommandLine.arguments.contains("--no-color")

    Detecting if output is a TTY (to avoid color in pipes):

    String.isColorizationEnabled = (isatty(fileno(stdout)) == 1)
  5. Access raw ANSI escape codes

    master

    If you need to manually manage escape sequences, you can access the open and close codes for styles and colors.

    For Modifiers: Use the TerminalStyle enum.

    For 256 Colors: Use the TerminalColor enum's style code methods.

    // Modifiers
    TerminalStyle.bold.open // "\u{001B}[1m"
    TerminalStyle.bold.close  // "\u{001B}[22m"
    
    // 256 Colors
    TerminalColor.red.foregroundStyleCode().open //"\u{001B}[38;5;9m"
    TerminalColor.red.backgroundStyleCode().open //"\u{001B}[48;5;9m"
  6. Use 256-colors with TerminalColor

    master

    For more granular color control, use the TerminalColor enumeration with the following methods:

    • foregroundColor(color: TerminalColor)
    • backgroundColor(color: TerminalColor)
    • colorize(foreground: TerminalColor, background: TerminalColor)
  7. Reference: Standard Foreground Colors

    master

    The following methods can be called on a String to set the foreground color:

    • black()
    • red()
    • green()
    • yellow()
    • blue()
    • magenta()
    • cyan()
    • lightGray()
    • darkGray()
    • lightRed()
    • lightGreen()
    • lightYellow()
    • lightBlue()
    • lightMagenta()
    • lightCyan()
    • white()
  8. Reference: Standard Background Colors

    master

    The following methods can be called on a String to set the background color:

    • onBlack()
    • onRed()
    • onGreen()
    • onYellow()
    • onBlue()
    • onMagenta()
    • onCyan()
    • onLightGray()
    • onDarkGray()
    • onLightRed()
    • onLightGreen()
    • onLightYellow()
    • onLightBlue()
    • onLightMagenta()
    • onLightCyan()
    • onWhite()