From 82eb14b37e57a665e7077fb4754a8b3e6a8af6de Mon Sep 17 00:00:00 2001 From: Cory Todd Date: Sun, 22 Aug 2021 09:12:38 -0700 Subject: [PATCH] add file link to cli table output Add option to ctrl+click to open the repo from the console Improve performance by using a counter instead of enumerating the commits list twice. --- Walrus.CLI/Commands/QueryCommand.cs | 28 ++++++++++++++++++---------- Walrus.Core/WalrusCommit.cs | 16 +++++++++++++--- Walrus.Core/WalrusRepository.cs | 6 +++--- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/Walrus.CLI/Commands/QueryCommand.cs b/Walrus.CLI/Commands/QueryCommand.cs index 93b2da5..38c500d 100644 --- a/Walrus.CLI/Commands/QueryCommand.cs +++ b/Walrus.CLI/Commands/QueryCommand.cs @@ -77,10 +77,18 @@ private void HandleQuery(WalrusQuery query, bool printTable) .OrderBy(c => c.Timestamp) .AsEnumerable(); + // Avoid calling Count() on the commits enumerable because it is slow + int? commitCount = null; if (printTable) { - PrintTable(commits); + commitCount = PrintTable(commits); } + + commitCount ??= commits.Count(); + + Console.WriteLine(new string('=', Console.WindowWidth / 2)); + Console.WriteLine("Total Commits: {0}", commitCount); + Console.WriteLine(new string('=', Console.WindowWidth / 2)); } /// @@ -89,31 +97,31 @@ private void HandleQuery(WalrusQuery query, bool printTable) /// SHA and commit title are shown for each commit. /// /// Commits to print - private void PrintTable(IEnumerable commits) + /// Count of commits in commit + private int PrintTable(IEnumerable commits) { + var count = 0; var header = new string('-', Console.WindowWidth / 2); foreach (var groupRepo in commits.GroupBy(c => c.RepoName)) { - Console.WriteLine($"Repository: {groupRepo.Key}"); + Console.WriteLine($"Repository: {groupRepo.Key} [file://{groupRepo.First().RepoPath}]"); Console.WriteLine(header); foreach (var groupDay in groupRepo.GroupBy(g => g.Timestamp.Date)) { Console.WriteLine($"{groupDay.Key:d}: {groupDay.Count()} Commits"); - foreach(var commit in groupDay) + foreach (var commit in groupDay) { - Console.WriteLine($"\t{commit.Sha} {commit.Message}"); + Console.WriteLine($"\t{commit.Timestamp:HH:mm} {commit.Sha} {commit.Message}"); + ++count; } } - Console.WriteLine(); + Console.WriteLine(Environment.NewLine); } - var count = commits.Count(); - - Console.WriteLine(new string('=', Console.WindowWidth / 2)); - Console.WriteLine("Total Commits: {0}", count); + return count; } } } diff --git a/Walrus.Core/WalrusCommit.cs b/Walrus.Core/WalrusCommit.cs index 878b351..544cd86 100644 --- a/Walrus.Core/WalrusCommit.cs +++ b/Walrus.Core/WalrusCommit.cs @@ -2,24 +2,34 @@ { using LibGit2Sharp; using System; + using System.IO; + using Walrus.Core.Internal; /// /// TODO: do we want to wrap this type or use the raw LibGit2 type? /// public class WalrusCommit { + private readonly WalrusRepository _repository; private readonly Commit _commit; - public WalrusCommit(string repoName, Commit commit) + public WalrusCommit(WalrusRepository repository, Commit commit) { - RepoName = repoName; + Ensure.IsNotNull(nameof(repository), repository); + Ensure.IsNotNull(nameof(commit), commit); + + _repository = repository; _commit = commit; } + /// + /// Path to repository containing this commit5 + /// + public string RepoPath => _repository.RepositoryPath!; /// /// Name of Git repo this commit belongs to /// - public string RepoName { get; } + public string RepoName => _repository.RepositoryName!; /// /// Commit message text diff --git a/Walrus.Core/WalrusRepository.cs b/Walrus.Core/WalrusRepository.cs index 27d8ffb..860e21c 100644 --- a/Walrus.Core/WalrusRepository.cs +++ b/Walrus.Core/WalrusRepository.cs @@ -22,12 +22,12 @@ internal WalrusRepository(Repository repository) /// /// Name of folder containing Git repo /// - public string? RepositoryName => Path.GetFileName(RepositoryPath); + public string RepositoryName => Path.GetFileName(RepositoryPath); /// /// Absolute path to Git repo /// - public string? RepositoryPath => Path.GetDirectoryName(_repository?.Info?.WorkingDirectory); + public string RepositoryPath => Path.GetDirectoryName(_repository?.Info?.WorkingDirectory)!; /// /// Most recent commit message @@ -103,7 +103,7 @@ private IEnumerable SafeGitCommitEnumeration(IEnumerator c var commit = commitIter.Current; if (IsMatch(commit, query)) { - yield return new WalrusCommit(RepositoryName!, commit); + yield return new WalrusCommit(this, commit); } } while (true);