You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

IAsyncEnumerableExtensions.cs 1.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. var characters = new[] { '|', '/', '-', '\\' };
  14. while (true)
  15. {
  16. var next = enumerator.MoveNextAsync();
  17. var (Left, Top) = Console.GetCursorPosition();
  18. // Keep showing the next spinner character while waiting for "MoveNextAsync" to finish
  19. var count = 0;
  20. while (!next.IsCompleted)
  21. {
  22. count = (count + 1) % characters.Length;
  23. Console.SetCursorPosition(Left, Top);
  24. Console.Write(characters[count]);
  25. await Task.Delay(75);
  26. }
  27. // Clear the spinner character
  28. Console.SetCursorPosition(Left, Top);
  29. Console.Write(" ");
  30. Console.SetCursorPosition(Left, Top);
  31. if (!next.Result)
  32. break;
  33. yield return enumerator.Current;
  34. }
  35. }
  36. }
  37. }