CommandLineToolkit
96 строк · 2.5 Кб
1enum ConsoleAttribute: Int, Equatable {
2case normal = 0
3case bold = 1
4case dim = 2
5case italic = 3
6case underline = 4
7case blink = 5
8case overline = 6
9case inverse = 7
10case hidden = 8
11case strike = 9
12case noBold = 21
13case noDim = 22
14case noItalic = 23
15case noUnderline = 24
16case noBlink = 25
17case noOverline = 26
18case noInverse = 27
19case noHidden = 28
20case noStrike = 29
21}
22
23/// Representation of a style for outputting to a Console in different colors with differing attributes.
24/// A few suggested default styles are provided.
25struct ConsoleStyle: Equatable {
26/// Optional text color. If `nil`, text is plain.
27let color: ConsoleColor?
28
29/// Optional background color. If `nil` background is plain.
30let background: ConsoleColor?
31
32/// If `true`, text is bold.
33let attributes: [ConsoleAttribute]
34
35/// Creates a new `ConsoleStyle`.
36init(
37color: ConsoleColor? = nil,
38background: ConsoleColor? = nil,
39attributes: [ConsoleAttribute] = []
40) {
41self.color = color
42self.background = background
43self.attributes = attributes
44}
45}
46
47extension ConsoleStyle {
48/// Plain text with no color or background.
49static let plain: ConsoleStyle = .init()
50
51static let progressBarProgress: ConsoleStyle = .init(
52color: .palette(28)
53)
54
55static let progressBarUnfinished: ConsoleStyle = .init(
56color: .palette(8)
57)
58
59/// Green text with no background.
60static let success: ConsoleStyle = .init(
61color: .palette(28),
62attributes: [.bold]
63)
64
65/// Light blue text with no background.
66static var info: ConsoleStyle = .init(
67color: .palette(36)
68)
69
70/// Light blue text with no background.
71static var notice: ConsoleStyle = .init(
72color: .palette(36),
73attributes: [.bold]
74)
75
76/// Yellow text with no background.
77static var warning: ConsoleStyle = .init(
78color: .palette(214),
79attributes: [.bold]
80)
81
82/// Red text with no background.
83static var error: ConsoleStyle = .init(
84color: .palette(9/*196*/),
85attributes: [.bold]
86)
87
88static let headerTitle: ConsoleStyle = .init(
89attributes: [.bold]
90)
91
92static var help: ConsoleStyle = .init(
93color: .palette(180),
94attributes: [.italic]
95)
96}
97