From 7f8e9072c9be0afccadfe74b9225e9fd9691ced0 Mon Sep 17 00:00:00 2001 From: Deepak Sangle Date: Tue, 3 Oct 2023 21:02:34 +0530 Subject: [PATCH] [Peek]Fix file and folder sizes to be similar to Explorer (#28089) * [Peek] displaying file size in correct grammatical format * Update Directory.Packages.props * removed unnecessary file * [Peek] Added new symbols in resources.resw * [Peek] added commas separation * modified spell check * [Peek] Changed 1024 factor to 1000 factor * modified spell check * made string country specific * fix: used 1024 and fixed precision * spellcheck fixed * Update src/modules/peek/Peek.UI/Strings/en-us/Resources.resw * Update src/modules/peek/Peek.UI/Strings/en-us/Resources.resw --- .github/actions/spell-check/expect.txt | 1 - .../peek/Peek.Common/Helpers/MathHelper.cs | 8 +++ .../Helpers/ReadableStringHelper.cs | 67 ++++++++++++++----- .../peek/Peek.UI/Strings/en-us/Resources.resw | 24 ++++--- 4 files changed, 74 insertions(+), 26 deletions(-) diff --git a/.github/actions/spell-check/expect.txt b/.github/actions/spell-check/expect.txt index 6c848b3e3b..f0795e524b 100644 --- a/.github/actions/spell-check/expect.txt +++ b/.github/actions/spell-check/expect.txt @@ -1370,7 +1370,6 @@ pef PElems Pels PERCEIVEDFLAG -Percision perfmon pesi petabyte diff --git a/src/modules/peek/Peek.Common/Helpers/MathHelper.cs b/src/modules/peek/Peek.Common/Helpers/MathHelper.cs index e731d14030..d9b8322c7d 100644 --- a/src/modules/peek/Peek.Common/Helpers/MathHelper.cs +++ b/src/modules/peek/Peek.Common/Helpers/MathHelper.cs @@ -2,6 +2,9 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; +using System.Globalization; + namespace Peek.Common.Helpers { public static class MathHelper @@ -10,5 +13,10 @@ namespace Peek.Common.Helpers { return a < 0 ? ((a % b) + b) % b : a % b; } + + public static int NumberOfDigits(int num) + { + return Math.Abs(num).ToString(CultureInfo.InvariantCulture).Length; + } } } diff --git a/src/modules/peek/Peek.Common/Helpers/ReadableStringHelper.cs b/src/modules/peek/Peek.Common/Helpers/ReadableStringHelper.cs index bc2cba4547..0ea75816e9 100644 --- a/src/modules/peek/Peek.Common/Helpers/ReadableStringHelper.cs +++ b/src/modules/peek/Peek.Common/Helpers/ReadableStringHelper.cs @@ -10,34 +10,40 @@ namespace Peek.Common.Helpers { public static class ReadableStringHelper { - private const int DecimalPercision = 10; + private const int MaxDigitsToDisplay = 3; + private const int PowerFactor = 1024; public static string BytesToReadableString(ulong bytes) { - var resourceLoader = ResourceLoaderInstance.ResourceLoader; - List format = new List - { - (bytes == 1) ? - resourceLoader.GetString("ReadableString_ByteAbbreviationFormat") : // "byte" - resourceLoader.GetString("ReadableString_BytesAbbreviationFormat"), // "bytes" - resourceLoader.GetString("ReadableString_KiloByteAbbreviationFormat"), // "KB" - resourceLoader.GetString("ReadableString_MegaByteAbbreviationFormat"), // "MB" - resourceLoader.GetString("ReadableString_GigaByteAbbreviationFormat"), // "GB" - resourceLoader.GetString("ReadableString_TeraByteAbbreviationFormat"), // "TB" - resourceLoader.GetString("ReadableString_PetaByteAbbreviationFormat"), // "PB" - resourceLoader.GetString("ReadableString_ExaByteAbbreviationFormat"), // "EB" - }; + string totalBytesDisplays = (bytes == 1) ? + ResourceLoaderInstance.ResourceLoader.GetString("ReadableString_ByteString") : + ResourceLoaderInstance.ResourceLoader.GetString("ReadableString_BytesString"); int index = 0; double number = 0.0; if (bytes > 0) { - index = (int)Math.Floor(Math.Log(bytes) / Math.Log(1024)); - number = Math.Round((bytes / Math.Pow(1024, index)) * DecimalPercision) / DecimalPercision; + index = (int)Math.Floor(Math.Log(bytes) / Math.Log(PowerFactor)); + number = bytes / Math.Pow(PowerFactor, index); } - return string.Format(CultureInfo.InvariantCulture, format[index], number); + if (index > 0 && number >= Math.Pow(10, MaxDigitsToDisplay)) + { + index++; + number = bytes / Math.Pow(PowerFactor, index); + } + + int precision = GetPrecision(index, number); + int decimalPrecision = (int)Math.Pow(10, precision); + + number = Math.Truncate(number * decimalPrecision) / decimalPrecision; + + string formatSpecifier = GetFormatSpecifierString(index, number, bytes, precision); + + return bytes == 0 + ? string.Format(CultureInfo.CurrentCulture, formatSpecifier, number) + : string.Format(CultureInfo.CurrentCulture, formatSpecifier + totalBytesDisplays, number, bytes); } public static string FormatResourceString(string resourceId, object? args) @@ -55,5 +61,32 @@ namespace Peek.Common.Helpers return formattedString; } + + public static int GetPrecision(int index, double number) + { + int numberOfDigits = MathHelper.NumberOfDigits((int)number); + return index == 0 ? + 0 : + MaxDigitsToDisplay - numberOfDigits; + } + + public static string GetFormatSpecifierString(int index, double number, ulong bytes, int precision) + { + var resourceLoader = ResourceLoaderInstance.ResourceLoader; + List format = new List + { + (bytes == 1) ? + resourceLoader.GetString("ReadableString_ByteAbbreviationFormat") : // "byte" + resourceLoader.GetString("ReadableString_BytesAbbreviationFormat"), // "bytes" + resourceLoader.GetString("ReadableString_KiloByteAbbreviationFormat"), // "KB" + resourceLoader.GetString("ReadableString_MegaByteAbbreviationFormat"), // "MB" + resourceLoader.GetString("ReadableString_GigaByteAbbreviationFormat"), // "GB" + resourceLoader.GetString("ReadableString_TeraByteAbbreviationFormat"), // "TB" + resourceLoader.GetString("ReadableString_PetaByteAbbreviationFormat"), // "PB" + resourceLoader.GetString("ReadableString_ExaByteAbbreviationFormat"), // "EB" + }; + + return "{0:F" + precision + "} " + format[index]; + } } } diff --git a/src/modules/peek/Peek.UI/Strings/en-us/Resources.resw b/src/modules/peek/Peek.UI/Strings/en-us/Resources.resw index d8df22b84a..b711a9e26f 100644 --- a/src/modules/peek/Peek.UI/Strings/en-us/Resources.resw +++ b/src/modules/peek/Peek.UI/Strings/en-us/Resources.resw @@ -154,31 +154,31 @@ Date Modified label for the unsupported files view. {0} is the date. - {0} byte + byte Abbreviation for the size unit byte. - {0} KB + KB Abbreviation for the size unit kilobyte. - {0} MB + MB Abbreviation for the size unit megabyte. - {0} GB + GB Abbreviation for the size unit gigabyte. - {0} TB + TB Abbreviation for the size unit terabyte. - {0} PB + PB Abbreviation for the size unit petabyte. - {0} EB + EB Abbreviation for the size unit exabyte. @@ -234,7 +234,7 @@ {0} is the size of the archive, {1} is the extracted size - {0} bytes + bytes Abbreviation for the size bytes @@ -253,4 +253,12 @@ Do you want Peek to open the external application? Title of the dialog showed when an URI is clicked,"Peek" is the name of the utility. + + ({1:N0} bytes) + Displays total number of bytes. Don't localize the "{1:N0}" part. + + + ({1:N0} byte) + Displays unit byte. Don't localize the "{1:N0}" part. + \ No newline at end of file