Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions Sharprompt.Tests/ConsoleKeyBindingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;

using Sharprompt.Internal;

using Xunit;

namespace Sharprompt.Tests;

public class ConsoleKeyBindingTests
{
[Fact]
public void Constructor_DefaultModifiers_IsNone()
{
var binding = new ConsoleKeyBinding(ConsoleKey.Enter);

Assert.Equal(ConsoleKey.Enter, binding.Key);
Assert.Equal(default(ConsoleModifiers), binding.Modifiers);
}

[Fact]
public void Constructor_WithModifiers_StoresModifiers()
{
var binding = new ConsoleKeyBinding(ConsoleKey.LeftArrow, ConsoleModifiers.Control);

Assert.Equal(ConsoleKey.LeftArrow, binding.Key);
Assert.Equal(ConsoleModifiers.Control, binding.Modifiers);
}

[Fact]
public void Equals_SameKeyAndModifiers_ReturnsTrue()
{
var a = new ConsoleKeyBinding(ConsoleKey.A, ConsoleModifiers.Control);
var b = new ConsoleKeyBinding(ConsoleKey.A, ConsoleModifiers.Control);

Assert.Equal(a, b);
Assert.True(a == b);
Assert.False(a != b);
}

[Fact]
public void Equals_DifferentKey_ReturnsFalse()
{
var a = new ConsoleKeyBinding(ConsoleKey.A);
var b = new ConsoleKeyBinding(ConsoleKey.B);

Assert.NotEqual(a, b);
Assert.False(a == b);
Assert.True(a != b);
}

[Fact]
public void Equals_SameKeyDifferentModifiers_ReturnsFalse()
{
var plain = new ConsoleKeyBinding(ConsoleKey.LeftArrow);
var ctrl = new ConsoleKeyBinding(ConsoleKey.LeftArrow, ConsoleModifiers.Control);

Assert.NotEqual(plain, ctrl);
Assert.False(plain == ctrl);
Assert.True(plain != ctrl);
}

[Fact]
public void GetHashCode_SameKeyAndModifiers_SameHash()
{
var a = new ConsoleKeyBinding(ConsoleKey.Delete, ConsoleModifiers.Control);
var b = new ConsoleKeyBinding(ConsoleKey.Delete, ConsoleModifiers.Control);

Assert.Equal(a.GetHashCode(), b.GetHashCode());
}

[Fact]
public void GetHashCode_DifferentBindings_DifferentHash()
{
var a = new ConsoleKeyBinding(ConsoleKey.Delete);
var b = new ConsoleKeyBinding(ConsoleKey.Delete, ConsoleModifiers.Control);

Assert.NotEqual(a.GetHashCode(), b.GetHashCode());
}

[Fact]
public void CanBeUsedAsDictionaryKey()
{
var dict = new System.Collections.Generic.Dictionary<ConsoleKeyBinding, string>
{
[new ConsoleKeyBinding(ConsoleKey.LeftArrow)] = "plain left",
[new ConsoleKeyBinding(ConsoleKey.LeftArrow, ConsoleModifiers.Control)] = "ctrl left"
};

Assert.Equal("plain left", dict[new ConsoleKeyBinding(ConsoleKey.LeftArrow)]);
Assert.Equal("ctrl left", dict[new ConsoleKeyBinding(ConsoleKey.LeftArrow, ConsoleModifiers.Control)]);
}

[Fact]
public void Equals_WithObject_ReturnsTrueForSameBinding()
{
var a = new ConsoleKeyBinding(ConsoleKey.Enter);
object b = new ConsoleKeyBinding(ConsoleKey.Enter);

Assert.True(a.Equals(b));
}

[Fact]
public void Equals_WithDifferentType_ReturnsFalse()
{
var a = new ConsoleKeyBinding(ConsoleKey.Enter);

Assert.False(a.Equals("Enter"));
}
}
18 changes: 16 additions & 2 deletions Sharprompt/Forms/FormBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ protected FormBase(PromptConfiguration configuration)
_consoleDriver.CancellationCallback = CancellationHandler;

_formRenderer = new FormRenderer(_consoleDriver, configuration);

KeyHandlerMaps = new()
{
[new ConsoleKeyBinding(ConsoleKey.Escape)] = HandleEscape
};
}

private readonly IConsoleDriver _consoleDriver;
Expand All @@ -29,7 +34,7 @@ protected FormBase(PromptConfiguration configuration)

protected TextInputBuffer InputBuffer { get; } = new();

protected Dictionary<ConsoleKey, Func<ConsoleKeyInfo, bool>> KeyHandlerMaps { get; set; } = new();
protected Dictionary<ConsoleKeyBinding, Func<bool>> KeyHandlerMaps { get; set; }

protected int Width => _consoleDriver.WindowWidth;

Expand Down Expand Up @@ -103,7 +108,9 @@ private bool TryGetResult([NotNullWhen(true)] out T? result)
return HandleEnter(out result);
}

if (KeyHandlerMaps.TryGetValue(keyInfo.Key, out var keyHandler) && keyHandler(keyInfo))
var binding = new ConsoleKeyBinding(keyInfo.Key, keyInfo.Modifiers);

if (KeyHandlerMaps.TryGetValue(binding, out var keyHandler) && keyHandler())
{
continue;
}
Expand Down Expand Up @@ -135,4 +142,11 @@ private void CancellationHandler()

Environment.Exit(1);
}

private bool HandleEscape()
{
CancellationHandler();

return true;
}
}
6 changes: 3 additions & 3 deletions Sharprompt/Forms/ListForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ protected override bool HandleEnter([NotNullWhen(true)] out IEnumerable<T>? resu
return false;
}

