| | | 1 | | namespace Kestrun.Tool; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Writes in-place progress updates for module operations. |
| | | 5 | | /// </summary> |
| | 4 | 6 | | internal sealed class ConsoleProgressBar(string label, long? total, Func<long, long?, string>? detailFormatter) : IDispo |
| | | 7 | | { |
| | | 8 | | private const int ProgressBarWidth = 28; |
| | 1 | 9 | | private static readonly TimeSpan RenderThrottle = TimeSpan.FromMilliseconds(80); |
| | | 10 | | |
| | 4 | 11 | | private readonly string _label = label; |
| | 4 | 12 | | private readonly long? _total = total.HasValue && total.Value > 0 ? total : null; |
| | 4 | 13 | | private readonly Func<long, long?, string>? _detailFormatter = detailFormatter; |
| | 4 | 14 | | private readonly bool _enabled = !Console.IsOutputRedirected; |
| | | 15 | | private int _lastRenderedLength; |
| | 4 | 16 | | private int _lastPercent = -1; |
| | 4 | 17 | | private long _lastValue = -1; |
| | 4 | 18 | | private DateTime _lastRenderUtc = DateTime.MinValue; |
| | | 19 | | private bool _hasRendered; |
| | | 20 | | private bool _isComplete; |
| | | 21 | | |
| | | 22 | | public void Report(long value, bool force = false) |
| | | 23 | | { |
| | 1 | 24 | | if (!_enabled || _isComplete) |
| | | 25 | | { |
| | 1 | 26 | | return; |
| | | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | var sanitizedValue = Math.Max(0, value); |
| | 0 | 30 | | var percent = GetPercent(sanitizedValue); |
| | 0 | 31 | | var now = DateTime.UtcNow; |
| | | 32 | | |
| | 0 | 33 | | if (!force |
| | 0 | 34 | | && sanitizedValue == _lastValue |
| | 0 | 35 | | && percent == _lastPercent) |
| | | 36 | | { |
| | 0 | 37 | | return; |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | if (!force |
| | 0 | 41 | | && percent == _lastPercent |
| | 0 | 42 | | && now - _lastRenderUtc < RenderThrottle) |
| | | 43 | | { |
| | 0 | 44 | | return; |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | _lastValue = sanitizedValue; |
| | 0 | 48 | | _lastPercent = percent; |
| | 0 | 49 | | _lastRenderUtc = now; |
| | 0 | 50 | | Render(sanitizedValue, percent); |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | public void Complete(long value) |
| | | 54 | | { |
| | 1 | 55 | | if (!_enabled || _isComplete) |
| | | 56 | | { |
| | 1 | 57 | | return; |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | var completionValue = _total ?? Math.Max(0, value); |
| | 0 | 61 | | Report(completionValue, force: true); |
| | 0 | 62 | | Console.WriteLine(); |
| | 0 | 63 | | _isComplete = true; |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | public void Dispose() |
| | | 67 | | { |
| | 1 | 68 | | if (_enabled && _hasRendered && !_isComplete) |
| | | 69 | | { |
| | 0 | 70 | | Console.WriteLine(); |
| | | 71 | | } |
| | 1 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Returns the percentage (0-100) for the given value based on the total, or -1 if percentage cannot be calculated. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="value">The current value.</param> |
| | | 78 | | /// <returns>The percentage (0-100) or -1 if percentage cannot be calculated.</returns> |
| | | 79 | | private int GetPercent(long value) |
| | | 80 | | { |
| | 3 | 81 | | if (!_total.HasValue || _total.Value <= 0) |
| | | 82 | | { |
| | 1 | 83 | | return -1; |
| | | 84 | | } |
| | | 85 | | // Use long math to avoid overflow, then clamp to 0-100. |
| | 2 | 86 | | return (int)Math.Clamp(value * 100L / _total.Value, 0, 100); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | private void Render(long value, int percent) |
| | | 90 | | { |
| | 1 | 91 | | var detail = _detailFormatter is null |
| | 1 | 92 | | ? _total.HasValue |
| | 1 | 93 | | ? $"{value}/{_total.Value}" |
| | 1 | 94 | | : value.ToString() |
| | 1 | 95 | | : _detailFormatter(value, _total); |
| | | 96 | | |
| | 1 | 97 | | var line = percent >= 0 |
| | 1 | 98 | | ? $"{_label} {BuildBar(percent)} {percent,3}% {detail}" |
| | 1 | 99 | | : $"{_label} {detail}"; |
| | | 100 | | |
| | 1 | 101 | | if (line.Length < _lastRenderedLength) |
| | | 102 | | { |
| | 0 | 103 | | line = line.PadRight(_lastRenderedLength); |
| | | 104 | | } |
| | | 105 | | |
| | 1 | 106 | | _lastRenderedLength = line.Length; |
| | 1 | 107 | | _hasRendered = true; |
| | | 108 | | |
| | 1 | 109 | | Console.Write('\r'); |
| | 1 | 110 | | Console.Write(line); |
| | 1 | 111 | | } |
| | | 112 | | |
| | | 113 | | private static string BuildBar(int percent) |
| | | 114 | | { |
| | 4 | 115 | | var filled = (int)Math.Round(percent / 100d * ProgressBarWidth, MidpointRounding.AwayFromZero); |
| | 4 | 116 | | filled = Math.Clamp(filled, 0, ProgressBarWidth); |
| | 4 | 117 | | return $"[{new string('#', filled)}{new string('-', ProgressBarWidth - filled)}]"; |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |