-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad3a2e2
commit 0b1ee0c
Showing
8 changed files
with
128 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/BlueUpdate/BlueUpdate Updater/CodeBits/ByteSizeFriendlyName.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* Documentation: http://codebits.codeplex.com/wikipage?title=ByteSizeFriendlyName */ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace CodeBits | ||
{ | ||
public static partial class ByteSizeFriendlyName | ||
{ | ||
public static string Build(long bytes, FriendlyNameOptions options = null) | ||
{ | ||
if (bytes < 0) | ||
throw new ArgumentOutOfRangeException("bytes", "bytes cannot be a negative value"); | ||
|
||
options = options ?? FriendlyNameOptions.Default; | ||
|
||
string units; | ||
double friendlySize = GetFriendlySize(bytes, out units); | ||
|
||
string sizeFormat = (options.GroupDigits ? "#,0." : "0.") + new string('#', options.DecimalPlaces); | ||
string size = friendlySize.ToString(sizeFormat); | ||
|
||
if (options.UnitDisplayMode == UnitDisplayMode.AlwaysDisplay) | ||
return string.Format("{0} {1}", size, units); | ||
if (options.UnitDisplayMode == UnitDisplayMode.AlwaysHide) | ||
return size; | ||
return bytes < 1024 ? size : string.Format("{0} {1}", size, units); | ||
} | ||
|
||
private static double GetFriendlySize(long bytes, out string units) | ||
{ | ||
foreach (KeyValuePair<double, string> byteMapping in ByteMappings) | ||
{ | ||
if (bytes >= byteMapping.Key) | ||
{ | ||
units = byteMapping.Value; | ||
return bytes / byteMapping.Key; | ||
} | ||
} | ||
units = bytes == 1 ? "byte" : "bytes"; | ||
return bytes; | ||
} | ||
|
||
private static readonly Dictionary<double, string> ByteMappings = new Dictionary<double, string> { | ||
{ Math.Pow(1024, 5), "PB" }, | ||
{ Math.Pow(1024, 4), "TB" }, | ||
{ Math.Pow(1024, 3), "GB" }, | ||
{ Math.Pow(1024, 2), "MB" }, | ||
{ Math.Pow(1024, 1), "KB" }, | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Options for generating the friendly name of a byte size. | ||
/// </summary> | ||
public sealed class FriendlyNameOptions | ||
{ | ||
/// <summary> | ||
/// Creates an instance of the FriendlyNameOptions class. | ||
/// </summary> | ||
public FriendlyNameOptions() | ||
{ | ||
DecimalPlaces = 2; | ||
UnitDisplayMode = UnitDisplayMode.AlwaysDisplay; | ||
} | ||
|
||
/// <summary> | ||
/// The number of decimal places to calculate for the friendly name size value. | ||
/// </summary> | ||
public int DecimalPlaces { get; set; } | ||
|
||
/// <summary> | ||
/// Specifies whether to group digits in the friendly name size value. | ||
/// </summary> | ||
public bool GroupDigits { get; set; } | ||
|
||
/// <summary> | ||
/// Specifies how the size unit is displayed in the friendly name. | ||
/// </summary> | ||
public UnitDisplayMode UnitDisplayMode { get; set; } | ||
|
||
/// <summary> | ||
/// Represents the default options value for generating a friendly name. | ||
/// </summary> | ||
public static readonly FriendlyNameOptions Default = new FriendlyNameOptions(); | ||
} | ||
|
||
/// <summary> | ||
/// Specifies how the size unit (KB, MB, etc) is displayed in the friendly name. | ||
/// </summary> | ||
public enum UnitDisplayMode | ||
{ | ||
/// <summary> | ||
/// Always display the size unit (the default). | ||
/// </summary> | ||
AlwaysDisplay, | ||
|
||
/// <summary> | ||
/// Always hide the size unit. | ||
/// </summary> | ||
AlwaysHide, | ||
|
||
/// <summary> | ||
/// Only display the size unit if the value is 1 KB or more. Never display for sizes less than that. | ||
/// </summary> | ||
HideOnlyForBytes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters