Skip to content

Commit 177c38c

Browse files
committed
Add ProcessAsyncEnumerable.FirstAsync, FirstOrDefaultAsync
1 parent 0ad270b commit 177c38c

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ await foreach (string item in ProcessX.StartAsync("dotnet --info"))
4848
Console.WriteLine(item);
4949
}
5050

51+
// receive string result from stdout.
52+
var branch = await ProcessX.StartAsync("git branch --show-current").FirstAsync();
53+
5154
// receive buffered result(similar as WaitForExit).
5255
string[] result = await ProcessX.StartAsync("dotnet --info").ToTask();
5356

@@ -260,6 +263,12 @@ StartReadBinaryAsync(string command, string? workingDirectory = null, IDictionar
260263
StartReadBinaryAsync(string fileName, string? arguments, string? workingDirectory = null, IDictionary<string, string>? environmentVariable = null, Encoding? encoding = null)
261264
StartReadBinaryAsync(ProcessStartInfo processStartInfo)
262265

266+
// return Task<string>
267+
FirstAsync(CancellationToken cancellationToken = default)
268+
269+
// return Task<string?>
270+
FirstOrDefaultAsync(CancellationToken cancellationToken = default)
271+
263272
// return Task<string[]>
264273
ToTask(CancellationToken cancellationToken = default)
265274

sandbox/ConsoleApp/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33

44
// ProcessX and C# 9.0 Top level statement; like google/zx.
55

6+
using Cysharp.Diagnostics;
67
using Zx;
78
using static Zx.Env;
89

10+
11+
12+
var s = await ProcessX.StartAsync("git branch --show-current").FirstAsync();
13+
System.Console.WriteLine(s);
14+
15+
916
// `await string` execute process like shell
1017
await "cat package.json | grep name";
1118

src/ProcessX/ProcessAsyncEnumerable.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ public IAsyncEnumerator<string> GetAsyncEnumerator(CancellationToken cancellatio
2424
return new ProcessAsyncEnumerator(process, channel, cancellationToken);
2525
}
2626

27+
/// <summary>
28+
/// Returning first value. If does not return any data, returns empty string.
29+
/// </summary>
30+
public async Task<string> FirstAsync(CancellationToken cancellationToken = default)
31+
{
32+
await foreach (var item in this.WithCancellation(cancellationToken).ConfigureAwait(false))
33+
{
34+
return item;
35+
}
36+
throw new InvalidOperationException("Process does not return any data.");
37+
}
38+
39+
/// <summary>
40+
/// Returning first value or null.
41+
/// </summary>
42+
public async Task<string?> FirstOrDefaultAsync(CancellationToken cancellationToken = default)
43+
{
44+
await foreach (var item in this.WithCancellation(cancellationToken).ConfigureAwait(false))
45+
{
46+
return item;
47+
}
48+
return default;
49+
}
50+
2751
public async Task<string[]> ToTask(CancellationToken cancellationToken = default)
2852
{
2953
var list = new List<string>();

src/ProcessX/ProcessX.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public static ProcessAsyncEnumerable StartAsync(ProcessStartInfo processStartInf
120120
{
121121
if (e.Data != null)
122122
{
123-
lock (errorList)
123+
lock (errorList)
124124
{
125125
errorList.Add(e.Data);
126126
}

0 commit comments

Comments
 (0)