Container size scan, improvements

This commit is contained in:
2023-08-03 00:00:45 +02:00
parent 1713973c3a
commit f4d361f767
57 changed files with 814 additions and 532 deletions

View File

@@ -1,46 +1,29 @@
using System.Globalization;
using Avalonia.Data.Converters;
using ByteSizeLib;
namespace FileTime.GuiApp.Converters;
public class FormatSizeConverter : IValueConverter
{
private const long OneKiloByte = 1024;
private const long OneMegaByte = OneKiloByte * 1024;
private const long OneGigaByte = OneMegaByte * 1024;
private const long OneTerraByte = OneGigaByte * 1024;
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return (value, int.TryParse(parameter?.ToString(), out var prec)) switch
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
(value, int.TryParse(parameter?.ToString(), out var prec)) switch
{
(long size, true) => ToSizeString(size, prec),
(long size, false) => ToSizeString(size),
(null, _) => "...",
_ => value
};
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public static string ToSizeString(long fileSize, int precision = 1)
public static string ToSizeString(long fileSize, int? precision = null)
{
var fileSizeD = (decimal)fileSize;
var (size, suffix) = fileSize switch
{
> OneTerraByte => (fileSizeD / OneTerraByte, "T"),
> OneGigaByte => (fileSizeD / OneGigaByte, "G"),
> OneMegaByte => (fileSizeD / OneMegaByte, "M"),
> OneKiloByte => (fileSizeD / OneKiloByte, "K"),
_ => (fileSizeD, "B")
};
var result = string.Format("{0:N" + precision + "}", size).Replace(',', '.');
if (result.Contains('.')) result = result.TrimEnd('0').TrimEnd('.');
return result + " " + suffix;
var size = new ByteSize(fileSize);
return precision == null? size.ToString()
: size.ToString("0." + new string('#', precision.Value));
}
}