Skip to content

Commit

Permalink
[Peek]Fix file and folder sizes to be similar to Explorer (microsoft#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Deepak-Sangle committed Oct 3, 2023
1 parent 5e2733e commit 7f8e907
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 26 deletions.
1 change: 0 additions & 1 deletion .github/actions/spell-check/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,6 @@ pef
PElems
Pels
PERCEIVEDFLAG
Percision
perfmon
pesi
petabyte
Expand Down
8 changes: 8 additions & 0 deletions src/modules/peek/Peek.Common/Helpers/MathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -10,5 +13,10 @@ public static int Modulo(int a, int b)
{
return a < 0 ? ((a % b) + b) % b : a % b;
}

public static int NumberOfDigits(int num)
{
return Math.Abs(num).ToString(CultureInfo.InvariantCulture).Length;
}
}
}
67 changes: 50 additions & 17 deletions src/modules/peek/Peek.Common/Helpers/ReadableStringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

if (index > 0 && number >= Math.Pow(10, MaxDigitsToDisplay))
{
index++;
number = bytes / Math.Pow(PowerFactor, index);
}

return string.Format(CultureInfo.InvariantCulture, format[index], number);
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)
Expand All @@ -55,5 +61,32 @@ public static string FormatResourceString(string resourceId, object? args0, obje

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];
}
}
}
24 changes: 16 additions & 8 deletions src/modules/peek/Peek.UI/Strings/en-us/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down Expand Up @@ -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">
Expand All @@ -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>

0 comments on commit 7f8e907

Please sign in to comment.