Performance improvement

This commit is contained in:
Mbucari 2023-07-02 22:23:29 -06:00
parent 83402028fd
commit db2b10d2a4
3 changed files with 47 additions and 47 deletions

View File

@ -30,7 +30,6 @@ internal class ConsoleProgressBar
} }
} }
private double m_Progress; private double m_Progress;
private TimeSpan m_RemainingTime; private TimeSpan m_RemainingTime;
private int m_LastWriteLength = 0; private int m_LastWriteLength = 0;
@ -71,7 +70,7 @@ internal class ConsoleProgressBar
public void Clear() public void Clear()
=> Output.Write( => Output.Write(
new string('\b', m_LastWriteLength) new string('\b', m_LastWriteLength) +
+ new string(' ', m_LastWriteLength) new string(' ', m_LastWriteLength) +
+ new string('\b', m_LastWriteLength)); new string('\b', m_LastWriteLength));
} }

View File

@ -16,18 +16,14 @@ internal class HelpVerb
/// <summary> /// <summary>
/// Create a base <see cref="HelpText"/> for <see cref="LibationCli"/> /// Create a base <see cref="HelpText"/> for <see cref="LibationCli"/>
/// </summary> /// </summary>
public static HelpText CreateHelpText() public static HelpText CreateHelpText() => new HelpText
{ {
var auto = new HelpText AutoVersion = false,
{ AutoHelp = false,
AutoVersion = false, Heading = $"LibationCli v{LibationScaffolding.BuildVersion.ToString(3)}",
AutoHelp = false, AdditionalNewLineAfterOption = true,
Heading = $"LibationCli v{LibationScaffolding.BuildVersion.ToString(3)}", MaximumDisplayWidth = 80
AdditionalNewLineAfterOption = true, };
MaximumDisplayWidth = 80
};
return auto;
}
/// <summary> /// <summary>
/// Get the <see cref="HelpType"/>'s <see cref="HelpText"/> /// Get the <see cref="HelpType"/>'s <see cref="HelpText"/>
@ -43,8 +39,8 @@ internal class HelpVerb
} }
else else
{ {
helpText.AddDashesToOption = true;
helpText.AutoHelp = true; helpText.AutoHelp = true;
helpText.AddDashesToOption = true;
helpText.AddOptions(result); helpText.AddOptions(result);
} }
return helpText; return helpText;

View File

@ -3,48 +3,53 @@ using CommandLine;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks; 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")] [Value(0, MetaName = "query", Required = true, HelpText = "Lucene search string")]
internal class SearchOptions : OptionsBase public IEnumerable<string> Query { get; set; }
protected override Task ProcessAsync()
{ {
[Value(0, MetaName = "query", Required = true, HelpText = "Lucene search string")] var query = string.Join(" ", Query).Trim('\"');
public IEnumerable<string> Query { get; set; } 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 sb = new StringBuilder();
var results = SearchEngineCommands.Search(query).Docs.ToList(); 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"; if (waitForNextBatch)
bool waitForNextBatch = true;
for (int i = 0; i < results.Count; i += 10)
{ {
foreach (var doc in results.Skip(i).Take(10)) Console.Write(nextPrompt);
Console.WriteLine(getDocDisplay(doc.Doc)); waitForNextBatch = Console.ReadKey(intercept: true).Key != ConsoleKey.Escape;
ReplaceConsoleText(Console.Out, nextPrompt.Length, "");
if (waitForNextBatch) Console.CursorLeft = 0;
{
Console.Write(nextPrompt);
waitForNextBatch = Console.ReadKey(intercept: true).Key != ConsoleKey.Escape;
ReplaceConsoleText(Console.Out, nextPrompt.Length, "");
Console.SetCursorPosition(0, Console.CursorTop);
}
} }
return Task.CompletedTask;
} }
private static string getDocDisplay(Lucene.Net.Documents.Document doc) return Task.CompletedTask;
{ }
var title = doc.GetField("title");
var id = doc.GetField("_ID_"); private static string getDocDisplay(Lucene.Net.Documents.Document doc)
return $"[{id.StringValue}] - {title.StringValue}"; {
} var title = doc.GetField("title");
var id = doc.GetField("_ID_");
return $"[{id.StringValue}] - {title.StringValue}";
} }
} }