1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
//===--- AnsiColor.swift - ANSI formatting control codes ------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Provides ANSI support via Swift string interpolation.
//
//===----------------------------------------------------------------------===//
enum AnsiColor {
case normal
case black
case red
case green
case yellow
case blue
case magenta
case cyan
case white
case gray
case brightRed
case brightGreen
case brightYellow
case brightBlue
case brightMagenta
case brightCyan
case brightWhite
case rgb(r: Int, g: Int, b: Int)
case grayscale(Int)
var foregroundCode: String {
switch self {
case .normal: return "39"
case .black: return "30"
case .red: return "31"
case .green: return "32"
case .yellow: return "33"
case .blue: return "34"
case .cyan: return "35"
case .magenta: return "36"
case .white: return "37"
case .gray: return "90"
case .brightRed: return "91"
case .brightGreen: return "92"
case .brightYellow: return "93"
case .brightBlue: return "94"
case .brightCyan: return "95"
case .brightMagenta: return "96"
case .brightWhite: return "97"
case let .rgb(r, g, b):
let ndx = 16 + 36 * r + 6 * g + b
return "38;5;\(ndx)"
case let .grayscale(g):
let ndx = 232 + g
return "38;5;\(ndx)"
}
}
var backgroundCode: String {
switch self {
case .normal: return "49"
case .black: return "40"
case .red: return "41"
case .green: return "42"
case .yellow: return "43"
case .blue: return "44"
case .cyan: return "45"
case .magenta: return "46"
case .white: return "47"
case .gray: return "100"
case .brightRed: return "101"
case .brightGreen: return "102"
case .brightYellow: return "103"
case .brightBlue: return "104"
case .brightCyan: return "105"
case .brightMagenta: return "106"
case .brightWhite: return "107"
case let .rgb(r, g, b):
let ndx = 16 + 36 * r + 6 * g + b
return "48;5;\(ndx)"
case let .grayscale(g):
let ndx = 232 + g
return "48;5;\(ndx)"
}
}
}
enum AnsiWeight {
case normal
case bold
case faint
var code: String {
switch self {
case .normal: return "22"
case .bold: return "1"
case .faint: return "2"
}
}
}
enum AnsiAttribute {
case fg(AnsiColor)
case bg(AnsiColor)
case weight(AnsiWeight)
case inverse(Bool)
}
extension DefaultStringInterpolation {
mutating func appendInterpolation(ansi attrs: AnsiAttribute...) {
var code = "\u{1b}["
var first = true
for attr in attrs {
if first {
first = false
} else {
code += ";"
}
switch attr {
case let .fg(color):
code += color.foregroundCode
case let .bg(color):
code += color.backgroundCode
case let .weight(weight):
code += weight.code
case let .inverse(enabled):
if enabled {
code += "7"
} else {
code += "27"
}
}
}
code += "m"
appendInterpolation(code)
}
mutating func appendInterpolation(fg: AnsiColor) {
return appendInterpolation(ansi: .fg(fg))
}
mutating func appendInterpolation(bg: AnsiColor) {
return appendInterpolation(ansi: .bg(bg))
}
mutating func appendInterpolation(weight: AnsiWeight) {
return appendInterpolation(ansi: .weight(weight))
}
mutating func appendInterpolation(inverse: Bool) {
return appendInterpolation(ansi: .inverse(inverse))
}
}
|