Skip to content

Commit

Permalink
Add ALT+J as alternative ENTER
Browse files Browse the repository at this point in the history
  • Loading branch information
AptiviCEO committed Jan 16, 2023
1 parent f45c95a commit e964391
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions TermRead/Bindings/BaseBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public abstract class BaseBinding : IBinding
/// </summary>
public virtual ConsoleKeyInfo[] BoundKeys { get; }

/// <summary>
/// Does this binding cause the input to exit?
/// </summary>
public virtual bool IsExit { get; }

/// <summary>
/// Whether the binding matched
/// </summary>
Expand Down
6 changes: 5 additions & 1 deletion TermRead/Bindings/BaseBindings/Return.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ internal class Return : BaseBinding, IBinding
/// <inheritdoc/>
public override ConsoleKeyInfo[] BoundKeys { get; } =
{
new ConsoleKeyInfo('\r', ConsoleKey.Enter, false, false, false)
new ConsoleKeyInfo('\r', ConsoleKey.Enter, false, false, false),
new ConsoleKeyInfo('\u000A', ConsoleKey.J, false, false, true),
};

/// <inheritdoc/>
public override bool IsExit => true;

/// <inheritdoc/>
public override void DoAction(TermReaderState state) =>
ConsoleWrapperTools.ActionWriteLine();
Expand Down
13 changes: 13 additions & 0 deletions TermRead/Bindings/BindingsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*
*/

using System;
using System.Linq;
using TermRead.Reader;

Expand All @@ -43,5 +44,17 @@ internal static void Execute(TermReaderState state)
if (!chosenBindings.Any())
BindingsList.fallbackBinding.DoAction(state);
}

internal static bool IsTerminate(ConsoleKeyInfo cki)
{
// Get the chosen bindings
var chosenBindings = BindingsList.AllBindings.Where((bindingInfo) => bindingInfo.BindMatched(cki));

// Return exit value in one of the bindings
foreach (var chosenBinding in chosenBindings)
return chosenBinding.IsExit;

return false;
}
}
}
2 changes: 1 addition & 1 deletion TermRead/Reader/TermReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static string Read(string inputPrompt, string defaultValue, bool password
readState.passwordMode = password;

// Get input
while (struckKey.Key != ConsoleKey.Enter)
while (!BindingsReader.IsTerminate(struckKey))
{
// Get a key
struckKey = ConsoleWrapperTools.ActionReadKey(true);
Expand Down

0 comments on commit e964391

Please sign in to comment.