using Chronos.Core.Localization;
namespace Chronos.App.Infrastructure;
/// Culture-aware number, size and duration formatting for the UI.
public static class Formatting
{
/// U+00A0, which joins a number to its unit.
public const char NoBreakSpace = '\u00A0';
/// Localized "{0} KB" formats from bytes to petabytes (BRIEF §16: sizes follow the language, "96,2 Go").
private static readonly string[] ByteUnits = ["Common.Size.B", "Common.Size.KB", "Common.Size.MB", "Common.Size.GB", "Common.Size.TB", "Common.Size.PB"];
///
/// 1,536 → "1.5 KB" (decimal units, as Explorer shows for drives), the number and its unit held together by a no-break
/// space (U+00A0) so a wrapped or trimmed line never parts them (issue #28).
///
public static string Bytes(long bytes)
{
double value = bytes;
var unit = 0;
while (value >= 1000 && unit < ByteUnits.Length - 1)
{
value /= 1000;
unit++;
}
var format = unit == 0 || value >= 100 ? "0" : "0.#";
return Loc.L(ByteUnits[unit], value.ToString(format, Loc.Culture)).Replace(' ', NoBreakSpace);
}
public static string Count(long n) => n.ToString("N0", Loc.Culture);
/// Compact duration: "2 h 00 m", "5 min", "40 s".
public static string Duration(TimeSpan t)
{
if (t < TimeSpan.Zero) t = TimeSpan.Zero;
if (t.TotalHours >= 24) return Loc.L("Common.Duration.Days", (int)t.TotalDays, t.Hours);
if (t.TotalHours >= 1) return Loc.L("Common.Duration.Hours", (int)t.TotalHours, t.Minutes.ToString("00", Loc.Culture));
if (t.TotalMinutes >= 1) return Loc.L("Common.Duration.Minutes", (int)Math.Round(t.TotalMinutes));
return Loc.L("Common.Duration.Seconds", (int)Math.Round(t.TotalSeconds));
}
/// Clock-style elapsed time for finished jobs: "2:41", "1:02:41".
public static string Elapsed(TimeSpan t)
{
if (t < TimeSpan.Zero) t = TimeSpan.Zero;
return t.TotalHours >= 1
? ((int)t.TotalHours).ToString(Loc.Culture) + ":" + t.Minutes.ToString("00", Loc.Culture) + ":" + t.Seconds.ToString("00", Loc.Culture)
: ((int)t.TotalMinutes).ToString(Loc.Culture) + ":" + t.Seconds.ToString("00", Loc.Culture);
}
/// 0.62 → "62 %".
public static string Percent(double fraction) => Loc.L("Common.Percent", Math.Round(Math.Clamp(fraction, 0, 1) * 100).ToString("0", Loc.Culture));
/// Items per second with one decimal below 100: "11.3", "118".
public static string Rate(double perSecond) => perSecond.ToString(perSecond < 100 ? "0.#" : "N0", Loc.Culture);
/// Local time of day in the culture's short format ("09:12").
public static string TimeOfDay(DateTimeOffset at) => at.ToLocalTime().ToString("t", Loc.Culture);
/// Calendar date in the culture's short format.
public static string Date(DateTimeOffset at) => at.ToLocalTime().ToString("d", Loc.Culture);
/// Camera wall-clock date in the culture's short format.
public static string Date(DateTime wallClock) => wallClock.ToString("d", Loc.Culture);
///
/// A path, address or fingerprint kept left to right inside right-to-left text (a Unicode left-to-right embedding),
/// so its separators and digits stay in place; it reads unchanged in left-to-right languages.
///
public static string LeftToRight(string text) => "\u202A" + text + "\u202C";
}