Skip to content

Commit b7379b7

Browse files
committed
Moved spinner out to an extension, so it can easily be used in other examples
1 parent d58fcbb commit b7379b7

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace LLama.Examples.Extensions
2+
{
3+
public static class IAsyncEnumerableExtensions
4+
{
5+
/// <summary>
6+
/// Show a console spinner while waiting for the next result
7+
/// </summary>
8+
/// <param name="source"></param>
9+
/// <returns></returns>
10+
public static async IAsyncEnumerable<string> Spinner(this IAsyncEnumerable<string> source)
11+
{
12+
var enumerator = source.GetAsyncEnumerator();
13+
14+
var characters = new[] { '|', '/', '-', '\\' };
15+
16+
while (true)
17+
{
18+
var next = enumerator.MoveNextAsync();
19+
20+
var (Left, Top) = Console.GetCursorPosition();
21+
22+
// Keep showing the next spinner character while waiting for "MoveNextAsync" to finish
23+
var count = 0;
24+
while (!next.IsCompleted)
25+
{
26+
count = (count + 1) % characters.Length;
27+
Console.SetCursorPosition(Left, Top);
28+
Console.Write(characters[count]);
29+
await Task.Delay(75);
30+
}
31+
32+
// Clear the spinner character
33+
Console.SetCursorPosition(Left, Top);
34+
Console.Write(" ");
35+
Console.SetCursorPosition(Left, Top);
36+
37+
if (!next.Result)
38+
break;
39+
yield return enumerator.Current;
40+
}
41+
}
42+
}
43+
}
Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using LLama.Common;
2+
using LLama.Examples.Extensions;
23

34
namespace LLama.Examples.NewVersion
45
{
@@ -35,49 +36,11 @@ public static async Task Run()
3536
Console.ForegroundColor = ConsoleColor.White;
3637
Console.Write("Answer: ");
3738
prompt = $"Question: {prompt?.Trim()} Answer: ";
38-
await foreach (var text in Spinner(ex.InferAsync(prompt, inferenceParams)))
39+
await foreach (var text in ex.InferAsync(prompt, inferenceParams).Spinner())
3940
{
4041
Console.Write(text);
4142
}
4243
}
4344
}
44-
45-
/// <summary>
46-
/// Show a spinner while waiting for the next result
47-
/// </summary>
48-
/// <param name="source"></param>
49-
/// <returns></returns>
50-
private static async IAsyncEnumerable<string> Spinner(IAsyncEnumerable<string> source)
51-
{
52-
var enumerator = source.GetAsyncEnumerator();
53-
54-
var characters = new[] { '|', '/', '-', '\\' };
55-
56-
while (true)
57-
{
58-
var next = enumerator.MoveNextAsync();
59-
60-
var (Left, Top) = Console.GetCursorPosition();
61-
62-
// Keep showing the next spinner character while waiting for "MoveNextAsync" to finish
63-
var count = 0;
64-
while (!next.IsCompleted)
65-
{
66-
count = (count + 1) % characters.Length;
67-
Console.SetCursorPosition(Left, Top);
68-
Console.Write(characters[count]);
69-
await Task.Delay(75);
70-
}
71-
72-
// Clear the spinner character
73-
Console.SetCursorPosition(Left, Top);
74-
Console.Write(" ");
75-
Console.SetCursorPosition(Left, Top);
76-
77-
if (!next.Result)
78-
break;
79-
yield return enumerator.Current;
80-
}
81-
}
8245
}
8346
}

0 commit comments

Comments
 (0)