Skip to content

Commit 3fc3c73

Browse files
refactor(progressconfig): convert bools to OutputMode enum
Signed-off-by: Karan <karanlokchandani@protonmail.com>
1 parent 2123490 commit 3fc3c73

File tree

8 files changed

+62
-34
lines changed

8 files changed

+62
-34
lines changed

Sources/ContainerBuild/BuildImageResolver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct BuildImageResolver: BuildPipelineHandler {
6666
showProgressBar: true,
6767
showSize: true,
6868
showSpeed: true,
69-
disableProgressUpdates: self.quiet
69+
outputMode: self.quiet ? .none : .ansi
7070
)
7171
let progress = ProgressBar(config: progressConfig)
7272
defer { progress.finish() }

Sources/ContainerCommands/Container/ContainerRun.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,14 @@ extension Application {
6565

6666
var progressConfig: ProgressConfig
6767
switch self.progressFlags.progress {
68-
case .none: progressConfig = try ProgressConfig(disableProgressUpdates: true)
68+
case .none: progressConfig = try ProgressConfig(outputMode: .none)
6969
case .ansi, .plain, .color:
7070
progressConfig = try ProgressConfig(
7171
showTasks: true,
7272
showItems: true,
7373
ignoreSmallSize: true,
7474
totalTasks: 6,
75-
color: self.progressFlags.progress == .color,
76-
plain: self.progressFlags.progress == .plain
75+
outputMode: self.progressFlags.progress.outputMode
7776
)
7877
}
7978

Sources/ContainerCommands/Image/ImagePull.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,14 @@ extension Application {
8282

8383
var progressConfig: ProgressConfig
8484
switch self.progressFlags.progress {
85-
case .none: progressConfig = try ProgressConfig(disableProgressUpdates: true)
85+
case .none: progressConfig = try ProgressConfig(outputMode: .none)
8686
case .ansi, .plain, .color:
8787
progressConfig = try ProgressConfig(
8888
showTasks: true,
8989
showItems: true,
9090
ignoreSmallSize: true,
9191
totalTasks: 2,
92-
color: self.progressFlags.progress == .color,
93-
plain: self.progressFlags.progress == .plain
92+
outputMode: self.progressFlags.progress.outputMode
9493
)
9594
}
9695

Sources/ContainerCommands/Image/ImagePush.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,15 @@ extension Application {
6969

7070
var progressConfig: ProgressConfig
7171
switch self.progressFlags.progress {
72-
case .none: progressConfig = try ProgressConfig(disableProgressUpdates: true)
72+
case .none: progressConfig = try ProgressConfig(outputMode: .none)
7373
case .ansi, .color, .plain:
7474
progressConfig = try ProgressConfig(
7575
description: "Pushing image \(image.reference)",
7676
itemsName: "blobs",
7777
showItems: true,
7878
showSpeed: false,
7979
ignoreSmallSize: true,
80-
color: self.progressFlags.progress == .color,
81-
plain: self.progressFlags.progress == .plain
80+
outputMode: self.progressFlags.progress.outputMode
8281
)
8382
}
8483

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===----------------------------------------------------------------------===//
2+
// Copyright © 2025 Apple Inc. and the container project authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//===----------------------------------------------------------------------===//
16+
17+
import ContainerClient
18+
import TerminalProgress
19+
20+
extension Flags.Progress.ProgressType {
21+
/// Converts the progress type to a ProgressConfig output mode.
22+
var outputMode: ProgressConfig.OutputMode {
23+
switch self {
24+
case .none: .none
25+
case .ansi: .ansi
26+
case .plain: .plain
27+
case .color: .color
28+
}
29+
}
30+
}

Sources/TerminalProgress/ProgressBar+Terminal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ extension ProgressBar {
6363
}
6464

6565
func displayText(_ text: String, terminating: String = "\r") {
66-
if config.plain {
66+
if config.outputMode == .plain {
6767
display(text + "\n")
6868
return
6969
}

Sources/TerminalProgress/ProgressBar.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public final class ProgressBar: Sendable {
7272
$0.subDescription = ""
7373
$0.tasks += 1
7474
}
75-
if config.plain {
75+
if config.outputMode == .plain {
7676
render(force: true)
7777
}
7878
}
@@ -83,7 +83,7 @@ public final class ProgressBar: Sendable {
8383
resetCurrentTask()
8484

8585
state.withLock { $0.subDescription = subDescription }
86-
if config.plain {
86+
if config.outputMode == .plain {
8787
render(force: true)
8888
}
8989
}
@@ -102,7 +102,7 @@ public final class ProgressBar: Sendable {
102102
/// Starts an animation of the progress bar.
103103
/// - Parameter intervalSeconds: The time interval between updates in seconds.
104104
public func start(intervalSeconds: TimeInterval = 0.04) {
105-
if config.plain {
105+
if config.outputMode == .plain {
106106
return
107107
}
108108
Task(priority: .utility) {
@@ -121,7 +121,7 @@ public final class ProgressBar: Sendable {
121121
// The last render.
122122
render(force: true)
123123

124-
if !config.disableProgressUpdates && !config.clearOnFinish {
124+
if config.outputMode != .none && !config.clearOnFinish {
125125
displayText(state.withLock { $0.output }, terminating: "\n")
126126
}
127127

@@ -143,7 +143,7 @@ extension ProgressBar {
143143
}
144144

145145
func render(force: Bool = false) {
146-
guard term != nil && !config.disableProgressUpdates && (force || !state.withLock { $0.finished }) else {
146+
guard term != nil && config.outputMode != .none && (force || !state.withLock { $0.finished }) else {
147147
return
148148
}
149149
let output = draw()
@@ -298,7 +298,7 @@ extension ProgressBar {
298298
}
299299

300300
private func colorize(_ text: String, _ color: String) -> String {
301-
guard config.color else { return text }
301+
guard config.outputMode == .color else { return text }
302302
return "\(color)\(text)\(Color.reset)"
303303
}
304304

Sources/TerminalProgress/ProgressConfig.swift

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ import Foundation
1818

1919
/// A configuration for displaying a progress bar.
2020
public struct ProgressConfig: Sendable {
21+
/// The output mode for progress display.
22+
public enum OutputMode: Sendable {
23+
/// No progress output.
24+
case none
25+
/// ANSI control sequences without color (default).
26+
case ansi
27+
/// Plain text output, one line per update.
28+
case plain
29+
/// ANSI control sequences with color.
30+
case color
31+
}
2132
/// The file handle for progress updates.
2233
let terminal: FileHandle
2334
/// The initial description of the progress bar.
@@ -63,12 +74,8 @@ public struct ProgressConfig: Sendable {
6374
public let theme: ProgressTheme
6475
/// The flag indicating whether to clear the progress bar before resetting the cursor.
6576
public let clearOnFinish: Bool
66-
/// The flag indicating whether to update the progress bar.
67-
public let disableProgressUpdates: Bool
68-
/// The flag indicating whether to use colors.
69-
public let color: Bool
70-
/// The flag indicating whether to use plain output (no ANSI codes, one line per update).
71-
public let plain: Bool
77+
/// The output mode for progress display.
78+
public let outputMode: OutputMode
7279
/// Creates a new instance of `ProgressConfig`.
7380
/// - Parameters:
7481
/// - terminal: The file handle for progress updates. The default value is `FileHandle.standardError`.
@@ -91,9 +98,7 @@ public struct ProgressConfig: Sendable {
9198
/// - width: The width of the progress bar in characters. The default value is `120`.
9299
/// - theme: The theme of the progress bar. The default value is `nil`.
93100
/// - clearOnFinish: The flag indicating whether to clear the progress bar before resetting the cursor. The default is `true`.
94-
/// - disableProgressUpdates: The flag indicating whether to update the progress bar. The default is `false`.
95-
/// - color: A flag indicating whether to enable ANSI color output. The default value is `false`.
96-
/// - plain: A flag indicating whether to force plain output with no control sequences. The default value is `false`.
101+
/// - outputMode: The output mode for progress display. The default value is `.ansi`.
97102
public init(
98103
terminal: FileHandle = .standardError,
99104
description: String = "",
@@ -115,9 +120,7 @@ public struct ProgressConfig: Sendable {
115120
width: Int = 120,
116121
theme: ProgressTheme? = nil,
117122
clearOnFinish: Bool = true,
118-
disableProgressUpdates: Bool = false,
119-
color: Bool = false,
120-
plain: Bool = false
123+
outputMode: OutputMode = .ansi
121124
) throws {
122125
if let totalTasks {
123126
guard totalTasks > 0 else {
@@ -140,11 +143,11 @@ public struct ProgressConfig: Sendable {
140143
self.initialSubDescription = subDescription
141144
self.initialItemsName = itemsName
142145

143-
self.showSpinner = plain ? false : showSpinner
146+
self.showSpinner = (outputMode == .plain || outputMode == .none) ? false : showSpinner
144147
self.showTasks = showTasks
145148
self.showDescription = showDescription
146149
self.showPercent = showPercent
147-
self.showProgressBar = plain ? false : showProgressBar
150+
self.showProgressBar = (outputMode == .plain || outputMode == .none) ? false : showProgressBar
148151
self.showItems = showItems
149152
self.showSize = showSize
150153
self.showSpeed = showSpeed
@@ -158,9 +161,7 @@ public struct ProgressConfig: Sendable {
158161
self.width = width
159162
self.theme = theme ?? DefaultProgressTheme()
160163
self.clearOnFinish = clearOnFinish
161-
self.disableProgressUpdates = disableProgressUpdates
162-
self.color = plain ? false : color
163-
self.plain = plain
164+
self.outputMode = outputMode
164165
}
165166
}
166167

0 commit comments

Comments
 (0)