From 7d8c4c071ab4345788f33c95757b394ab61b6bde Mon Sep 17 00:00:00 2001 From: Ben Adams <thundercat@illyriad.co.uk> Date: Tue, 2 Jul 2024 23:29:52 +0100 Subject: [PATCH] Clamp progress bar inputs (#7235) --- src/Nethermind/Nethermind.Logging/Progress.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Nethermind/Nethermind.Logging/Progress.cs b/src/Nethermind/Nethermind.Logging/Progress.cs index 523d9e7a97f..167f4a16c6d 100644 --- a/src/Nethermind/Nethermind.Logging/Progress.cs +++ b/src/Nethermind/Nethermind.Logging/Progress.cs @@ -26,10 +26,11 @@ private static string[] CreateChunks(char ch, string start, string end) public static string GetMeter(float value, int max) { - float progressF = value / max * WidthBar; + float progressF = Math.Clamp(value / max, 0, 1) * WidthBar; int progress = (int)Math.Floor(progressF); - int progressChar = (int)((progressF - progress) * _progressChars.Length); + int progressChar = Math.Clamp((int)((progressF - progress) * _progressChars.Length), 0, _progressChars.Length - 1); + int empty = Math.Clamp(WidthBar - progress - 1, 0, WidthBar); - return string.Concat(_fullChunks[progress], _progressChars[progressChar], _emptyChunks[WidthBar - progress - 1]); + return string.Concat(_fullChunks[progress], _progressChars[progressChar], _emptyChunks[empty]); } }