|
| 1 | +using Spectre.Console; |
| 2 | + |
| 3 | +namespace OpenKey.Ui; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Interactive prompts that degrade instead of throwing. |
| 7 | +/// <para> |
| 8 | +/// Spectre's prompts raise <see cref="NotSupportedException"/> when the terminal is not |
| 9 | +/// interactive — which is true whenever any standard stream is redirected, so it happens for |
| 10 | +/// `openkey < script.txt`, for piped input, and inside CI. Nothing caught it, so a piped |
| 11 | +/// <c>/models</c> killed the app with a raw exception name on screen. |
| 12 | +/// </para> |
| 13 | +/// <para> |
| 14 | +/// Every fallback here reads a plain line instead, which is exactly what a piped caller can |
| 15 | +/// provide. Destructive confirmations deliberately require an explicit word rather than accepting |
| 16 | +/// a bare newline. |
| 17 | +/// </para> |
| 18 | +/// </summary> |
| 19 | +internal static class Prompts |
| 20 | +{ |
| 21 | + /// <summary>True when Spectre's own prompts are safe to use.</summary> |
| 22 | + public static bool CanPrompt => ConsoleLayout.Rich; |
| 23 | + |
| 24 | + public static bool Confirm(string question, bool defaultValue = false) |
| 25 | + { |
| 26 | + if (CanPrompt) |
| 27 | + { |
| 28 | + try { return AnsiConsole.Confirm(question, defaultValue); } |
| 29 | + catch (NotSupportedException) { /* fall through */ } |
| 30 | + } |
| 31 | + |
| 32 | + AnsiConsole.Markup($"{Markup.Escape(question)} [{Theme.Muted}](yes/no)[/] "); |
| 33 | + var answer = Console.ReadLine(); |
| 34 | + if (Console.IsInputRedirected) AnsiConsole.WriteLine(); |
| 35 | + |
| 36 | + // No answer means no. A destructive default of "yes" on EOF would be indefensible. |
| 37 | + return answer?.Trim().ToLowerInvariant() is "y" or "yes"; |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Presents a choice. Returns the selected item, or null when the user declined or nothing |
| 42 | + /// could be read. |
| 43 | + /// </summary> |
| 44 | + public static string? Select(string title, IReadOnlyList<string> choices) |
| 45 | + { |
| 46 | + if (choices.Count == 0) return null; |
| 47 | + |
| 48 | + if (CanPrompt) |
| 49 | + { |
| 50 | + try |
| 51 | + { |
| 52 | + var prompt = new SelectionPrompt<string> |
| 53 | + { |
| 54 | + Title = Components.PickerTitle(title), |
| 55 | + PageSize = 12, |
| 56 | + MoreChoicesText = $"[{Theme.Muted}]More below[/]", |
| 57 | + }; |
| 58 | + foreach (var c in choices) prompt.AddChoice(c); |
| 59 | + return AnsiConsole.Prompt(prompt); |
| 60 | + } |
| 61 | + catch (NotSupportedException) { /* fall through */ } |
| 62 | + } |
| 63 | + |
| 64 | + // Numbered list is the only sane fallback: a piped caller cannot press arrow keys. |
| 65 | + AnsiConsole.MarkupLine(Markup.Escape(title)); |
| 66 | + for (var i = 0; i < choices.Count; i++) |
| 67 | + AnsiConsole.MarkupLine($" [{Theme.Brand}]{i + 1}[/] {Markup.Escape(choices[i])}"); |
| 68 | + |
| 69 | + Components.HintLine("Type a number and press Enter, or press Enter to cancel."); |
| 70 | + AnsiConsole.Markup($"[{Theme.Brand}]#[/] "); |
| 71 | + |
| 72 | + var line = Console.ReadLine(); |
| 73 | + if (Console.IsInputRedirected) AnsiConsole.WriteLine(); |
| 74 | + |
| 75 | + return int.TryParse(line?.Trim(), out var pick) && pick >= 1 && pick <= choices.Count |
| 76 | + ? choices[pick - 1] |
| 77 | + : null; |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Reads a secret. Masked when the terminal allows it; when it does not, the value is coming |
| 82 | + /// from a pipe and there is nothing on screen to hide. |
| 83 | + /// </summary> |
| 84 | + public static string? Secret(string label) |
| 85 | + { |
| 86 | + if (CanPrompt) |
| 87 | + { |
| 88 | + try |
| 89 | + { |
| 90 | + return AnsiConsole.Prompt( |
| 91 | + new TextPrompt<string>(label) |
| 92 | + .Secret() |
| 93 | + .Validate(k => string.IsNullOrWhiteSpace(k) |
| 94 | + ? ValidationResult.Error($"[{Theme.Danger}]Paste a key to continue, or press Ctrl+C to go back.[/]") |
| 95 | + : ValidationResult.Success())); |
| 96 | + } |
| 97 | + catch (NotSupportedException) { /* fall through */ } |
| 98 | + } |
| 99 | + |
| 100 | + AnsiConsole.Markup(Markup.Escape(label)); |
| 101 | + var value = Console.ReadLine(); |
| 102 | + if (Console.IsInputRedirected) AnsiConsole.WriteLine(); |
| 103 | + |
| 104 | + return string.IsNullOrWhiteSpace(value) ? null : value.Trim(); |
| 105 | + } |
| 106 | +} |
0 commit comments