From f77750894912d7471f1c0c572029af4a6d0eefda Mon Sep 17 00:00:00 2001 From: Kattouf Date: Mon, 2 Sep 2024 18:51:37 +0700 Subject: [PATCH 1/2] add --static-text option --- Sources/ProgressLine.swift | 4 ++++ Sources/ProgressLineController.swift | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Sources/ProgressLine.swift b/Sources/ProgressLine.swift index ada6289..4c7ad89 100644 --- a/Sources/ProgressLine.swift +++ b/Sources/ProgressLine.swift @@ -11,6 +11,9 @@ struct ProgressLine: AsyncParsableCommand { usage: "some-command | progressline" ) + @Option(name: [.long, .customShort("t")], help: "The static text to display instead of the latest stdin data.") + var staticText: String? + @Option(name: [.customLong("activity-style"), .customShort("s")], help: "The style of the activity indicator.") var activityIndicatorStyle: ActivityIndicatorStyle = .dots @@ -44,6 +47,7 @@ struct ProgressLine: AsyncParsableCommand { let activityIndicator: ActivityIndicator = .make(style: activityIndicatorStyle) #endif let progressLineController = await ProgressLineController.buildAndStart( + textMode: staticText.map { .staticText($0) } ?? .stdin, printers: printers, logger: logger, activityIndicator: activityIndicator, diff --git a/Sources/ProgressLineController.swift b/Sources/ProgressLineController.swift index 296607f..9a04d03 100644 --- a/Sources/ProgressLineController.swift +++ b/Sources/ProgressLineController.swift @@ -2,7 +2,13 @@ import Foundation import TaggedTime final actor ProgressLineController { + enum TextMode { + case staticText(String) + case stdin + } + // Dependencies + private let textMode: TextMode private let printers: PrintersHolder private let logger: AboveProgressLineLogger private let progressLineFormatter: ProgressLineFormatter @@ -13,11 +19,13 @@ final actor ProgressLineController { private var progress: Progress? private init( + textMode: TextMode, printers: PrintersHolder, logger: AboveProgressLineLogger, progressLineFormatter: ProgressLineFormatter, progressTracker: ProgressTracker ) { + self.textMode = textMode self.printers = printers self.logger = logger self.progressLineFormatter = progressLineFormatter @@ -27,6 +35,7 @@ final actor ProgressLineController { // MARK: - Public static func buildAndStart( + textMode: TextMode, printers: PrintersHolder, logger: AboveProgressLineLogger, activityIndicator: ActivityIndicator, @@ -41,6 +50,7 @@ final actor ProgressLineController { ) let controller = Self( + textMode: textMode, printers: printers, logger: logger, progressLineFormatter: progressLineFormatter, @@ -54,6 +64,10 @@ final actor ProgressLineController { // MARK: - Input func didGetStdinDataChunk(_ data: Data) async { + guard case .stdin = textMode else { + return + } + let stdinText = String(data: data, encoding: .utf8) guard let stdinText else { await logger.logError(ErrorMessage.canNotDecodeData) @@ -100,7 +114,13 @@ final actor ProgressLineController { } private func redrawProgressLine() async { - let progress = progressTracker.moveForward(lastStdinLine) + let lineText: String? = switch textMode { + case .staticText(let text): + text + case .stdin: + lastStdinLine + } + let progress = progressTracker.moveForward(lineText) let progressLine = progressLineFormatter.inProgress(progress: progress) self.progress = progress await printers.withPrinter { printer in From 965e60f9e8226d641f090ccfd92fb952c0dca1fe Mon Sep 17 00:00:00 2001 From: Kattouf Date: Mon, 2 Sep 2024 18:53:30 +0700 Subject: [PATCH 2/2] add static-text mode integration test --- Tests/integration_tests.sh | 5 +++++ Tests/snapshots/static_text.snapshot | 2 ++ 2 files changed, 7 insertions(+) create mode 100644 Tests/snapshots/static_text.snapshot diff --git a/Tests/integration_tests.sh b/Tests/integration_tests.sh index ae16b48..7a38bd9 100755 --- a/Tests/integration_tests.sh +++ b/Tests/integration_tests.sh @@ -33,6 +33,11 @@ generate_test_output="swift $TESTS_DIR/test_data_producer.swift $test_data_produ output=$($generate_test_output | "$executable_path" --test-mode) assert_snapshot "default" "$output" +# Test static text mode + +output=$($generate_test_output | "$executable_path" --test-mode --static-text "Static text") +assert_snapshot "static_text" "$output" + # Test default mode with save original log output=$($generate_test_output | "$executable_path" --test-mode --original-log-path /tmp/progressline_test_original_log.txt) diff --git a/Tests/snapshots/static_text.snapshot b/Tests/snapshots/static_text.snapshot new file mode 100644 index 0000000..10fa7b7 --- /dev/null +++ b/Tests/snapshots/static_text.snapshot @@ -0,0 +1,2 @@ + ❯ Static text +✓ ❯ Static text