Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup StatusCharacterMap #74

Merged
merged 3 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/RepoM.Api/Git/RepositoryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ private void EnsureStatusCache()
return;
}

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

_cachedRepositoryStatusCode = repositoryStatusCode;
}
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 const char IDENTICAL_SIGN = '\u2261';

public static StatusCharacterMap Instance { get; } = new StatusCharacterMap();
public const char NO_UPSTREAM_SIGN = '\u2302';

public string IdenticalSign => "\u2261";
public const char ARROW_UP_SIGN = '\u2191';

public string NoUpstreamSign => "\u2302";
public const char ARROW_DOWN_SIGN = '\u2193';

public string ArrowUpSign => "\u2191";
public const char ELLIPSES_SIGN = '\u2026';

public string ArrowDownSign => "\u2193";

public string EllipsesSign => "\u2026";

public string StashSign => "\u205E";
public const char STASH_SIGN = '\u205E';
}
27 changes: 10 additions & 17 deletions src/RepoM.Api/Git/StatusCompressor.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
namespace RepoM.Api.Git;

using System;
using System.Text;

public class StatusCompressor
public static 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)
public static 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.IDENTICAL_SIGN);
}
else
{
if (isBehind)
{
builder.Append($"{_statusCharacterMap.ArrowDownSign}{repository.BehindBy}");
builder.Append($"{StatusCharacterMap.ARROW_DOWN_SIGN}{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.ARROW_UP_SIGN}{repository.AheadBy}");
}
}
}
else
{
builder.Append(_statusCharacterMap.NoUpstreamSign);
builder.Append(StatusCharacterMap.NO_UPSTREAM_SIGN);
}

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

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

return builder.ToString();
}

public string CompressWithBranch(Repository repository)
public static string CompressWithBranch(Repository repository)
{
var branch = repository.CurrentBranch;

Expand All @@ -112,7 +105,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.ELLIPSES_SIGN})";
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/RepoM.App/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +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);
Container.Register<IRepositoryDetectorFactory, DefaultRepositoryDetectorFactory>(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.IDENTICAL_SIGN,
StatusCharacterMap.STASH_SIGN,
StatusCharacterMap.IDENTICAL_SIGN,
StatusCharacterMap.ARROW_UP_SIGN,
StatusCharacterMap.ARROW_DOWN_SIGN,
StatusCharacterMap.NO_UPSTREAM_SIGN,
StatusCharacterMap.STASH_SIGN
);
}

Expand Down
44 changes: 11 additions & 33 deletions tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,37 @@
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;
private readonly RepositoryBuilder _builder = new();

public StatusCompressorTests()
private static string Compress(Repository repo)
{
_builder = new RepositoryBuilder();
_characterMap = StatusCharacterMap.Instance;
_compressor = new StatusCompressor(_characterMap);
return StatusCompressor.Compress(repo);
}

private string Compress(Repository repo)
private static string CompressWithBranch(Repository repo)
{
return _compressor.Compress(repo);
return StatusCompressor.CompressWithBranch(repo);
}

private string CompressWithBranch(Repository repo)
{
return _compressor.CompressWithBranch(repo);
}

private string Up => _characterMap.ArrowUpSign;
private static string Up => StatusCharacterMap.ARROW_UP_SIGN.ToString();

private string Down => _characterMap.ArrowDownSign;
private static string Down => StatusCharacterMap.ARROW_DOWN_SIGN.ToString();

private string Eq => _characterMap.IdenticalSign;
private static string Eq => StatusCharacterMap.IDENTICAL_SIGN.ToString();

private string NoUp => _characterMap.NoUpstreamSign;
private static string NoUp => StatusCharacterMap.NO_UPSTREAM_SIGN.ToString();

private string Ellipses => _characterMap.EllipsesSign;
private static string Ellipses => StatusCharacterMap.ELLIPSES_SIGN.ToString();

private string StashCount => _characterMap.StashSign;
private static string StashCount => StatusCharacterMap.STASH_SIGN.ToString();

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
Loading