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 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));
}

View File

@ -16,18 +16,14 @@ internal class HelpVerb
/// <summary>
/// Create a base <see cref="HelpText"/> for <see cref="LibationCli"/>
/// </summary>
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
};
/// <summary>
/// Get the <see cref="HelpType"/>'s <see cref="HelpText"/>
@ -43,8 +39,8 @@ internal class HelpVerb
}
else
{
helpText.AddDashesToOption = true;
helpText.AutoHelp = true;
helpText.AddDashesToOption = true;
helpText.AddOptions(result);
}
return helpText;

View File

@ -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<string> Query { get; set; }
protected override Task ProcessAsync()
{
[Value(0, MetaName = "query", Required = true, HelpText = "Lucene search string")]
public IEnumerable<string> 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}";
}
}