-
Notifications
You must be signed in to change notification settings - Fork 123
/
ProgressAnimation.swift
302 lines (246 loc) · 9.3 KB
/
ProgressAnimation.swift
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
This source file is part of the Swift.org open source project
Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import TSCBasic
/// A protocol to operate on terminal based progress animations.
public protocol ProgressAnimationProtocol {
/// Update the animation with a new step.
/// - Parameters:
/// - step: The index of the operation's current step.
/// - total: The total number of steps before the operation is complete.
/// - text: The description of the current step.
func update(step: Int, total: Int, text: String)
/// Complete the animation.
/// - Parameters:
/// - success: Defines if the operation the animation represents was succesful.
func complete(success: Bool)
/// Clear the animation.
func clear()
}
/// A single line percent-based progress animation.
public final class SingleLinePercentProgressAnimation: ProgressAnimationProtocol {
private let stream: OutputByteStream
private let header: String?
private var displayedPercentages: Set<Int> = []
private var hasDisplayedHeader = false
init(stream: OutputByteStream, header: String?) {
self.stream = stream
self.header = header
}
public func update(step: Int, total: Int, text: String) {
if let header = header, !hasDisplayedHeader {
stream <<< header
stream <<< "\n"
stream.flush()
hasDisplayedHeader = true
}
let percentage = step * 100 / total
let roundedPercentage = Int(Double(percentage / 10).rounded(.down)) * 10
if percentage != 100, !displayedPercentages.contains(roundedPercentage) {
stream <<< String(roundedPercentage) <<< ".. "
displayedPercentages.insert(roundedPercentage)
}
stream.flush()
}
public func complete(success: Bool) {
if success {
stream <<< "OK"
stream.flush()
}
}
public func clear() {
}
}
/// A multi-line ninja-like progress animation.
public final class MultiLineNinjaProgressAnimation: ProgressAnimationProtocol {
private struct Info: Equatable {
let step: Int
let total: Int
let text: String
}
private let stream: OutputByteStream
private var lastDisplayedText: String? = nil
public init(stream: OutputByteStream) {
self.stream = stream
}
public func update(step: Int, total: Int, text: String) {
assert(step <= total)
guard text != lastDisplayedText else { return }
stream <<< "[\(step)/\(total)] " <<< text
stream <<< "\n"
stream.flush()
lastDisplayedText = text
}
public func complete(success: Bool) {
}
public func clear() {
}
}
/// A redrawing ninja-like progress animation.
public final class RedrawingNinjaProgressAnimation: ProgressAnimationProtocol {
private let terminal: TerminalController
private var hasDisplayedProgress = false
init(terminal: TerminalController) {
self.terminal = terminal
}
public func update(step: Int, total: Int, text: String) {
assert(step <= total)
terminal.clearLine()
let progressText = "[\(step)/\(total)] \(text)"
if progressText.utf8.count > terminal.width {
let suffix = "…"
terminal.write(String(progressText.prefix(terminal.width - suffix.utf8.count)))
terminal.write(suffix)
} else {
terminal.write(progressText)
}
hasDisplayedProgress = true
}
public func complete(success: Bool) {
if hasDisplayedProgress {
terminal.endLine()
}
}
public func clear() {
terminal.clearLine()
}
}
/// A ninja-like progress animation that adapts to the provided output stream.
public final class NinjaProgressAnimation: DynamicProgressAnimation {
public init(stream: OutputByteStream) {
super.init(
stream: stream,
ttyTerminalAnimationFactory: { RedrawingNinjaProgressAnimation(terminal: $0) },
dumbTerminalAnimationFactory: { SingleLinePercentProgressAnimation(stream: stream, header: nil) },
defaultAnimationFactory: { MultiLineNinjaProgressAnimation(stream: stream) })
}
}
/// A multi-line percent-based progress animation.
public final class MultiLinePercentProgressAnimation: ProgressAnimationProtocol {
private struct Info: Equatable {
let percentage: Int
let text: String
}
private let stream: OutputByteStream
private let header: String
private var hasDisplayedHeader = false
private var lastDisplayedText: String? = nil
init(stream: OutputByteStream, header: String) {
self.stream = stream
self.header = header
}
public func update(step: Int, total: Int, text: String) {
assert(step <= total)
if !hasDisplayedHeader {
stream <<< header
stream <<< "\n"
stream.flush()
hasDisplayedHeader = true
}
let percentage = step * 100 / total
stream <<< "\(percentage)%: " <<< text
stream <<< "\n"
stream.flush()
lastDisplayedText = text
}
public func complete(success: Bool) {
}
public func clear() {
}
}
/// A redrawing lit-like progress animation.
public final class RedrawingLitProgressAnimation: ProgressAnimationProtocol {
private let terminal: TerminalController
private let header: String
private var hasDisplayedHeader = false
init(terminal: TerminalController, header: String) {
self.terminal = terminal
self.header = header
}
/// Creates repeating string for count times.
/// If count is negative, returns empty string.
private func repeating(string: String, count: Int) -> String {
return String(repeating: string, count: max(count, 0))
}
public func update(step: Int, total: Int, text: String) {
assert(step <= total)
if !hasDisplayedHeader {
let spaceCount = terminal.width / 2 - header.utf8.count / 2
terminal.write(repeating(string: " ", count: spaceCount))
terminal.write(header, inColor: .cyan, bold: true)
terminal.endLine()
hasDisplayedHeader = true
}
terminal.clearLine()
let percentage = step * 100 / total
let paddedPercentage = percentage < 10 ? " \(percentage)" : "\(percentage)"
let prefix = "\(paddedPercentage)% " + terminal.wrap("[", inColor: .green, bold: true)
terminal.write(prefix)
let barWidth = terminal.width - prefix.utf8.count
let n = Int(Double(barWidth) * Double(percentage) / 100.0)
terminal.write(repeating(string: "=", count: n) + repeating(string: "-", count: barWidth - n), inColor: .green)
terminal.write("]", inColor: .green, bold: true)
terminal.endLine()
terminal.clearLine()
if text.utf8.count > terminal.width {
let prefix = "…"
terminal.write(prefix)
terminal.write(String(text.suffix(terminal.width - prefix.utf8.count)))
} else {
terminal.write(text)
}
terminal.moveCursor(up: 1)
}
public func complete(success: Bool) {
terminal.endLine()
terminal.endLine()
}
public func clear() {
terminal.clearLine()
terminal.moveCursor(up: 1)
terminal.clearLine()
}
}
/// A percent-based progress animation that adapts to the provided output stream.
public final class PercentProgressAnimation: DynamicProgressAnimation {
public init(stream: OutputByteStream, header: String) {
super.init(
stream: stream,
ttyTerminalAnimationFactory: { RedrawingLitProgressAnimation(terminal: $0, header: header) },
dumbTerminalAnimationFactory: { SingleLinePercentProgressAnimation(stream: stream, header: header) },
defaultAnimationFactory: { MultiLinePercentProgressAnimation(stream: stream, header: header) })
}
}
/// A progress animation that adapts to the provided output stream.
public class DynamicProgressAnimation: ProgressAnimationProtocol {
private let animation: ProgressAnimationProtocol
public init(
stream: OutputByteStream,
ttyTerminalAnimationFactory: (TerminalController) -> ProgressAnimationProtocol,
dumbTerminalAnimationFactory: () -> ProgressAnimationProtocol,
defaultAnimationFactory: () -> ProgressAnimationProtocol
) {
if let terminal = TerminalController(stream: stream) {
animation = ttyTerminalAnimationFactory(terminal)
} else if let fileStream = stream as? LocalFileOutputByteStream,
TerminalController.terminalType(fileStream) == .dumb
{
animation = dumbTerminalAnimationFactory()
} else {
animation = defaultAnimationFactory()
}
}
public func update(step: Int, total: Int, text: String) {
animation.update(step: step, total: total, text: text)
}
public func complete(success: Bool) {
animation.complete(success: success)
}
public func clear() {
animation.clear()
}
}