Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ let result = try await run(
error: .string(limit: 4096)
) { execution in
var lineCount = 0
for try await _ in execution.standardOutput.lines() {
for try await _ in execution.standardOutput.strings() {
lineCount += 1
}
return lineCount
Expand All @@ -155,10 +155,10 @@ try await run(
) { execution in
try await withThrowingTaskGroup { group in
group.addTask {
for try await line in execution.standardOutput.lines() { /* ... */ }
for try await line in execution.standardOutput.strings() { /* ... */ }
}
group.addTask {
for try await line in execution.standardError.lines() { /* ... */ }
for try await line in execution.standardError.strings() { /* ... */ }
}
group.addTask {
_ = try await execution.standardInputWriter.write("Hello Subprocess")
Expand All @@ -171,12 +171,12 @@ try await run(

In the closure-based API, output streams are delivered as an `SubprocessOutputSequence` — an asynchronous sequence of `Buffer` values. Each `Buffer` provides access to its bytes via `withUnsafeBytes(_:)` or the `bytes` property (a `RawSpan`).

The preferred way to convert `Buffer` to `String` is to read output line by line using `.lines()`. You can optionally specify an encoding and buffering policy:
The preferred way to convert `Buffer` to `String` is to read output line by line using `.strings()`. You can optionally specify an encoding and buffering policy:

```swift
for try await line in execution.standardOutput.lines(
encoding: UTF16.self,
bufferingPolicy: .maxLineLength(1024)
for try await line in execution.standardOutput.strings(
bufferingPolicy: .maxLineLength(1024),
as: UTF16.self
) {
// ...
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/Subprocess/SubprocessOutputSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public struct SubprocessOutputSequence: AsyncSequence, @unchecked Sendable {

/// Splits the buffer into strings using the specified separator.
///
/// The separator characters are not included in the returned strings,
/// similar to how `.split(separator:)` works. See ``StringSequence``.
///
/// - Parameters:
/// - separator: The delimiter to split on. The default
/// value is `.lineBreaks`.
Expand All @@ -142,6 +145,9 @@ public struct SubprocessOutputSequence: AsyncSequence, @unchecked Sendable {
/// Splits the buffer into strings with the given encoding
/// and separator.
///
/// The separator characters are not included in the returned strings,
/// similar to how `.split(separator:)` works. See ``StringSequence``.
///
/// - Parameters:
/// - separator: The delimiter to split on. The default
/// value is `.lineBreaks`.
Expand Down
Loading