mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
Add enhancements to progress widget
* Adds TransferSpeedColumn * Adds DownloadedColumn * Adds ElapsedTimeColumn * Minor enhancements to existing columns
This commit is contained in:

committed by
Patrik Svensson

parent
d87d8e4422
commit
07db28bb6f
89
src/Spectre.Console/Internal/FileSize.cs
Normal file
89
src/Spectre.Console/Internal/FileSize.cs
Normal file
@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal struct FileSize
|
||||
{
|
||||
public double Bytes { get; }
|
||||
public FileSizeUnit Unit { get; }
|
||||
public string Suffix => GetSuffix();
|
||||
|
||||
public FileSize(double bytes)
|
||||
{
|
||||
Bytes = bytes;
|
||||
Unit = Detect(bytes);
|
||||
}
|
||||
|
||||
public FileSize(double bytes, FileSizeUnit unit)
|
||||
{
|
||||
Bytes = bytes;
|
||||
Unit = unit;
|
||||
}
|
||||
|
||||
public string Format(CultureInfo? culture = null)
|
||||
{
|
||||
var @base = GetBase(Unit);
|
||||
if (@base == 0)
|
||||
{
|
||||
@base = 1;
|
||||
}
|
||||
|
||||
var bytes = Bytes / @base;
|
||||
|
||||
return Unit == FileSizeUnit.Bytes
|
||||
? ((int)bytes).ToString(culture ?? CultureInfo.InvariantCulture)
|
||||
: bytes.ToString("F1", culture ?? CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return ToString(suffix: true, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public string ToString(bool suffix = true, CultureInfo? culture = null)
|
||||
{
|
||||
if (suffix)
|
||||
{
|
||||
return $"{Format(culture)} {Suffix}";
|
||||
}
|
||||
|
||||
return Format(culture);
|
||||
}
|
||||
|
||||
private string GetSuffix()
|
||||
{
|
||||
return (Bytes, Unit) switch
|
||||
{
|
||||
(_, FileSizeUnit.KiloByte) => "KB",
|
||||
(_, FileSizeUnit.MegaByte) => "MB",
|
||||
(_, FileSizeUnit.GigaByte) => "GB",
|
||||
(_, FileSizeUnit.TeraByte) => "TB",
|
||||
(_, FileSizeUnit.PetaByte) => "PB",
|
||||
(_, FileSizeUnit.ExaByte) => "EB",
|
||||
(_, FileSizeUnit.ZettaByte) => "ZB",
|
||||
(_, FileSizeUnit.YottaByte) => "YB",
|
||||
(1, _) => "byte",
|
||||
(_, _) => "bytes",
|
||||
};
|
||||
}
|
||||
|
||||
private static FileSizeUnit Detect(double bytes)
|
||||
{
|
||||
foreach (var unit in (FileSizeUnit[])Enum.GetValues(typeof(FileSizeUnit)))
|
||||
{
|
||||
if (bytes < (GetBase(unit) * 1024))
|
||||
{
|
||||
return unit;
|
||||
}
|
||||
}
|
||||
|
||||
return FileSizeUnit.Bytes;
|
||||
}
|
||||
|
||||
private static double GetBase(FileSizeUnit unit)
|
||||
{
|
||||
return Math.Pow(1024, (int)unit);
|
||||
}
|
||||
}
|
||||
}
|
17
src/Spectre.Console/Internal/FileSizeUnit.cs
Normal file
17
src/Spectre.Console/Internal/FileSizeUnit.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal enum FileSizeUnit
|
||||
{
|
||||
Bytes = 0,
|
||||
KiloByte = 1,
|
||||
MegaByte = 2,
|
||||
GigaByte = 3,
|
||||
TeraByte = 4,
|
||||
PetaByte = 5,
|
||||
ExaByte = 6,
|
||||
ZettaByte = 7,
|
||||
YottaByte = 8,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user