[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
This commit is contained in:
Deepak Sangle
2023-10-03 21:02:34 +05:30
committed by GitHub
parent 5e2733ece9
commit 7f8e9072c9
4 changed files with 74 additions and 26 deletions

View File

@@ -1370,7 +1370,6 @@ pef
PElems
Pels
PERCEIVEDFLAG
Percision
perfmon
pesi
petabyte

View File

@@ -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;
}
}
}

View File

@@ -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<string> format = new List<string>
{
(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<string> format = new List<string>
{
(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];
}
}
}

View File

@@ -154,31 +154,31 @@
<comment>Date Modified label for the unsupported files view. {0} is the date.</comment>
</data>
<data name="ReadableString_ByteAbbreviationFormat" xml:space="preserve">
<value>{0} byte</value>
<value>byte</value>
<comment>Abbreviation for the size unit byte.</comment>
</data>
<data name="ReadableString_KiloByteAbbreviationFormat" xml:space="preserve">
<value>{0} KB</value>
<value>KB</value>
<comment>Abbreviation for the size unit kilobyte.</comment>
</data>
<data name="ReadableString_MegaByteAbbreviationFormat" xml:space="preserve">
<value>{0} MB</value>
<value>MB</value>
<comment>Abbreviation for the size unit megabyte.</comment>
</data>
<data name="ReadableString_GigaByteAbbreviationFormat" xml:space="preserve">
<value>{0} GB</value>
<value>GB</value>
<comment>Abbreviation for the size unit gigabyte.</comment>
</data>
<data name="ReadableString_TeraByteAbbreviationFormat" xml:space="preserve">
<value>{0} TB</value>
<value>TB</value>
<comment>Abbreviation for the size unit terabyte.</comment>
</data>
<data name="ReadableString_PetaByteAbbreviationFormat" xml:space="preserve">
<value>{0} PB</value>
<value>PB</value>
<comment>Abbreviation for the size unit petabyte.</comment>
</data>
<data name="ReadableString_ExaByteAbbreviationFormat" xml:space="preserve">
<value>{0} EB</value>
<value>EB</value>
<comment>Abbreviation for the size unit exabyte.</comment>
</data>
<data name="PreviewTooltip_FileName" xml:space="preserve">
@@ -234,7 +234,7 @@
<comment>{0} is the size of the archive, {1} is the extracted size</comment>
</data>
<data name="ReadableString_BytesAbbreviationFormat" xml:space="preserve">
<value>{0} bytes</value>
<value>bytes</value>
<comment>Abbreviation for the size bytes</comment>
</data>
<data name="OpenUriDialog.CloseButtonText" xml:space="preserve">
@@ -253,4 +253,12 @@
<value>Do you want Peek to open the external application?</value>
<comment>Title of the dialog showed when an URI is clicked,"Peek" is the name of the utility. </comment>
</data>
<data name="ReadableString_BytesString" xml:space="preserve">
<value> ({1:N0} bytes)</value>
<comment>Displays total number of bytes. Don't localize the "{1:N0}" part.</comment>
</data>
<data name="ReadableString_ByteString" xml:space="preserve">
<value> ({1:N0} byte)</value>
<comment>Displays unit byte. Don't localize the "{1:N0}" part.</comment>
</data>
</root>