Skip to content

Commit

Permalink
Support different key bindings to those displayed. Implement vim-styl…
Browse files Browse the repository at this point in the history
…e arrow key bindings
  • Loading branch information
JustAman62 committed Feb 28, 2025
1 parent 90cd7df commit 098c3a3
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion OpenF1.Console/ConsoleLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void UpdateInputFooter(Layout layout)
var commandDescriptions = inputHandlers
.Where(x => x.IsEnabled && x.ApplicableScreens.Contains(state.CurrentScreen))
.OrderBy(x => x.Sort)
.Select(x => $"[{x.Keys.ToDisplayCharacters()}] {x.Description}");
.Select(x => $"[{x.DisplayKeys.ToDisplayCharacters()}] {x.Description}");

var columns = new Columns(commandDescriptions.Select(x => new Text(x)));
columns.Collapse();
Expand Down
10 changes: 7 additions & 3 deletions OpenF1.Console/Extensions/ConsoleKeyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ namespace OpenF1.Console;

public static class ConsoleKeyExtensions
{
private static readonly ConsoleKey[] _hiddenKeys =
[
(ConsoleKey)3, // ^C
];

public static string GetConsoleKeyDisplayCharacter(this ConsoleKey key) =>
key switch
{
Expand All @@ -14,13 +19,12 @@ public static string GetConsoleKeyDisplayCharacter(this ConsoleKey key) =>
ConsoleKey.OemComma => ",",
ConsoleKey.OemPeriod => ".",
ConsoleKey.Enter => "⏎",
_ => key.ToString()
_ => key.ToString(),
};

public static string ToDisplayCharacters(this ConsoleKey[] keys)
{
var characters = keys.Where(x => x != (ConsoleKey)3) // Remove the ^C key code
.Select(x => x.GetConsoleKeyDisplayCharacter());
var characters = keys.Except(_hiddenKeys).Select(GetConsoleKeyDisplayCharacter);
return string.Join('/', characters);
}
}
7 changes: 5 additions & 2 deletions OpenF1.Console/Input/CursorInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ public sealed class CursorInputHandler(State state) : IInputHandler

public Screen[] ApplicableScreens => Enum.GetValues<Screen>();

public ConsoleKey[] Keys => [ConsoleKey.UpArrow, ConsoleKey.DownArrow];
public ConsoleKey[] Keys =>
[ConsoleKey.UpArrow, ConsoleKey.DownArrow, ConsoleKey.J, ConsoleKey.K];

public ConsoleKey[] DisplayKeys => [ConsoleKey.UpArrow, ConsoleKey.DownArrow];

public string Description => $"Cursor {state.CursorOffset}";

Expand All @@ -17,7 +20,7 @@ public Task ExecuteAsync(
CancellationToken cancellationToken = default
)
{
var changeBy = consoleKeyInfo.Key == ConsoleKey.DownArrow ? 1 : -1;
var changeBy = consoleKeyInfo.Key is ConsoleKey.DownArrow or ConsoleKey.J ? 1 : -1;
if (consoleKeyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift))
{
changeBy *= 5;
Expand Down
16 changes: 9 additions & 7 deletions OpenF1.Console/Input/EscapeInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ public class EscapeInputHandler(State state) : IInputHandler
public Screen[] ApplicableScreens => Enum.GetValues<Screen>();

public ConsoleKey[] Keys =>
[
state.CurrentScreen == Screen.Main ? ConsoleKey.X : ConsoleKey.Escape,
(ConsoleKey)3 // Key Code for Control+C
];
state.CurrentScreen == Screen.Main
? [ConsoleKey.Q, ConsoleKey.X, (ConsoleKey)3]
: [ConsoleKey.Escape, (ConsoleKey)3];

public ConsoleKey[] DisplayKeys =>
[state.CurrentScreen == Screen.Main ? ConsoleKey.Q : ConsoleKey.Escape];

public int Sort => 1;

public string Description =>
state.CurrentScreen switch
{
Screen.Main => "Exit",
_ => "Return"
Screen.Main => "Quit",
_ => "Back",
};

public Task ExecuteAsync(
Expand All @@ -30,7 +32,7 @@ public Task ExecuteAsync(
{
Screen.Main => Screen.Shutdown,
Screen.StartSimulatedSession => Screen.ManageSession,
_ => Screen.Main
_ => Screen.Main,
};

return Task.CompletedTask;
Expand Down
11 changes: 11 additions & 0 deletions OpenF1.Console/Input/IInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@ public interface IInputHandler

Screen[] ApplicableScreens { get; }

/// <summary>
/// Which keys will activate this input handler.
/// </summary>
ConsoleKey[] Keys { get; }

/// <summary>
/// The keys to display to the user that would activate this input handler.
// Defaults to <see cref="Keys"/>. Use this to have keys which will activate
// this handler without showing the user that those work will work.
// For example, for alternate bindings (e.g. HJKL instead of arrow keys).
/// </summary>
ConsoleKey[] DisplayKeys => Keys;

string Description { get; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ State state

public Screen[] ApplicableScreens => [Screen.StartSimulatedSession];

public ConsoleKey[] Keys => [ConsoleKey.Enter, ConsoleKey.RightArrow];
public ConsoleKey[] Keys => [ConsoleKey.Enter, ConsoleKey.RightArrow, ConsoleKey.L];

public ConsoleKey[] DisplayKeys => [ConsoleKey.RightArrow];

public string Description => "Select";

Expand Down Expand Up @@ -56,7 +58,9 @@ State state

public Screen[] ApplicableScreens => [Screen.StartSimulatedSession];

public ConsoleKey[] Keys => [ConsoleKey.LeftArrow];
public ConsoleKey[] Keys => [ConsoleKey.LeftArrow, ConsoleKey.H];

public ConsoleKey[] DisplayKeys => [ConsoleKey.LeftArrow];

public string Description => "Deselect";

Expand Down
11 changes: 8 additions & 3 deletions OpenF1.Console/Input/SwitchPageInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ public class SwitchPageInputHandler(LapCountProcessor lapCountProcessor, State s
Screen.TimingTower,
Screen.TimingHistory,
Screen.TyreStints,
Screen.ChampionshipStats
Screen.ChampionshipStats,
];

public ConsoleKey[] Keys => [ConsoleKey.LeftArrow, ConsoleKey.RightArrow];
public ConsoleKey[] Keys =>
[ConsoleKey.LeftArrow, ConsoleKey.H, ConsoleKey.RightArrow, ConsoleKey.L];

public ConsoleKey[] DisplayKeys => [ConsoleKey.LeftArrow, ConsoleKey.RightArrow];

public string Description => $"Page {GetScreenIndex() + 1}";

Expand All @@ -33,7 +36,9 @@ public async Task ExecuteAsync(

// Find the index of the current screen, and move to the next one
var index = GetScreenIndex();
var newIndex = consoleKeyInfo.Key == ConsoleKey.LeftArrow ? index - 1 : index + 1;
var newIndex = consoleKeyInfo.Key is ConsoleKey.LeftArrow or ConsoleKey.H
? index - 1
: index + 1;
var newScreen = newIndex % ApplicableScreens.Length;
state.CurrentScreen =
newScreen < 0 ? ApplicableScreens.Last() : ApplicableScreens[newScreen];
Expand Down

0 comments on commit 098c3a3

Please sign in to comment.