Files
Chronos/src/Chronos.App/Infrastructure/Formatting.cs
T
Alby96andClaude Opus 5.5 5a6efbb72f feat(videos): board of published and workspace renders; card line starts with the preset
The Videos section and the admin Videos page gain a Board view with two
lanes, newest first, each with a count and the list's filters: Published
(library and server renders, including those sent from a workspace) and
Workspace (workspace exports, unsent ones plus sent ones marked Sent).
Cards show the sequence, project and version when known, preset, kind,
date and state chips, and reuse the existing actions; no new store.

Video cards now start with the preset ("4K archive · HEVC · 0:41 · 1.4 GB")
and Formatting.Bytes keeps number and unit together with a no-break space
(#28). The capture tool adds the videos-board scene in both themes.

Refs #29

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 17:13:26 +01:00

73 lines
3.6 KiB
C#

using Chronos.Core.Localization;
namespace Chronos.App.Infrastructure;
/// <summary>Culture-aware number, size and duration formatting for the UI.</summary>
public static class Formatting
{
/// <summary>U+00A0, which joins a number to its unit.</summary>
public const char NoBreakSpace = '\u00A0';
/// <summary>Localized "{0} KB" formats from bytes to petabytes (BRIEF §16: sizes follow the language, "96,2 Go").</summary>
private static readonly string[] ByteUnits = ["Common.Size.B", "Common.Size.KB", "Common.Size.MB", "Common.Size.GB", "Common.Size.TB", "Common.Size.PB"];
/// <summary>
/// 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).
/// </summary>
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);
/// <summary>Compact duration: "2 h 00 m", "5 min", "40 s".</summary>
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));
}
/// <summary>Clock-style elapsed time for finished jobs: "2:41", "1:02:41".</summary>
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);
}
/// <summary>0.62 → "62 %".</summary>
public static string Percent(double fraction) => Loc.L("Common.Percent", Math.Round(Math.Clamp(fraction, 0, 1) * 100).ToString("0", Loc.Culture));
/// <summary>Items per second with one decimal below 100: "11.3", "118".</summary>
public static string Rate(double perSecond) => perSecond.ToString(perSecond < 100 ? "0.#" : "N0", Loc.Culture);
/// <summary>Local time of day in the culture's short format ("09:12").</summary>
public static string TimeOfDay(DateTimeOffset at) => at.ToLocalTime().ToString("t", Loc.Culture);
/// <summary>Calendar date in the culture's short format.</summary>
public static string Date(DateTimeOffset at) => at.ToLocalTime().ToString("d", Loc.Culture);
/// <summary>Camera wall-clock date in the culture's short format.</summary>
public static string Date(DateTime wallClock) => wallClock.ToString("d", Loc.Culture);
/// <summary>
/// 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.
/// </summary>
public static string LeftToRight(string text) => "\u202A" + text + "\u202C";
}