Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CancelCommand and Bind it to EscapeKey. #8

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions src/RadLine.Tests/Commands/CancelCommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Shouldly;
using Xunit;

namespace RadLine.Tests
{
public sealed class CancelCommandTests
{
[Fact]
public void Should_Cancel_Input()
{
// Given
var buffer = new LineBuffer("Foo");
var context = new LineEditorContext(buffer);
buffer.Insert("Bar");
var command = new CancelCommand();

// When
command.Execute(context);

// Then
buffer.Content.ShouldBe("Foo");
buffer.Position.ShouldBe(3);
}
}
}
20 changes: 20 additions & 0 deletions src/RadLine.Tests/LineEditorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ namespace RadLine.Tests
{
public sealed class LineEditorTests
{
[Fact]
public async Task Should_Return_Original_Text_When_Pressing_Escape()
{
// Given
var editor = new LineEditor(
new TestConsole(),
new TestInputSource()
.Push("Bar")
.PushEscape())
{
Text = "Foo",
};

// When
var result = await editor.ReadLine(CancellationToken.None);

// Then
result.ShouldBe("Foo");
}

[Fact]
public async Task Should_Return_Entered_Text_When_Pressing_Enter()
{
Expand Down
6 changes: 6 additions & 0 deletions src/RadLine.Tests/Utilities/TestInputSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public TestInputSource PushEnter()
return this;
}

public TestInputSource PushEscape()
{
Push(ConsoleKey.Escape);
return this;
}

public TestInputSource Push(ConsoleKey input, ConsoleModifiers modifiers)
{
var shift = modifiers.HasFlag(ConsoleModifiers.Shift);
Expand Down
10 changes: 10 additions & 0 deletions src/RadLine/Commands/CancelCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace RadLine;

public sealed class CancelCommand : LineEditorCommand
{
public override void Execute(LineEditorContext context)
{
context.Buffer.Reset();
context.Submit(SubmitAction.Cancel);
}
}
1 change: 1 addition & 0 deletions src/RadLine/KeyBindingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static void AddDefault(this KeyBindings bindings)
bindings.Add<NextWordCommand>(ConsoleKey.RightArrow, ConsoleModifiers.Control);
bindings.Add<PreviousHistoryCommand>(ConsoleKey.UpArrow, ConsoleModifiers.Control);
bindings.Add<NextHistoryCommand>(ConsoleKey.DownArrow, ConsoleModifiers.Control);
bindings.Add<CancelCommand>(ConsoleKey.Escape);
bindings.Add<SubmitCommand>(ConsoleKey.Enter);
bindings.Add<NewLineCommand>(ConsoleKey.Enter, ConsoleModifiers.Shift);
}
Expand Down
12 changes: 11 additions & 1 deletion src/RadLine/LineBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ namespace RadLine
{
public sealed class LineBuffer
{
private readonly string _initialContent;
private string _buffer;
private int _position;

public int Position => _position;
public int Length => _buffer.Length;
public string InitialContent => _initialContent;
public string Content => _buffer;

public bool AtBeginning => Position == 0;
Expand Down Expand Up @@ -77,7 +79,8 @@ public bool IsAtEndOfWord

public LineBuffer(string? content = null)
{
_buffer = content ?? string.Empty;
_initialContent = content ?? string.Empty;
_buffer = _initialContent;
_position = _buffer.Length;
}

Expand All @@ -88,6 +91,7 @@ public LineBuffer(LineBuffer buffer)
throw new ArgumentNullException(nameof(buffer));
}

_initialContent = buffer.InitialContent;
_buffer = buffer.Content;
_position = _buffer.Length;
}
Expand Down Expand Up @@ -115,6 +119,12 @@ public void Insert(string text)
_buffer = _buffer.Insert(_position, text);
}

public void Reset()
{
_buffer = _initialContent;
_position = _buffer.Length;
}

public int Clear(int index, int count)
{
if (index < 0)
Expand Down
5 changes: 3 additions & 2 deletions src/RadLine/LineEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ public static bool IsSupported(IAnsiConsole console)
_history.Add(state.GetBuffers());
}

// Return all the lines
return cancelled ? null : state.Text;
// If cancelled return initial text otherwise
// return text from current state
return cancelled ? Text : state.Text;
}

private async Task<(LineBuffer Buffer, SubmitAction Result)> ReadLine(
Expand Down
4 changes: 3 additions & 1 deletion src/RadLine/LineEditorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ public LineEditorContext(LineBuffer buffer, IServiceProvider? provider = null)
{
_state = new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase);
_provider = provider;

Buffer = buffer ?? throw new ArgumentNullException(nameof(buffer));
InitialText = buffer.Content;
}

public string? InitialText { get; }

public object? GetService(Type serviceType)
{
if (_provider != null)
Expand Down
Binary file not shown.
Loading