Skip to content

Commit ce77361

Browse files
doc: Document plain clear-on finish behavior (apple#1379)
- Clarified the `.plain` progress contract. This also documents how `clearOnFinish` interacts with plain output. Added test coverage for the current behavior.
1 parent 220fc06 commit ce77361

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

Sources/TerminalProgress/ProgressConfig.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public struct ProgressConfig: Sendable {
6262
/// The theme of the progress bar.
6363
public let theme: ProgressTheme
6464
/// The flag indicating whether to clear the progress bar before resetting the cursor.
65+
/// In `.plain` mode, `finish()` only emits a final line when this value is `false`.
6566
public let clearOnFinish: Bool
6667
/// The flag indicating whether to update the progress bar.
6768
public let disableProgressUpdates: Bool
@@ -89,7 +90,8 @@ public struct ProgressConfig: Sendable {
8990
/// - totalSize: The initial total size of the progress bar. The default value is `nil`.
9091
/// - width: The width of the progress bar in characters. The default value is `120`.
9192
/// - theme: The theme of the progress bar. The default value is `nil`.
92-
/// - clearOnFinish: The flag indicating whether to clear the progress bar before resetting the cursor. The default is `true`.
93+
/// - clearOnFinish: The flag indicating whether to clear the progress bar before resetting the cursor. In `.plain` mode,
94+
/// `finish()` only emits a final line when this value is `false`. The default is `true`.
9395
/// - disableProgressUpdates: The flag indicating whether to update the progress bar. The default is `false`.
9496
/// - outputMode: The output mode for progress rendering. The default is `.ansi`.
9597
public init(

Tests/TerminalProgressTests/ProgressBarTests.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ final class ProgressBarTests: XCTestCase {
880880
try pipe.fileHandleForWriting.close()
881881

882882
let data = pipe.fileHandleForReading.readDataToEndOfFile()
883-
let output = String(data: data, encoding: .utf8) ?? ""
883+
let output = String(decoding: data, as: UTF8.self)
884884
let lines = output.components(separatedBy: "\n").filter { !$0.isEmpty }
885885
// Expect exactly 2 lines: one from render, one from finish
886886
XCTAssertEqual(lines.count, 2)
@@ -903,7 +903,7 @@ final class ProgressBarTests: XCTestCase {
903903
try pipe.fileHandleForWriting.close()
904904

905905
let data = pipe.fileHandleForReading.readDataToEndOfFile()
906-
let output = String(data: data, encoding: .utf8) ?? ""
906+
let output = String(decoding: data, as: UTF8.self)
907907
XCTAssertFalse(output.contains("\u{001B}"))
908908
}
909909

@@ -922,12 +922,32 @@ final class ProgressBarTests: XCTestCase {
922922
try pipe.fileHandleForWriting.close()
923923

924924
let data = pipe.fileHandleForReading.readDataToEndOfFile()
925-
let output = String(data: data, encoding: .utf8) ?? ""
925+
let output = String(decoding: data, as: UTF8.self)
926926
// Plain mode should use newlines, not carriage returns
927927
XCTAssertFalse(output.contains("\r"))
928928
XCTAssertTrue(output.contains("\n"))
929929
}
930930

931+
func testPlainModeDefaultClearOnFinishOmitsFinalLine() async throws {
932+
let pipe = Pipe()
933+
let config = try ProgressConfig(
934+
terminal: pipe.fileHandleForWriting,
935+
description: "Task",
936+
showSpinner: false,
937+
outputMode: .plain
938+
)
939+
let progress = ProgressBar(config: config)
940+
progress.render(force: true)
941+
progress.finish()
942+
try pipe.fileHandleForWriting.close()
943+
944+
let data = pipe.fileHandleForReading.readDataToEndOfFile()
945+
let output = String(decoding: data, as: UTF8.self)
946+
let lines = output.components(separatedBy: "\n").filter { !$0.isEmpty }
947+
XCTAssertEqual(lines.count, 1)
948+
XCTAssertEqual(lines.first, "Task [0s]")
949+
}
950+
931951
func testOutputModeDefaultIsAnsi() async throws {
932952
let config = try ProgressConfig(description: "Task")
933953
XCTAssertEqual(config.outputMode, .ansi)

0 commit comments

Comments
 (0)