protected override bool HandleDelete(ConsoleKeyInfo keyInfo)
protected override bool HandleCtrlDelete()
{
if (keyInfo.Modifiers == ConsoleModifiers.Control && _inputItems.Count > 0)
if (_inputItems.Count > 0)
{
_inputItems.RemoveAt(_inputItems.Count - 1);

return true;
}

return base.HandleDelete(keyInfo);
return base.HandleCtrlDelete();
}
}
22 changes: 6 additions & 16 deletions Sharprompt/Forms/MultiSelectForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public MultiSelectForm(MultiSelectOptions<T> options, PromptConfiguration config
_selectedItems.Add(defaultValue);
}

KeyHandlerMaps[ConsoleKey.Spacebar] = HandleSpacebar;
KeyHandlerMaps[ConsoleKey.A] = HandleAWithControl;
KeyHandlerMaps[ConsoleKey.I] = HandleIWithControl;
KeyHandlerMaps[new ConsoleKeyBinding(ConsoleKey.Spacebar)] = HandleSpacebar;
KeyHandlerMaps[new ConsoleKeyBinding(ConsoleKey.A, ConsoleModifiers.Control)] = HandleCtrlA;
KeyHandlerMaps[new ConsoleKeyBinding(ConsoleKey.I, ConsoleModifiers.Control)] = HandleCtrlI;
}

private readonly MultiSelectOptions<T> _options;
Expand Down Expand Up @@ -100,7 +100,7 @@ protected override bool HandleEnter([NotNullWhen(true)] out IEnumerable<T>? resu
return false;
}

private bool HandleSpacebar(ConsoleKeyInfo keyInfo)
private bool HandleSpacebar()
{
if (!Paginator.TryGetSelectedItem(out var currentItem))
{
Expand All @@ -122,13 +122,8 @@ private bool HandleSpacebar(ConsoleKeyInfo keyInfo)
return true;
}

private bool HandleAWithControl(ConsoleKeyInfo keyInfo)
private bool HandleCtrlA()
{
if (keyInfo.Modifiers != ConsoleModifiers.Control)
{
return false;
}

if (_selectedItems.Count == Paginator.TotalCount)
{
_selectedItems.Clear();
Expand All @@ -144,13 +139,8 @@ private bool HandleAWithControl(ConsoleKeyInfo keyInfo)
return true;
}

private bool HandleIWithControl(ConsoleKeyInfo keyInfo)
private bool HandleCtrlI()
{
if (keyInfo.Modifiers != ConsoleModifiers.Control)
{
return false;
}

var invertedItems = Paginator.Except(_selectedItems).ToArray();

_selectedItems.Clear();
Expand Down
7 changes: 2 additions & 5 deletions Sharprompt/Forms/PasswordForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ public PasswordForm(PasswordOptions options, PromptConfiguration configuration)

_options = options;

KeyHandlerMaps = new()
{
[ConsoleKey.Backspace] = HandleBackspace
};
KeyHandlerMaps[new ConsoleKeyBinding(ConsoleKey.Backspace)] = HandleBackspace;
}

private readonly PasswordOptions _options;
Expand Down Expand Up @@ -63,7 +60,7 @@ protected override bool HandleEnter([NotNullWhen(true)] out string? result)
return true;
}

private bool HandleBackspace(ConsoleKeyInfo keyInfo)
private bool HandleBackspace()
{
if (InputBuffer.IsStart)
{
Expand Down
20 changes: 10 additions & 10 deletions Sharprompt/Forms/SelectFormBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ protected void InitializePaginator(IEnumerable<TItem> items, int pageSize, Optio
LoopingSelection = loopingSelection
};

KeyHandlerMaps[ConsoleKey.UpArrow] = HandleUpArrow;
KeyHandlerMaps[ConsoleKey.DownArrow] = HandleDownArrow;
KeyHandlerMaps[ConsoleKey.LeftArrow] = HandleLeftArrow;
KeyHandlerMaps[ConsoleKey.RightArrow] = HandleRightArrow;
KeyHandlerMaps[ConsoleKey.Backspace] = HandleBackspace;
KeyHandlerMaps[new ConsoleKeyBinding(ConsoleKey.UpArrow)] = HandleUpArrow;
KeyHandlerMaps[new ConsoleKeyBinding(ConsoleKey.DownArrow)] = HandleDownArrow;
KeyHandlerMaps[new ConsoleKeyBinding(ConsoleKey.LeftArrow)] = HandleLeftArrow;
KeyHandlerMaps[new ConsoleKeyBinding(ConsoleKey.RightArrow)] = HandleRightArrow;
KeyHandlerMaps[new ConsoleKeyBinding(ConsoleKey.Backspace)] = HandleBackspace;
}

protected override bool HandleTextInput(ConsoleKeyInfo keyInfo)
Expand All @@ -45,35 +45,35 @@ protected void RenderPagination(OffscreenBuffer offscreenBuffer, Func<int, int,
}
}

private bool HandleUpArrow(ConsoleKeyInfo keyInfo)
private bool HandleUpArrow()
{
Paginator.PreviousItem();

return true;
}

private bool HandleDownArrow(ConsoleKeyInfo keyInfo)
private bool HandleDownArrow()
{
Paginator.NextItem();

return true;
}

private bool HandleLeftArrow(ConsoleKeyInfo keyInfo)
private bool HandleLeftArrow()
{
Paginator.PreviousPage();

return true;
}

private bool HandleRightArrow(ConsoleKeyInfo keyInfo)
private bool HandleRightArrow()
{
Paginator.NextPage();

return true;
}

private bool HandleBackspace(ConsoleKeyInfo keyInfo)
private bool HandleBackspace()
{
if (InputBuffer.IsStart)
{
Expand Down
Loading
Loading