Wp2ghost/Wp2GhostUI/PromptOptions.cs

76 lines
3 KiB
C#
Raw Normal View History

2026-10-14 19:12:29 +02:00
namespace Wp2GhostUI;
/// <summary>Eine Antwortmöglichkeit einer Rückfrage: Beschriftung des Knopfs und was er sendet.</summary>
public record PromptOption(string Label, string Value);
/// <summary>
/// Liest aus dem Text einer Rückfrage die angebotenen Antworten, damit die Oberfläche dafür
/// Knöpfe anzeigen kann. Die Fragen des Importers folgen einem festen Muster, z. B.
/// "... (j/n): "
/// "[k=beibehalten / d=löschen / e=bearbeiten / b=Browser öffnen]: "
/// "(a = alle / e = einzeln fragen / n = nichts): "
/// "(mehrere mit Komma, leer = keiner, + = wie zuletzt: Zypern): "
/// Was sich nicht als Knopf darstellen lässt (z. B. "Nummer" oder "w&lt;ID&gt;"), tippt der User ins Textfeld.
/// </summary>
public static class PromptOptions
{
public static List<PromptOption> Parse(string? prompt)
{
var options = new List<PromptOption>();
string? group = LastGroup(prompt ?? "");
if (group == null) return options;
// Trennzeichen: " / ", sonst "/" (j/n), sonst ", "
string[] tokens = group.Contains(" / ") ? group.Split(" / ")
: group.Contains('/') && !group.Contains("://") ? group.Split('/')
: group.Split(", ");
foreach (string raw in tokens)
{
string token = raw.Trim();
if (token.Length == 0) continue;
int eq = token.IndexOf('=');
if (eq > 0)
{
string key = token.Substring(0, eq).Trim();
string label = token.Substring(eq + 1).Trim();
if (key.Equals("Enter", StringComparison.OrdinalIgnoreCase) || key.Equals("leer", StringComparison.OrdinalIgnoreCase))
options.Add(new PromptOption(Capitalize(label), ""));
else if (key.Length <= 2)
options.Add(new PromptOption($"{Capitalize(label)} ({key})", key));
// längere Schlüssel (z. B. "w<ID>") sind Platzhalter → Textfeld
}
else if (token.Length == 1)
{
options.Add(new PromptOption(token switch { "j" => "Ja", "n" => "Nein", _ => token }, token));
}
}
// Eine einzelne erkannte Angabe wie "(Nummer)" ist keine Auswahl
return options;
}
// Inhalt der letzten Klammer am Ende der Frage: [...] oder (...), auch mit Klammern darin
private static string? LastGroup(string prompt)
{
string text = prompt.TrimEnd().TrimEnd(':').TrimEnd();
if (text.Length == 0) return null;
char close = text[^1];
if (close != ']' && close != ')') return null;
char open = close == ']' ? '[' : '(';
int depth = 0;
for (int i = text.Length - 1; i >= 0; i--)
{
if (text[i] == close) depth++;
else if (text[i] == open && --depth == 0)
return text.Substring(i + 1, text.Length - i - 2);
}
return null;
}
private static string Capitalize(string text) =>
text.Length == 0 ? text : char.ToUpper(text[0]) + text.Substring(1);
}