Skip to content

Commit

Permalink
cleanup StatusCharacterMap
Browse files Browse the repository at this point in the history
  • Loading branch information
coenm committed Sep 10, 2023
1 parent e841860 commit 90bcd1c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/RepoM.Api/Git/RepositoryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private void EnsureStatusCache()
return;
}

var compressor = new StatusCompressor(StatusCharacterMap.Instance);
var compressor = new StatusCompressor();
_cachedRepositoryStatus = compressor.Compress(Repository);
_cachedRepositoryStatusWithBranch = compressor.CompressWithBranch(Repository);

Expand Down
20 changes: 7 additions & 13 deletions src/RepoM.Api/Git/StatusCharacterMap.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
namespace RepoM.Api.Git;

public class StatusCharacterMap
public static class StatusCharacterMap
{
private StatusCharacterMap()
{
}
public static string IdenticalSign => "\u2261";

public static StatusCharacterMap Instance { get; } = new StatusCharacterMap();
public static string NoUpstreamSign => "\u2302";

public string IdenticalSign => "\u2261";
public static string ArrowUpSign => "\u2191";

public string NoUpstreamSign => "\u2302";
public static string ArrowDownSign => "\u2193";

public string ArrowUpSign => "\u2191";
public static string EllipsesSign => "\u2026";

public string ArrowDownSign => "\u2193";

public string EllipsesSign => "\u2026";

public string StashSign => "\u205E";
public static string StashSign => "\u205E";
}
20 changes: 6 additions & 14 deletions src/RepoM.Api/Git/StatusCompressor.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
namespace RepoM.Api.Git;

using System;
using System.Text;

public class StatusCompressor
{
private const int COMMIT_SHA_DISPLAY_CHARS = 7;

private readonly StatusCharacterMap _statusCharacterMap;

public StatusCompressor(StatusCharacterMap statusCharacterMap)
{
_statusCharacterMap = statusCharacterMap ?? throw new ArgumentNullException(nameof(statusCharacterMap));
}

public string Compress(Repository repository)
{
if (string.IsNullOrEmpty(repository.CurrentBranch))
Expand All @@ -35,13 +27,13 @@ public string Compress(Repository repository)
{
if (isOnCommitLevel)
{
builder.Append(_statusCharacterMap.IdenticalSign);
builder.Append(StatusCharacterMap.IdenticalSign);
}
else
{
if (isBehind)
{
builder.Append($"{_statusCharacterMap.ArrowDownSign}{repository.BehindBy}");
builder.Append($"{StatusCharacterMap.ArrowDownSign}{repository.BehindBy}");
}

if (isAhead)
Expand All @@ -51,13 +43,13 @@ public string Compress(Repository repository)
builder.Append(' ');
}

builder.Append($"{_statusCharacterMap.ArrowUpSign}{repository.AheadBy}");
builder.Append($"{StatusCharacterMap.ArrowUpSign}{repository.AheadBy}");
}
}
}
else
{
builder.Append(_statusCharacterMap.NoUpstreamSign);
builder.Append(StatusCharacterMap.NoUpstreamSign);
}

if (printAddStagedRemoved)
Expand Down Expand Up @@ -92,7 +84,7 @@ public string Compress(Repository repository)
builder.Append(' ');
}

builder.Append(_statusCharacterMap.StashSign + repository.StashCount);
builder.Append(StatusCharacterMap.StashSign + repository.StashCount);
}

return builder.ToString();
Expand All @@ -112,7 +104,7 @@ public string CompressWithBranch(Repository repository)
// put commit shas in parenthesis (), shorten them and show ellipses afterwards
if (repository.CurrentBranchIsDetached && branch.Length > COMMIT_SHA_DISPLAY_CHARS)
{
branch = $"({branch[..COMMIT_SHA_DISPLAY_CHARS]}{_statusCharacterMap.EllipsesSign})";
branch = $"({branch[..COMMIT_SHA_DISPLAY_CHARS]}{StatusCharacterMap.EllipsesSign})";
}
}

