From db2b10d2a48a3a21673c8f3bed924ebea9265e6d Mon Sep 17 00:00:00 2001 From: Mbucari <37587114+Mbucari@users.noreply.github.com> Date: Sun, 2 Jul 2023 22:23:29 -0600 Subject: [PATCH] Performance improvement --- Source/LibationCli/ConsoleProgressBar.cs | 7 +-- Source/LibationCli/HelpVerb.cs | 20 +++--- Source/LibationCli/Options/SearchOptions.cs | 67 +++++++++++---------- 3 files changed, 47 insertions(+), 47 deletions(-) diff --git a/Source/LibationCli/ConsoleProgressBar.cs b/Source/LibationCli/ConsoleProgressBar.cs index 7369f427..44231680 100644 --- a/Source/LibationCli/ConsoleProgressBar.cs +++ b/Source/LibationCli/ConsoleProgressBar.cs @@ -30,7 +30,6 @@ internal class ConsoleProgressBar } } - private double m_Progress; private TimeSpan m_RemainingTime; private int m_LastWriteLength = 0; @@ -71,7 +70,7 @@ internal class ConsoleProgressBar public void Clear() => Output.Write( - new string('\b', m_LastWriteLength) - + new string(' ', m_LastWriteLength) - + new string('\b', m_LastWriteLength)); + new string('\b', m_LastWriteLength) + + new string(' ', m_LastWriteLength) + + new string('\b', m_LastWriteLength)); } diff --git a/Source/LibationCli/HelpVerb.cs b/Source/LibationCli/HelpVerb.cs index b9e329cf..022873c8 100644 --- a/Source/LibationCli/HelpVerb.cs +++ b/Source/LibationCli/HelpVerb.cs @@ -16,18 +16,14 @@ internal class HelpVerb /// /// Create a base for /// - public static HelpText CreateHelpText() + public static HelpText CreateHelpText() => new HelpText { - var auto = new HelpText - { - AutoVersion = false, - AutoHelp = false, - Heading = $"LibationCli v{LibationScaffolding.BuildVersion.ToString(3)}", - AdditionalNewLineAfterOption = true, - MaximumDisplayWidth = 80 - }; - return auto; - } + AutoVersion = false, + AutoHelp = false, + Heading = $"LibationCli v{LibationScaffolding.BuildVersion.ToString(3)}", + AdditionalNewLineAfterOption = true, + MaximumDisplayWidth = 80 + }; /// /// Get the 's @@ -43,8 +39,8 @@ internal class HelpVerb } else { - helpText.AddDashesToOption = true; helpText.AutoHelp = true; + helpText.AddDashesToOption = true; helpText.AddOptions(result); } return helpText; diff --git a/Source/LibationCli/Options/SearchOptions.cs b/Source/LibationCli/Options/SearchOptions.cs index 76479dcb..d17e95c2 100644 --- a/Source/LibationCli/Options/SearchOptions.cs +++ b/Source/LibationCli/Options/SearchOptions.cs @@ -3,48 +3,53 @@ using CommandLine; using System; using System.Collections.Generic; using System.Linq; +using System.Text; using System.Threading.Tasks; -namespace LibationCli.Options +namespace LibationCli.Options; + +[Verb("search", HelpText = "Search for books in your library")] +internal class SearchOptions : OptionsBase { - [Verb("search", HelpText = "Search for books in your library")] - internal class SearchOptions : OptionsBase + [Value(0, MetaName = "query", Required = true, HelpText = "Lucene search string")] + public IEnumerable Query { get; set; } + + protected override Task ProcessAsync() { - [Value(0, MetaName = "query", Required = true, HelpText = "Lucene search string")] - public IEnumerable Query { get; set; } + var query = string.Join(" ", Query).Trim('\"'); + var results = SearchEngineCommands.Search(query).Docs.ToList(); - protected override Task ProcessAsync() + Console.WriteLine($"Found {results.Count} matching results."); + + const int numResults = 10; + + string nextPrompt = "Press any key for the next " + numResults + " results or Esc for all results"; + bool waitForNextBatch = true; + + for (int i = 0; i < results.Count; i += numResults) { - var query = string.Join(" ", Query).Trim('\"'); - var results = SearchEngineCommands.Search(query).Docs.ToList(); + var sb = new StringBuilder(); + for (int j = i; j < int.Min(results.Count, i + numResults); j++) + sb.AppendLine(getDocDisplay(results[j].Doc)); - Console.WriteLine($"Found {results.Count} matching results."); + Console.Write(sb.ToString()); - const string nextPrompt = "Press any key for the next 10 results or Esc for all results"; - bool waitForNextBatch = true; - - for (int i = 0; i < results.Count; i += 10) + if (waitForNextBatch) { - foreach (var doc in results.Skip(i).Take(10)) - Console.WriteLine(getDocDisplay(doc.Doc)); - - if (waitForNextBatch) - { - Console.Write(nextPrompt); - waitForNextBatch = Console.ReadKey(intercept: true).Key != ConsoleKey.Escape; - ReplaceConsoleText(Console.Out, nextPrompt.Length, ""); - Console.SetCursorPosition(0, Console.CursorTop); - } + Console.Write(nextPrompt); + waitForNextBatch = Console.ReadKey(intercept: true).Key != ConsoleKey.Escape; + ReplaceConsoleText(Console.Out, nextPrompt.Length, ""); + Console.CursorLeft = 0; } - - return Task.CompletedTask; } - private static string getDocDisplay(Lucene.Net.Documents.Document doc) - { - var title = doc.GetField("title"); - var id = doc.GetField("_ID_"); - return $"[{id.StringValue}] - {title.StringValue}"; - } + return Task.CompletedTask; + } + + private static string getDocDisplay(Lucene.Net.Documents.Document doc) + { + var title = doc.GetField("title"); + var id = doc.GetField("_ID_"); + return $"[{id.StringValue}] - {title.StringValue}"; } }