Skip to content

Commit

Permalink
Merge pull request #191 from kindermannhubert/disable-history-scrolli…
Browse files Browse the repository at this point in the history
…ng-in-multiline

Disable history scrolling by UpArrow in multiline statements (#181).
  • Loading branch information
kindermannhubert authored Apr 2, 2022
2 parents b7105ed + 17411c5 commit c59b0c9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/PrettyPrompt/History/HistoryLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,20 @@ public async Task OnKeyUp(KeyPress key, CancellationToken cancellationToken)

if (history.Count == 0 || key.Handled) return;

var contents = codePane.Document.GetText();
if (contents.Contains('\n'))
{
//we do not want to cycle in history in multiline documents
return;
}

switch (key.ObjectPattern)
{
case UpArrow:
if (currentIndex == -1)
{
currentIndex = history.Count - 1;
unsubmittedBuffer.SetContents(codePane.Document.GetText());
unsubmittedBuffer.SetContents(contents);
}
else if (currentIndex > 0)
{
Expand Down Expand Up @@ -189,7 +196,7 @@ internal async Task SavePersistentHistoryAsync(string input)
if (history.Count == 0 || history[^1] != input) //filter out duplicates
{
var entry = Convert.ToBase64String(Encoding.UTF8.GetBytes(input));
await File.AppendAllLinesAsync(persistentHistoryFilepath, new[] { entry }).ConfigureAwait(false);
await File.AppendAllLinesAsync(persistentHistoryFilepath, new[] { entry }).ConfigureAwait(false);
}
}
}
21 changes: 20 additions & 1 deletion tests/PrettyPrompt.Tests/HistoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#endregion

using System;
using System.IO;
using System.Threading.Tasks;
using Xunit;
using static System.ConsoleKey;
using static System.ConsoleModifiers;

namespace PrettyPrompt.Tests;

Expand Down Expand Up @@ -257,4 +259,21 @@ public async Task ReadLine_PersistentHistory_Deduplication()
File.Delete(historyFile);
}
}
}

/// <summary>
/// https://github.com/waf/PrettyPrompt/issues/181
/// </summary>
[Fact]
public async Task ReadLine_UpArrow_DoesNotCycleThroughHistory_InMultilineStatements()
{
var console = ConsoleStub.NewConsole();
var prompt = new Prompt(console: console);

console.StubInput($"a{Enter}");
await prompt.ReadLineAsync();

console.StubInput($"{Shift}{Enter}{UpArrow}{UpArrow}{UpArrow}{UpArrow}{UpArrow}b{Enter}");
var result = await prompt.ReadLineAsync();
Assert.Equal($"b{Environment.NewLine}", result.Text);
}
}

0 comments on commit c59b0c9

Please sign in to comment.