Expand Down
1 change: 0 additions & 1 deletion src/RepoM.App/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public static void RegisterServices(IFileSystem fileSystem)
{
Container.RegisterInstance<ObjectCache>(MemoryCache.Default);
Container.Register<MainWindow>(Lifestyle.Singleton);
Container.RegisterInstance(StatusCharacterMap.Instance);
Container.Register<StatusCompressor>(Lifestyle.Singleton);
Container.Register<IRepositoryInformationAggregator, DefaultRepositoryInformationAggregator>(Lifestyle.Singleton);
Container.Register<IRepositoryMonitor, DefaultRepositoryMonitor>(Lifestyle.Singleton);
Expand Down
19 changes: 9 additions & 10 deletions src/RepoM.App/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public partial class MainWindow
private readonly IAppDataPathProvider _appDataPathProvider;

public MainWindow(
StatusCharacterMap statusCharacterMap,
IRepositoryInformationAggregator aggregator,
IRepositoryMonitor repositoryMonitor,
IRepositoryActionProvider repositoryActionProvider,
Expand Down Expand Up @@ -101,7 +100,7 @@ public MainWindow(

AssemblyName? appName = Assembly.GetEntryAssembly()?.GetName();
txtHelpCaption.Text = appName?.Name + " " + appName?.Version?.ToString(2);
txtHelp.Text = GetHelp(statusCharacterMap);
txtHelp.Text = GetHelp();

PlaceFormByTaskBarLocation();
}
Expand Down Expand Up @@ -638,17 +637,17 @@ private void TxtFilter_Finish(object sender, EventArgs e)
item?.Focus();
}

private string GetHelp(StatusCharacterMap statusCharacterMap)
private string GetHelp()
{
return _translationService.Translate(
"Help Detail",
statusCharacterMap.IdenticalSign,
statusCharacterMap.StashSign,
statusCharacterMap.IdenticalSign,
statusCharacterMap.ArrowUpSign,
statusCharacterMap.ArrowDownSign,
statusCharacterMap.NoUpstreamSign,
statusCharacterMap.StashSign
StatusCharacterMap.IdenticalSign,
StatusCharacterMap.StashSign,
StatusCharacterMap.IdenticalSign,
StatusCharacterMap.ArrowUpSign,
StatusCharacterMap.ArrowDownSign,
StatusCharacterMap.NoUpstreamSign,
StatusCharacterMap.StashSign
);
}

Expand Down
37 changes: 8 additions & 29 deletions tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
namespace RepoM.Api.Tests.Git;

using System;
using FluentAssertions;
using RepoM.Api.Git;
using Xunit;

public class StatusCompressorTests
{
private readonly RepositoryBuilder _builder;
private readonly StatusCharacterMap _characterMap;
private readonly StatusCompressor _compressor;

public StatusCompressorTests()
{
_builder = new RepositoryBuilder();
_characterMap = StatusCharacterMap.Instance;
_compressor = new StatusCompressor(_characterMap);
}
private readonly RepositoryBuilder _builder = new();
private readonly StatusCompressor _compressor = new();

private string Compress(Repository repo)
{
Expand All @@ -28,32 +19,20 @@ private string CompressWithBranch(Repository repo)
return _compressor.CompressWithBranch(repo);
}

private string Up => _characterMap.ArrowUpSign;
private string Up => StatusCharacterMap.ArrowUpSign;

private string Down => _characterMap.ArrowDownSign;
private string Down => StatusCharacterMap.ArrowDownSign;

private string Eq => _characterMap.IdenticalSign;
private string Eq => StatusCharacterMap.IdenticalSign;

private string NoUp => _characterMap.NoUpstreamSign;
private string NoUp => StatusCharacterMap.NoUpstreamSign;

private string Ellipses => _characterMap.EllipsesSign;
private string Ellipses => StatusCharacterMap.EllipsesSign;

private string StashCount => _characterMap.StashSign;
private string StashCount => StatusCharacterMap.StashSign;

public class CompressMethod : StatusCompressorTests
{
[Fact]
public void Ctor_ShouldThrow_WhenArgumentIsNull()
{
// arrange

// act
Action act = () => _ = new StatusCompressor(null!);

// asset
act.Should().Throw<ArgumentNullException>();
}

[Fact]
public void Returns_Empty_String_For_Empty_Repositories()
{
Expand Down

0 comments on commit 90bcd1c

Please sign in to comment.