fix core dump on export option

This commit is contained in:
2026-05-20 11:26:44 +02:00
parent 4874bf096c
commit 76064fa822
3 changed files with 59 additions and 60 deletions
+28 -43
View File
@@ -10,9 +10,16 @@ namespace HanaTui.Tui;
/// </summary>
public static class KeySelectionScreen
{
// Sentinel values for the non-key choices
private const string ManualEntry = "__MANUAL__";
private const string ExitChoice = "__EXIT__";
private sealed class KeyChoice
{
public string Display { get; init; } = "";
public string? KeyName { get; init; } // null = exit, "" = manual entry
public static readonly KeyChoice Manual = new() { Display = "[ Enter key name manually ]", KeyName = "" };
public static readonly KeyChoice Exit = new() { Display = "[ Exit ]", KeyName = null };
public override string ToString() => Display;
}
public static string? Run()
{
@@ -30,14 +37,11 @@ public static class KeySelectionScreen
return null;
}
// Show client path for reference
var clientDir = HdbClientLocator.ClientDirectory;
if (clientDir is not null)
AnsiConsole.MarkupLine($"[dim]HDB client: {Markup.Escape(clientDir)}[/]\n");
// Load keys with a spinner
List<HdbUserstoreKey> keys = [];
AnsiConsole.Status()
.Spinner(Spinner.Known.Dots)
.SpinnerStyle(Style.Parse("blue"))
@@ -52,52 +56,34 @@ public static class KeySelectionScreen
AnsiConsole.MarkupLine("You can still enter a key name manually.\n");
}
// Build a plain-string list where the value IS the key name (or sentinel).
// We use SelectionPrompt<string> with a display converter so Spectre renders
// the label as markup but we always get back the plain key name.
var choiceNames = new List<string>();
var displayMap = new Dictionary<string, string>(); // name -> display label
// Build typed choices. Display is plain text — never parsed as markup.
var choices = keys
.Select(k => new KeyChoice { Display = BuildKeyDisplay(k), KeyName = k.Name })
.ToList();
choices.Add(KeyChoice.Manual);
choices.Add(KeyChoice.Exit);
foreach (var k in keys)
{
choiceNames.Add(k.Name);
displayMap[k.Name] = BuildKeyDisplay(k);
}
choiceNames.Add(ManualEntry);
displayMap[ManualEntry] = "[ Enter key name manually ]";
choiceNames.Add(ExitChoice);
displayMap[ExitChoice] = "[ Exit ]";
var prompt = new SelectionPrompt<string>()
var prompt = new SelectionPrompt<KeyChoice>()
.Title("[bold]Select HDBUSERSTORE key:[/]")
.PageSize(15)
.HighlightStyle(Style.Parse("bold dodgerblue1"))
.UseConverter(name => displayMap.TryGetValue(name, out var label)
? Markup.Escape(label)
: Markup.Escape(name))
.AddChoices(choiceNames);
.UseConverter(c => Markup.Escape(c.Display))
.AddChoices(choices);
var selected = AnsiConsole.Prompt(prompt);
if (selected == ExitChoice)
return null;
if (selected.KeyName is null)
return null; // Exit
if (selected == ManualEntry)
if (selected.KeyName == "")
{
var manual = AnsiConsole.Ask<string>("[bold]Enter HDBUSERSTORE key name:[/]").Trim();
return string.IsNullOrWhiteSpace(manual) ? null : manual;
}
// selected is already the plain key name
return selected;
return selected.KeyName;
}
/// <summary>
/// Builds a plain-text display label (no markup) for a key.
/// Spectre's UseConverter renders the returned string as plain text.
/// </summary>
private static string BuildKeyDisplay(HdbUserstoreKey k)
{
var sb = new SysText.StringBuilder(k.Name);
@@ -111,12 +97,11 @@ public static class KeySelectionScreen
sb.Append(':');
sb.Append(k.Port);
}
}
if (!string.IsNullOrEmpty(k.Tenant))
{
sb.Append('@');
sb.Append(k.Tenant);
if (!string.IsNullOrEmpty(k.Tenant))
{
sb.Append('@');
sb.Append(k.Tenant);
}
}
if (!string.IsNullOrEmpty(k.User))