Skip to content

Commit 1d58d95

Browse files
authored
Add up/down arrow keys to interactive help viewer (#46)
1 parent c18e848 commit 1d58d95

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

src/Vertical/CommandLine/Help/InteractiveConsoleHelpWriter.cs

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System;
88
using System.Collections.Generic;
99
using System.Diagnostics.CodeAnalysis;
10+
using System.Linq;
1011

1112
namespace Vertical.CommandLine.Help
1213
{
@@ -43,8 +44,8 @@ private InteractiveConsoleHelpWriter(bool helpMode)
4344
"\tu : Previous half page",
4445
"\tj, <enter> : Next line",
4546
"\tk : Previous line",
46-
"\tg, < : First line",
47-
"\tG, > : Next line",
47+
"\tg, < : First page",
48+
"\tG, > : Last page",
4849
"\th : Show command list",
4950
"\tq : Quit"
5051
};
@@ -85,41 +86,46 @@ private enum Command
8586
Help,
8687
Quit
8788
}
88-
89-
// Define key/command structure
90-
private struct CommandKeyMapping
89+
90+
private sealed class CommandKeyMapping2
9191
{
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)
9396
{
9497
Command = command;
95-
KeyChar = keyChar;
98+
99+
_key = key;
100+
_modifiers = shift ? ConsoleModifiers.Shift : default;
96101
}
97-
102+
98103
internal Command Command { get; }
99-
internal char KeyChar { get; }
104+
105+
internal bool IsEquivalentOf(in ConsoleKeyInfo key) => key.Key == _key && key.Modifiers == _modifiers;
100106
}
101107

102108
// Defines mapping between keys and commands
103-
private static readonly CommandKeyMapping[] Commands = new[]
109+
private static readonly CommandKeyMapping2[] Commands = new[]
104110
{
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)
120127
};
121-
122-
128+
123129
/// <inheritdoc />
124130
public void WriteContent(IReadOnlyCollection<string> content)
125131
{
@@ -154,10 +160,13 @@ private bool HandleUserInput(FormatInfo formatInfo, int lineCount, out int start
154160

155161
while (true)
156162
{
157-
switch (PromptAndAwaitCommand(prompt))
163+
var command = PromptAndAwaitCommand(prompt);
164+
165+
if (_helpMode) return false;
166+
167+
switch (command)
158168
{
159169
case Command.None:
160-
if (_helpMode) return false;
161170
prompt = "[command], [h]elp, [q]uit";
162171
continue;
163172

@@ -216,9 +225,10 @@ private static Command PromptAndAwaitCommand(string prompt)
216225
}
217226
Console.Write(" ");
218227

219-
var keyChar = Console.ReadKey(true).KeyChar;
228+
var keyInfo = Console.ReadKey(true);
220229

221-
return Array.Find(Commands, cmd => cmd.KeyChar == keyChar).Command;
230+
return Commands.FirstOrDefault(cmd => cmd.IsEquivalentOf(keyInfo))?.Command
231+
?? Command.None;
222232
}
223233

224234
private static void ShowHelp()

0 commit comments

Comments
 (0)