Skip to content

Commit

Permalink
Introduced UserOutputService for output with user settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-17 committed Oct 20, 2024
1 parent abb5443 commit 8f3689a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Gibbon.Git.Server.Tests/Services/UserOutputServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Gibbon.Git.Server.Services;

namespace Gibbon.Git.Server.Tests.Services;

[TestClass]
public class UserOutputServiceTests
{
private IUserOutputService _userOutputService = null!;

[TestInitialize]
public void Init()
{
_userOutputService = new UserOutputService();
}

[DataTestMethod]
[DataRow(0, "0 B")]
[DataRow(2, "2 B")]
[DataRow(512, "512 B")]
[DataRow(100, "100 B")]
[DataRow(2000, "1,95 kB")]
[DataRow(1024, "1 kB")]
[DataRow(1500, "1,46 kB")]
[DataRow(2000000, "1,91 MB")]
[DataRow(1048576, "1 MB")]
[DataRow(2000000000, "1,86 GB")]
[DataRow(1073741824, "1 GB")]
[DataRow(2000000000000, "1,82 TB")]
[DataRow(2000000000000000, "1819 TB")]
[DataRow(-2000000, "1,91 MB")]
[DataRow(999, "999 B")]
[DataRow(10240, "10 kB")]
[DataRow(1023999, "1000 kB")]
[DataRow(1099511627776, "1 TB")]
public void TestGetFileSizeString(long size, string expected)
{
Assert.AreEqual(expected, _userOutputService.GetFileSizeString(size));
}
}
1 change: 1 addition & 0 deletions Gibbon.Git.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ DbContextOptionsBuilder ConfigureOptions(DbContextOptionsBuilder options)
services.AddScoped<IRepositoryBrowserFactory, RepositoryBrowserFactory>();
services.AddScoped<IAuthorizationHandler, RepositoryPermissionHandler>();

services.AddScoped<IUserOutputService, UserOutputService>();
services.AddTransient<IRepositoryBrowser, RepositoryBrowser>();

services.AddTransient<IStartupService, RepositoryStartupService>();
Expand Down
42 changes: 42 additions & 0 deletions Gibbon.Git.Server/Services/UserOutputService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace Gibbon.Git.Server.Services;

public interface IUserOutputService
{
/// <summary>
/// <para>Returns the human-readable file size for an arbitrary, 64-bit file size</para>
/// <para>The default format is "0.### XB", e.g. "4.2 KB" or "1.434 GB"</para>
/// </summary>
string GetFileSizeString(long size);
}

public class UserOutputService : IUserOutputService
{
/// <summary>
/// <para>Returns the human-readable file size for an arbitrary, 64-bit file size</para>
/// <para>The default format is "0.### XB", e.g. "4.2 KB" or "1.434 GB"</para>
/// </summary>
public string GetFileSizeString(long size)
{
if (size == 0)
{
return "0 B";
}

string[] suffixes = ["B", "kB", "MB", "GB", "TB"];
var readable = Math.Abs((double)size);
var suffixIndex = 0;

while (readable >= 1024 && suffixIndex < suffixes.Length - 1)
{
readable /= 1024;
suffixIndex++;
}

return readable switch
{
>= 100 => $"{readable:0} {suffixes[suffixIndex]}",
> 10 => $"{readable:0.#} {suffixes[suffixIndex]}",
_ => $"{readable:0.##} {suffixes[suffixIndex]}"
};
}
}

0 comments on commit 8f3689a

Please sign in to comment.