Browse Source

Finish up basic CLI. Next up: testing!

master
Starbeamrainbowlabs 4 years ago
parent
commit
b24f573eb9
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
  1. 26
      SearchBox-CLI/Program.cs
  2. 1
      SearchBox/SearchBox.cs
  3. 10
      SearchBox/SearchResult.cs

26
SearchBox-CLI/Program.cs

@ -28,6 +28,8 @@ namespace SearchBoxCLI @@ -28,6 +28,8 @@ namespace SearchBoxCLI
}
class MainClass {
private static List<string> Extras = new List<string>();
private static OperatingModes Mode = OperatingModes.Query;
private static bool Batch = false;
private static string Name = string.Empty;
@ -39,11 +41,10 @@ namespace SearchBoxCLI @@ -39,11 +41,10 @@ namespace SearchBoxCLI
public static int Main(string[] args)
{
List<string> extras = new List<string>();
for (int i = 0; i < args.Length; i++)
{
if (!args[i].StartsWith("-")) {
extras.Add(args[i]);
Extras.Add(args[i]);
continue;
}
@ -89,8 +90,8 @@ namespace SearchBoxCLI @@ -89,8 +90,8 @@ namespace SearchBoxCLI
return 1;
}
}
if (extras.Count < 1) return HandleHelp();
string modeText = extras.First(); extras.RemoveAt(0);
if (Extras.Count < 1) return HandleHelp();
string modeText = Extras.First(); Extras.RemoveAt(0);
Mode = (OperatingModes)Enum.Parse(typeof(OperatingModes), modeText, true);
switch (Mode) {
@ -111,6 +112,7 @@ namespace SearchBoxCLI @@ -111,6 +112,7 @@ namespace SearchBoxCLI
Console.WriteLine();
Console.WriteLine("Usage:");
Console.WriteLine(" ./SearchBox.exe {mode} [options]");
Console.WriteLine(" ./SearchBox.exe query \"{query string}\" [options]");
Console.WriteLine();
Console.WriteLine("Modes:");
Console.WriteLine(" query Query a pre-existing inverted search index");
@ -213,13 +215,25 @@ namespace SearchBoxCLI @@ -213,13 +215,25 @@ namespace SearchBoxCLI
private static int HandleQuery()
{
if (Extras.Count < 1) {
Console.Error.WriteLine("Error: No query specified!");
return 1;
}
SearchBox searchBox = JsonConvert.DeserializeObject<SearchBox>(
File.ReadAllText(SearchIndexFilepath)
);
List<SearchResult> results = searchBox.Query(Extras[0], new QuerySettings());
switch (OutputMode)
{
case OutputModes.Json:
Console.WriteLine(JsonConvert.SerializeObject(results));
break;
case OutputModes.Text:
foreach (SearchResult nextResult in results)
Console.WriteLine(nextResult);
break;
}
return 0;

1
SearchBox/SearchBox.cs

@ -114,6 +114,7 @@ namespace LibSearchBox @@ -114,6 +114,7 @@ namespace LibSearchBox
List<SearchOffset> offsets = new List<SearchOffset>();
foreach (Tuple<int, string> token in tokenizer.IterateTokens())
offsets.AddRange(index.Query(token.Item2)[pageDef.Key].Select((int offset) => new SearchOffset(token.Item2, offset)));
offsets.Sort((SearchOffset x, SearchOffset y) => x.Offset - y.Offset);
resultsRaw.Add(new SearchResult(
idMap.GetPageName(pageDef.Key),

10
SearchBox/SearchResult.cs

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace LibSearchBox
{
@ -30,6 +32,12 @@ namespace LibSearchBox @@ -30,6 +32,12 @@ namespace LibSearchBox
Offsets = inOffsets;
}
public override string ToString()
{
StringBuilder result = new StringBuilder();
result.AppendLine($"{Rank.ToString().PadLeft(5)}: {PageName}");
result.Append($" {string.Join(", ", Offsets.Select((SearchOffset nextOffset) => $"{nextOffset.Term} @ {nextOffset.Offset}"))}");
return base.ToString();
}
}
}

Loading…
Cancel
Save