Skip to content

Commit 197fdf6

Browse files
authored
Merge pull request #3 from kattouf/static-text-option
add --static-text option
2 parents b8623cf + 965e60f commit 197fdf6

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

Sources/ProgressLine.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ struct ProgressLine: AsyncParsableCommand {
1111
usage: "some-command | progressline"
1212
)
1313

14+
@Option(name: [.long, .customShort("t")], help: "The static text to display instead of the latest stdin data.")
15+
var staticText: String?
16+
1417
@Option(name: [.customLong("activity-style"), .customShort("s")], help: "The style of the activity indicator.")
1518
var activityIndicatorStyle: ActivityIndicatorStyle = .dots
1619

@@ -44,6 +47,7 @@ struct ProgressLine: AsyncParsableCommand {
4447
let activityIndicator: ActivityIndicator = .make(style: activityIndicatorStyle)
4548
#endif
4649
let progressLineController = await ProgressLineController.buildAndStart(
50+
textMode: staticText.map { .staticText($0) } ?? .stdin,
4751
printers: printers,
4852
logger: logger,
4953
activityIndicator: activityIndicator,

Sources/ProgressLineController.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import Foundation
22
import TaggedTime
33

44
final actor ProgressLineController {
5+
enum TextMode {
6+
case staticText(String)
7+
case stdin
8+
}
9+
510
// Dependencies
11+
private let textMode: TextMode
612
private let printers: PrintersHolder
713
private let logger: AboveProgressLineLogger
814
private let progressLineFormatter: ProgressLineFormatter
@@ -13,11 +19,13 @@ final actor ProgressLineController {
1319
private var progress: Progress?
1420

1521
private init(
22+
textMode: TextMode,
1623
printers: PrintersHolder,
1724
logger: AboveProgressLineLogger,
1825
progressLineFormatter: ProgressLineFormatter,
1926
progressTracker: ProgressTracker
2027
) {
28+
self.textMode = textMode
2129
self.printers = printers
2230
self.logger = logger
2331
self.progressLineFormatter = progressLineFormatter
@@ -27,6 +35,7 @@ final actor ProgressLineController {
2735
// MARK: - Public
2836

2937
static func buildAndStart(
38+
textMode: TextMode,
3039
printers: PrintersHolder,
3140
logger: AboveProgressLineLogger,
3241
activityIndicator: ActivityIndicator,
@@ -41,6 +50,7 @@ final actor ProgressLineController {
4150
)
4251

4352
let controller = Self(
53+
textMode: textMode,
4454
printers: printers,
4555
logger: logger,
4656
progressLineFormatter: progressLineFormatter,
@@ -54,6 +64,10 @@ final actor ProgressLineController {
5464
// MARK: - Input
5565

5666
func didGetStdinDataChunk(_ data: Data) async {
67+
guard case .stdin = textMode else {
68+
return
69+
}
70+
5771
let stdinText = String(data: data, encoding: .utf8)
5872
guard let stdinText else {
5973
await logger.logError(ErrorMessage.canNotDecodeData)
@@ -100,7 +114,13 @@ final actor ProgressLineController {
100114
}
101115

102116
private func redrawProgressLine() async {
103-
let progress = progressTracker.moveForward(lastStdinLine)
117+
let lineText: String? = switch textMode {
118+
case .staticText(let text):
119+
text
120+
case .stdin:
121+
lastStdinLine
122+
}
123+
let progress = progressTracker.moveForward(lineText)
104124
let progressLine = progressLineFormatter.inProgress(progress: progress)
105125
self.progress = progress
106126
await printers.withPrinter { printer in

Tests/integration_tests.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ generate_test_output="swift $TESTS_DIR/test_data_producer.swift $test_data_produ
3333
output=$($generate_test_output | "$executable_path" --test-mode)
3434
assert_snapshot "default" "$output"
3535

36+
# Test static text mode
37+
38+
output=$($generate_test_output | "$executable_path" --test-mode --static-text "Static text")
39+
assert_snapshot "static_text" "$output"
40+
3641
# Test default mode with save original log
3742

3843
output=$($generate_test_output | "$executable_path" --test-mode --original-log-path /tmp/progressline_test_original_log.txt)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<activity> <duration> ❯ Static text
2+
✓ <duration> ❯ Static text

0 commit comments

Comments
 (0)