|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace Silk.NET.SilkTouch.Logging |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// A console renderer which handles progress bar writing |
| 14 | + /// </summary> |
| 15 | + public class ConsoleRenderer |
| 16 | + { |
| 17 | + private readonly IProgressService _progressService; |
| 18 | + private readonly TextWriter _originalOut; |
| 19 | + private readonly StringWriter _consoleOutput; |
| 20 | + private readonly Timer _timer; |
| 21 | + private bool _isRunning = true; |
| 22 | + private int _progressBarCount; |
| 23 | + private readonly object _lockObject = new object(); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Creates and instance of the <see cref="ConsoleRenderer"/> |
| 27 | + /// </summary> |
| 28 | + /// <param name="progressService">the progress service</param> |
| 29 | + public ConsoleRenderer(IProgressService progressService) |
| 30 | + { |
| 31 | + _progressService = progressService; |
| 32 | + _originalOut = Console.Out; |
| 33 | + _consoleOutput = new StringWriter(); |
| 34 | + Console.SetOut(_consoleOutput); // Redirect console output |
| 35 | + |
| 36 | + _timer = new Timer(Render, null, 0, 500); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Stops the console renderer processing and restores console to original state |
| 41 | + /// </summary> |
| 42 | + public void Stop() |
| 43 | + { |
| 44 | + _isRunning = false; |
| 45 | + _timer.Dispose(); |
| 46 | + Console.SetOut(_originalOut); // Restore original output |
| 47 | + } |
| 48 | + |
| 49 | + private void Render(object? state) |
| 50 | + { |
| 51 | + if (!_isRunning && _consoleOutput.GetStringBuilder().Length > 0) |
| 52 | + return; |
| 53 | + |
| 54 | + lock (_lockObject) |
| 55 | + { |
| 56 | + // Clear the progress bars by moving the cursor and overwriting with spaces |
| 57 | + if (_progressBarCount > 0) |
| 58 | + { |
| 59 | + int currentTop = Console.CursorTop; |
| 60 | + Console.SetCursorPosition(0, currentTop - _progressBarCount); |
| 61 | + for (int i = 0; i < _progressBarCount; i++) |
| 62 | + { |
| 63 | + _originalOut.Write(new string(' ', Console.WindowWidth)); |
| 64 | + } |
| 65 | + Console.SetCursorPosition(0, currentTop - _progressBarCount); |
| 66 | + } |
| 67 | + |
| 68 | + var progressDictionary = _progressService.GetAllProgress(); |
| 69 | + _progressBarCount = progressDictionary.Count(); |
| 70 | + |
| 71 | + // Write all console output captured since the last render |
| 72 | + var output = _consoleOutput.GetStringBuilder().ToString(); |
| 73 | + if (!string.IsNullOrWhiteSpace(output)) |
| 74 | + { |
| 75 | + _originalOut.Write(output); |
| 76 | + _consoleOutput.GetStringBuilder().Clear(); |
| 77 | + } |
| 78 | + |
| 79 | + foreach (var kvp in progressDictionary) |
| 80 | + { |
| 81 | + string task = string.IsNullOrWhiteSpace(kvp.Value.Item1) ? string.Empty : $" - {kvp.Value.Item1}"; |
| 82 | + _originalOut.WriteLine($"{kvp.Key}{task}: ({new string('|', (int)(kvp.Value.Item2 * 20))}{new string('-', 20 - (int)(kvp.Value.Item2 * 20))}) ({(kvp.Value.Item2 * 100):F2}%)"); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments