Skip to content

Commit

Permalink
Merge pull request #3 from kattouf/static-text-option
Browse files Browse the repository at this point in the history
add --static-text option
  • Loading branch information
kattouf authored Sep 2, 2024
2 parents b8623cf + 965e60f commit 197fdf6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Sources/ProgressLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
22 changes: 21 additions & 1 deletion Sources/ProgressLineController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -27,6 +35,7 @@ final actor ProgressLineController {
// MARK: - Public

static func buildAndStart(
textMode: TextMode,
printers: PrintersHolder,
logger: AboveProgressLineLogger,
activityIndicator: ActivityIndicator,
Expand All @@ -41,6 +50,7 @@ final actor ProgressLineController {
)

let controller = Self(
textMode: textMode,
printers: printers,
logger: logger,
progressLineFormatter: progressLineFormatter,
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Tests/integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Tests/snapshots/static_text.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<activity> <duration> ❯ Static text
✓ <duration> ❯ Static text

0 comments on commit 197fdf6

Please sign in to comment.