From 90bcd1cfc0c5d28857ee90e453bf3d686d525d8a Mon Sep 17 00:00:00 2001 From: Coen van den Munckhof Date: Sun, 10 Sep 2023 11:02:03 +0200 Subject: [PATCH 1/3] cleanup StatusCharacterMap --- src/RepoM.Api/Git/RepositoryViewModel.cs | 2 +- src/RepoM.Api/Git/StatusCharacterMap.cs | 20 ++++------ src/RepoM.Api/Git/StatusCompressor.cs | 20 +++------- src/RepoM.App/Bootstrapper.cs | 1 - src/RepoM.App/MainWindow.xaml.cs | 19 +++++----- .../Git/StatusCompressorTests.cs | 37 ++++--------------- 6 files changed, 31 insertions(+), 68 deletions(-) diff --git a/src/RepoM.Api/Git/RepositoryViewModel.cs b/src/RepoM.Api/Git/RepositoryViewModel.cs index f9dba708..d8fd5bf5 100644 --- a/src/RepoM.Api/Git/RepositoryViewModel.cs +++ b/src/RepoM.Api/Git/RepositoryViewModel.cs @@ -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); diff --git a/src/RepoM.Api/Git/StatusCharacterMap.cs b/src/RepoM.Api/Git/StatusCharacterMap.cs index 70abb5c7..a0e64bcd 100644 --- a/src/RepoM.Api/Git/StatusCharacterMap.cs +++ b/src/RepoM.Api/Git/StatusCharacterMap.cs @@ -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"; } \ No newline at end of file diff --git a/src/RepoM.Api/Git/StatusCompressor.cs b/src/RepoM.Api/Git/StatusCompressor.cs index 6ec1d3b8..e3e66d1e 100644 --- a/src/RepoM.Api/Git/StatusCompressor.cs +++ b/src/RepoM.Api/Git/StatusCompressor.cs @@ -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)) @@ -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) @@ -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) @@ -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(); @@ -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})"; } } diff --git a/src/RepoM.App/Bootstrapper.cs b/src/RepoM.App/Bootstrapper.cs index caac53dd..9d9f71ee 100644 --- a/src/RepoM.App/Bootstrapper.cs +++ b/src/RepoM.App/Bootstrapper.cs @@ -58,7 +58,6 @@ public static void RegisterServices(IFileSystem fileSystem) { Container.RegisterInstance(MemoryCache.Default); Container.Register(Lifestyle.Singleton); - Container.RegisterInstance(StatusCharacterMap.Instance); Container.Register(Lifestyle.Singleton); Container.Register(Lifestyle.Singleton); Container.Register(Lifestyle.Singleton); diff --git a/src/RepoM.App/MainWindow.xaml.cs b/src/RepoM.App/MainWindow.xaml.cs index 4d63b62b..db34f373 100644 --- a/src/RepoM.App/MainWindow.xaml.cs +++ b/src/RepoM.App/MainWindow.xaml.cs @@ -45,7 +45,6 @@ public partial class MainWindow private readonly IAppDataPathProvider _appDataPathProvider; public MainWindow( - StatusCharacterMap statusCharacterMap, IRepositoryInformationAggregator aggregator, IRepositoryMonitor repositoryMonitor, IRepositoryActionProvider repositoryActionProvider, @@ -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(); } @@ -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 ); } diff --git a/tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs b/tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs index 40d8531e..e7b2b672 100644 --- a/tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs +++ b/tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs @@ -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) { @@ -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(); - } - [Fact] public void Returns_Empty_String_For_Empty_Repositories() { From cf7bbad1ec6fbbb41fc2af8a855add0bc59637da Mon Sep 17 00:00:00 2001 From: Coen van den Munckhof Date: Sun, 10 Sep 2023 17:41:42 +0200 Subject: [PATCH 2/3] StatusCompressor static, values const instead of static --- src/RepoM.Api/Git/RepositoryViewModel.cs | 5 ++--- src/RepoM.Api/Git/StatusCharacterMap.cs | 12 +++++------ src/RepoM.Api/Git/StatusCompressor.cs | 19 +++++++++-------- src/RepoM.App/Bootstrapper.cs | 1 - src/RepoM.App/MainWindow.xaml.cs | 14 ++++++------- .../Git/StatusCompressorTests.cs | 21 +++++++++---------- 6 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/RepoM.Api/Git/RepositoryViewModel.cs b/src/RepoM.Api/Git/RepositoryViewModel.cs index d8fd5bf5..abb0e9ee 100644 --- a/src/RepoM.Api/Git/RepositoryViewModel.cs +++ b/src/RepoM.Api/Git/RepositoryViewModel.cs @@ -46,9 +46,8 @@ private void EnsureStatusCache() return; } - var compressor = new StatusCompressor(); - _cachedRepositoryStatus = compressor.Compress(Repository); - _cachedRepositoryStatusWithBranch = compressor.CompressWithBranch(Repository); + _cachedRepositoryStatus = StatusCompressor.Compress(Repository); + _cachedRepositoryStatusWithBranch = StatusCompressor.CompressWithBranch(Repository); _cachedRepositoryStatusCode = repositoryStatusCode; } diff --git a/src/RepoM.Api/Git/StatusCharacterMap.cs b/src/RepoM.Api/Git/StatusCharacterMap.cs index a0e64bcd..ba352f30 100644 --- a/src/RepoM.Api/Git/StatusCharacterMap.cs +++ b/src/RepoM.Api/Git/StatusCharacterMap.cs @@ -2,15 +2,15 @@ namespace RepoM.Api.Git; public static class StatusCharacterMap { - public static string IdenticalSign => "\u2261"; + public const char IDENTICAL_SIGN = '\u2261'; - public static string NoUpstreamSign => "\u2302"; + public const char NO_UPSTREAM_SIGN = '\u2302'; - public static string ArrowUpSign => "\u2191"; + public const char ARROW_UP_SIGN = '\u2191'; - public static string ArrowDownSign => "\u2193"; + public const char ARROW_DOWN_SIGN = '\u2193'; - public static string EllipsesSign => "\u2026"; + public const char ELLIPSES_SIGN = '\u2026'; - public static string StashSign => "\u205E"; + public const char STASH_SIGN = '\u205E'; } \ No newline at end of file diff --git a/src/RepoM.Api/Git/StatusCompressor.cs b/src/RepoM.Api/Git/StatusCompressor.cs index e3e66d1e..7667788c 100644 --- a/src/RepoM.Api/Git/StatusCompressor.cs +++ b/src/RepoM.Api/Git/StatusCompressor.cs @@ -2,11 +2,11 @@ namespace RepoM.Api.Git; using System.Text; -public class StatusCompressor +public static class StatusCompressor { private const int COMMIT_SHA_DISPLAY_CHARS = 7; - public string Compress(Repository repository) + public static string Compress(Repository repository) { if (string.IsNullOrEmpty(repository.CurrentBranch)) { @@ -27,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) @@ -43,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) @@ -84,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; @@ -104,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})"; } } diff --git a/src/RepoM.App/Bootstrapper.cs b/src/RepoM.App/Bootstrapper.cs index 9d9f71ee..1590cd82 100644 --- a/src/RepoM.App/Bootstrapper.cs +++ b/src/RepoM.App/Bootstrapper.cs @@ -58,7 +58,6 @@ public static void RegisterServices(IFileSystem fileSystem) { Container.RegisterInstance(MemoryCache.Default); Container.Register(Lifestyle.Singleton); - Container.Register(Lifestyle.Singleton); Container.Register(Lifestyle.Singleton); Container.Register(Lifestyle.Singleton); Container.Register(Lifestyle.Singleton); diff --git a/src/RepoM.App/MainWindow.xaml.cs b/src/RepoM.App/MainWindow.xaml.cs index db34f373..b05798f6 100644 --- a/src/RepoM.App/MainWindow.xaml.cs +++ b/src/RepoM.App/MainWindow.xaml.cs @@ -641,13 +641,13 @@ 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 ); } diff --git a/tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs b/tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs index e7b2b672..0b8aa9fc 100644 --- a/tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs +++ b/tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs @@ -7,29 +7,28 @@ namespace RepoM.Api.Tests.Git; public class StatusCompressorTests { private readonly RepositoryBuilder _builder = new(); - private readonly StatusCompressor _compressor = new(); - private string Compress(Repository repo) + private static string Compress(Repository repo) { - return _compressor.Compress(repo); + return StatusCompressor.Compress(repo); } - private string CompressWithBranch(Repository repo) + private static string CompressWithBranch(Repository repo) { - return _compressor.CompressWithBranch(repo); + return StatusCompressor.CompressWithBranch(repo); } - private string Up => StatusCharacterMap.ArrowUpSign; + private string Up => StatusCharacterMap.ARROW_UP_SIGN.ToString(); - private string Down => StatusCharacterMap.ArrowDownSign; + private string Down => StatusCharacterMap.ARROW_DOWN_SIGN.ToString(); - private string Eq => StatusCharacterMap.IdenticalSign; + private string Eq => StatusCharacterMap.IDENTICAL_SIGN.ToString(); - private string NoUp => StatusCharacterMap.NoUpstreamSign; + private string NoUp => StatusCharacterMap.NO_UPSTREAM_SIGN.ToString(); - private string Ellipses => StatusCharacterMap.EllipsesSign; + private string Ellipses => StatusCharacterMap.ELLIPSES_SIGN.ToString(); - private string StashCount => StatusCharacterMap.StashSign; + private string StashCount => StatusCharacterMap.STASH_SIGN.ToString(); public class CompressMethod : StatusCompressorTests { From be8fce3da96f43ddab6bc772d3e9b86196226c87 Mon Sep 17 00:00:00 2001 From: Coen van den Munckhof Date: Sun, 10 Sep 2023 17:59:06 +0200 Subject: [PATCH 3/3] .. --- tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs b/tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs index 0b8aa9fc..02b0e9a7 100644 --- a/tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs +++ b/tests/RepoM.Api.Tests/Git/StatusCompressorTests.cs @@ -18,17 +18,17 @@ private static string CompressWithBranch(Repository repo) return StatusCompressor.CompressWithBranch(repo); } - private string Up => StatusCharacterMap.ARROW_UP_SIGN.ToString(); + private static string Up => StatusCharacterMap.ARROW_UP_SIGN.ToString(); - private string Down => StatusCharacterMap.ARROW_DOWN_SIGN.ToString(); + private static string Down => StatusCharacterMap.ARROW_DOWN_SIGN.ToString(); - private string Eq => StatusCharacterMap.IDENTICAL_SIGN.ToString(); + private static string Eq => StatusCharacterMap.IDENTICAL_SIGN.ToString(); - private string NoUp => StatusCharacterMap.NO_UPSTREAM_SIGN.ToString(); + private static string NoUp => StatusCharacterMap.NO_UPSTREAM_SIGN.ToString(); - private string Ellipses => StatusCharacterMap.ELLIPSES_SIGN.ToString(); + private static string Ellipses => StatusCharacterMap.ELLIPSES_SIGN.ToString(); - private string StashCount => StatusCharacterMap.STASH_SIGN.ToString(); + private static string StashCount => StatusCharacterMap.STASH_SIGN.ToString(); public class CompressMethod : StatusCompressorTests {