|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +namespace DevDecoder.GpioSimulator.Sample |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Provides auto-completion suggestions for the interactive GPIO board simulator terminal. |
| 9 | + /// </summary> |
| 10 | + public class CommandAutoCompleteHandler : IAutoCompleteHandler |
| 11 | + { |
| 12 | + private static readonly string[] Commands = |
| 13 | + { |
| 14 | + "open", "close", "write", "read", "setmode", "isopen", |
| 15 | + "issupported", "scheme", "schema", "status", "help", |
| 16 | + "exit", "quit" |
| 17 | + }; |
| 18 | + |
| 19 | + private static readonly string[] Modes = { "in", "out", "pullup", "pulldown" }; |
| 20 | + private static readonly string[] Schemes = { "logical", "board" }; |
| 21 | + private static readonly string[] WriteValues = { "1", "0", "high", "low" }; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Gets or sets the characters that define the boundary for word separations. |
| 25 | + /// </summary> |
| 26 | + public char[] Separators { get; set; } = new char[] { ' ' }; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Generates auto-completion suggestions based on the current input text and cursor index. |
| 30 | + /// </summary> |
| 31 | + /// <param name="text">The current input line text.</param> |
| 32 | + /// <param name="index">The current cursor index.</param> |
| 33 | + /// <returns>An array of suggestions matching the context.</returns> |
| 34 | + public string[] GetSuggestions(string text, int index) |
| 35 | + { |
| 36 | + if (string.IsNullOrEmpty(text)) |
| 37 | + { |
| 38 | + return Commands; |
| 39 | + } |
| 40 | + |
| 41 | + // Split the line by spaces |
| 42 | + string[] parts = text.Split(Separators, StringSplitOptions.None); |
| 43 | + if (parts.Length == 0) |
| 44 | + { |
| 45 | + return Commands; |
| 46 | + } |
| 47 | + |
| 48 | + string cmd = parts[0].ToLower(); |
| 49 | + |
| 50 | + // If we're typing the first word (command) |
| 51 | + if (parts.Length == 1) |
| 52 | + { |
| 53 | + return Commands.Where(c => c.StartsWith(cmd, StringComparison.OrdinalIgnoreCase)).ToArray(); |
| 54 | + } |
| 55 | + |
| 56 | + // Auto-complete sub-commands or parameters |
| 57 | + // Case 1: scheme/schema parameter autocomplete (e.g., scheme <tab>) |
| 58 | + if ((cmd == "scheme" || cmd == "schema") && parts.Length == 2) |
| 59 | + { |
| 60 | + string arg = parts[1].ToLower(); |
| 61 | + return Schemes.Where(s => s.StartsWith(arg, StringComparison.OrdinalIgnoreCase)).ToArray(); |
| 62 | + } |
| 63 | + |
| 64 | + // Case 2: open <pin> <mode> -> parts[2] is the mode |
| 65 | + if (cmd == "open" && parts.Length == 3) |
| 66 | + { |
| 67 | + string arg = parts[2].ToLower(); |
| 68 | + return Modes.Where(m => m.StartsWith(arg, StringComparison.OrdinalIgnoreCase)).ToArray(); |
| 69 | + } |
| 70 | + |
| 71 | + // Case 3: setmode <pin> <mode> / set <pin> <mode> -> parts[2] is the mode |
| 72 | + if ((cmd == "setmode" || cmd == "set" || cmd == "sm") && parts.Length == 3) |
| 73 | + { |
| 74 | + string arg = parts[2].ToLower(); |
| 75 | + return Modes.Where(m => m.StartsWith(arg, StringComparison.OrdinalIgnoreCase)).ToArray(); |
| 76 | + } |
| 77 | + |
| 78 | + // Case 4: issupported <pin> <mode> -> parts[2] is the mode |
| 79 | + if ((cmd == "issupported" || cmd == "is") && parts.Length == 3) |
| 80 | + { |
| 81 | + string arg = parts[2].ToLower(); |
| 82 | + return Modes.Where(m => m.StartsWith(arg, StringComparison.OrdinalIgnoreCase)).ToArray(); |
| 83 | + } |
| 84 | + |
| 85 | + // Case 5: write <pin> <value> -> parts[2] is the value |
| 86 | + if ((cmd == "write" || cmd == "w") && parts.Length == 3) |
| 87 | + { |
| 88 | + string arg = parts[2].ToLower(); |
| 89 | + return WriteValues.Where(v => v.StartsWith(arg, StringComparison.OrdinalIgnoreCase)).ToArray(); |
| 90 | + } |
| 91 | + |
| 92 | + return Array.Empty<string>(); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments