|
7 | 7 | using System; |
8 | 8 | using System.Collections.Generic; |
9 | 9 | using System.Diagnostics.CodeAnalysis; |
| 10 | +using System.Linq; |
10 | 11 |
|
11 | 12 | namespace Vertical.CommandLine.Help |
12 | 13 | { |
@@ -43,8 +44,8 @@ private InteractiveConsoleHelpWriter(bool helpMode) |
43 | 44 | "\tu : Previous half page", |
44 | 45 | "\tj, <enter> : Next line", |
45 | 46 | "\tk : Previous line", |
46 | | - "\tg, < : First line", |
47 | | - "\tG, > : Next line", |
| 47 | + "\tg, < : First page", |
| 48 | + "\tG, > : Last page", |
48 | 49 | "\th : Show command list", |
49 | 50 | "\tq : Quit" |
50 | 51 | }; |
@@ -85,41 +86,46 @@ private enum Command |
85 | 86 | Help, |
86 | 87 | Quit |
87 | 88 | } |
88 | | - |
89 | | - // Define key/command structure |
90 | | - private struct CommandKeyMapping |
| 89 | + |
| 90 | + private sealed class CommandKeyMapping2 |
91 | 91 | { |
92 | | - internal CommandKeyMapping(Command command, char keyChar) |
| 92 | + private readonly ConsoleKey _key; |
| 93 | + private readonly ConsoleModifiers _modifiers; |
| 94 | + |
| 95 | + internal CommandKeyMapping2(Command command, ConsoleKey key, bool shift = false) |
93 | 96 | { |
94 | 97 | Command = command; |
95 | | - KeyChar = keyChar; |
| 98 | + |
| 99 | + _key = key; |
| 100 | + _modifiers = shift ? ConsoleModifiers.Shift : default; |
96 | 101 | } |
97 | | - |
| 102 | + |
98 | 103 | internal Command Command { get; } |
99 | | - internal char KeyChar { get; } |
| 104 | + |
| 105 | + internal bool IsEquivalentOf(in ConsoleKeyInfo key) => key.Key == _key && key.Modifiers == _modifiers; |
100 | 106 | } |
101 | 107 |
|
102 | 108 | // Defines mapping between keys and commands |
103 | | - private static readonly CommandKeyMapping[] Commands = new[] |
| 109 | + private static readonly CommandKeyMapping2[] Commands = new[] |
104 | 110 | { |
105 | | - new CommandKeyMapping(Command.PrevPage, 'b'), |
106 | | - new CommandKeyMapping(Command.PrevHalfPage, 'u'), |
107 | | - new CommandKeyMapping(Command.PrevLine, 'k'), |
108 | | - new CommandKeyMapping(Command.NextPage, ' '), |
109 | | - new CommandKeyMapping(Command.NextHalfPage, 'd'), |
110 | | - new CommandKeyMapping(Command.NextLine, 'j'), |
111 | | - new CommandKeyMapping(Command.NextLine, (char)0xd), |
112 | | - new CommandKeyMapping(Command.NextLine, (char)0xa), |
113 | | - new CommandKeyMapping(Command.FirstLine, 'g'), |
114 | | - new CommandKeyMapping(Command.FirstLine, '<'), |
115 | | - new CommandKeyMapping(Command.LastLine, 'G'), |
116 | | - new CommandKeyMapping(Command.LastLine, '>'), |
117 | | - new CommandKeyMapping(Command.Help, 'h'), |
118 | | - new CommandKeyMapping(Command.Quit, 'q'), |
119 | | - new CommandKeyMapping(Command.Quit, (char)0x1b) |
| 111 | + new CommandKeyMapping2(Command.PrevPage, ConsoleKey.B), |
| 112 | + new CommandKeyMapping2(Command.PrevPage, ConsoleKey.UpArrow), |
| 113 | + new CommandKeyMapping2(Command.PrevHalfPage, ConsoleKey.U), |
| 114 | + new CommandKeyMapping2(Command.PrevLine, ConsoleKey.K), |
| 115 | + new CommandKeyMapping2(Command.NextPage, ConsoleKey.Spacebar), |
| 116 | + new CommandKeyMapping2(Command.NextPage, ConsoleKey.DownArrow), |
| 117 | + new CommandKeyMapping2(Command.NextHalfPage, ConsoleKey.D), |
| 118 | + new CommandKeyMapping2(Command.NextLine, ConsoleKey.J), |
| 119 | + new CommandKeyMapping2(Command.NextLine, ConsoleKey.Enter), |
| 120 | + new CommandKeyMapping2(Command.FirstLine, ConsoleKey.G), |
| 121 | + new CommandKeyMapping2(Command.FirstLine, ConsoleKey.OemComma, shift: true), |
| 122 | + new CommandKeyMapping2(Command.LastLine, ConsoleKey.G, shift: true), |
| 123 | + new CommandKeyMapping2(Command.LastLine, ConsoleKey.OemPeriod, shift: true), |
| 124 | + new CommandKeyMapping2(Command.Help, ConsoleKey.H), |
| 125 | + new CommandKeyMapping2(Command.Quit, ConsoleKey.Q), |
| 126 | + new CommandKeyMapping2(Command.Quit, ConsoleKey.Escape) |
120 | 127 | }; |
121 | | - |
122 | | - |
| 128 | + |
123 | 129 | /// <inheritdoc /> |
124 | 130 | public void WriteContent(IReadOnlyCollection<string> content) |
125 | 131 | { |
@@ -154,10 +160,13 @@ private bool HandleUserInput(FormatInfo formatInfo, int lineCount, out int start |
154 | 160 |
|
155 | 161 | while (true) |
156 | 162 | { |
157 | | - switch (PromptAndAwaitCommand(prompt)) |
| 163 | + var command = PromptAndAwaitCommand(prompt); |
| 164 | + |
| 165 | + if (_helpMode) return false; |
| 166 | + |
| 167 | + switch (command) |
158 | 168 | { |
159 | 169 | case Command.None: |
160 | | - if (_helpMode) return false; |
161 | 170 | prompt = "[command], [h]elp, [q]uit"; |
162 | 171 | continue; |
163 | 172 |
|
@@ -216,9 +225,10 @@ private static Command PromptAndAwaitCommand(string prompt) |
216 | 225 | } |
217 | 226 | Console.Write(" "); |
218 | 227 |
|
219 | | - var keyChar = Console.ReadKey(true).KeyChar; |
| 228 | + var keyInfo = Console.ReadKey(true); |
220 | 229 |
|
221 | | - return Array.Find(Commands, cmd => cmd.KeyChar == keyChar).Command; |
| 230 | + return Commands.FirstOrDefault(cmd => cmd.IsEquivalentOf(keyInfo))?.Command |
| 231 | + ?? Command.None; |
222 | 232 | } |
223 | 233 |
|
224 | 234 | private static void ShowHelp() |
|
0 commit comments